0

ユーザがalertdialogでconfirmを押した後で3つのedittextの値を変更したいと思います。しかし、リフレッシュしていません。このフラグメントalertdialogが閉じられた後にedittextをリフレッシュする方法

SharedPreferences sp; 
EditText ip,port,appkey; 
Button connect; 

public ConnectFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v= inflater.inflate(R.layout.fragment_connect, container, false); 
    ip= (EditText) v.findViewById(R.id.editid); 
    port= (EditText) v.findViewById(R.id.editport); 
    appkey= (EditText) v.findViewById(R.id.editsppKey); 
    Toast.makeText(getActivity(),"oncreateview",Toast.LENGTH_SHORT).show(); 
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE); 
    ip.setText(sp.getString("ip",null)); 
    port.setText(sp.getString("port",null)); 
    appkey.setText(sp.getString("appkey",null)); 
    connect= (Button) v.findViewById(R.id.connectf); 
    connect.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      senddata(); 
     } 
    }); 
    return v; 
} 
private void senddata() { 
    //senddata 

} 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    Toast.makeText(getActivity(),"on Activity Created",Toast.LENGTH_SHORT).show(); 
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE); 
    ip.setText(sp.getString("ip",null)); 
    port.setText(sp.getString("port",null)); 
    appkey.setText(sp.getString("appkey",null)); 
} 


@Override 
public void onAttachFragment(Fragment childFragment) { 
    super.onAttachFragment(childFragment); 
    Toast.makeText(getActivity(),"On Attach Fragment",Toast.LENGTH_SHORT).show(); 
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE); 
    ip.setText(sp.getString("ip",null)); 
    port.setText(sp.getString("port",null)); 
    appkey.setText(sp.getString("appkey",null)); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    Toast.makeText(getActivity(),"On Resume",Toast.LENGTH_SHORT).show(); 
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE); 
    ip.setText(sp.getString("ip",null)); 
    port.setText(sp.getString("port",null)); 
    appkey.setText(sp.getString("appkey",null)); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    Toast.makeText(getActivity(),"stop",Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    Toast.makeText(getActivity(),"pause",Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void setUserVisibleHint(boolean isVisibleToUser) { 
    super.setUserVisibleHint(isVisibleToUser); 
    if (this.isVisible()) { 
     // If we are becoming invisible, then... 
     if (!isVisibleToUser) { 
      sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE); 
      ip.setText(sp.getString("ip",null)); 
      port.setText(sp.getString("port",null)); 
      appkey.setText(sp.getString("appkey",null)); 
      Log.d("MyFragment", "Not visible anymore. Stopping audio."); 
      // TODO stop audio playback 
     } 
    } 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    Toast.makeText(getActivity(),"On start",Toast.LENGTH_SHORT).show(); 
    sp=getActivity().getSharedPreferences("connection", Context.MODE_PRIVATE); 
    ip.setText(sp.getString("ip",null)); 
    port.setText(sp.getString("port",null)); 
    appkey.setText(sp.getString("appkey",null)); 
} 

}に戻ってくることは

はここ活動

final View v=LayoutInflater.from(this).inflate(R.layout.formlayout,null); 
     AlertDialog.Builder ab=new AlertDialog.Builder(this); 
     ab.setView(v); 
     tp= (TextInputLayout) v.findViewById(R.id.ip); 
     tp1= (TextInputLayout) v.findViewById(R.id.port); 
     tp2= (TextInputLayout) v.findViewById(R.id.appkey); 

     ab.setTitle("Enter Connection Details"); 
     ab.setPositiveButton("confirm", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

       String a=tp.getEditText().getText().toString(); 
       String b=tp1.getEditText().getText().toString(); 
       String c=tp2.getEditText().getText().toString(); 
       if(!TextUtils.isEmpty(a)&&!TextUtils.isEmpty(b) && !TextUtils.isEmpty(c)) 
       { 
        SharedPreferences.Editor edit=sp.edit(); 
        edit.putString("ip",a); 
        edit.putString("port",b); 
        edit.putString("appkey",c); 
        edit.commit(); 
       } 
      } 
     }); 
     ab.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     AlertDialog abc=ab.create(); 
     abc.show(); 
     abc.setOnDismissListener(new DialogInterface.OnDismissListener() { 
      @Override 
      public void onDismiss(DialogInterface dialog) { 

      } 
     }); 
+0

あなたはビューを更新するためにダイアログ消滅リスナーを使うことができます。 –

+0

あなたのコードであなたの対話はどこですか? –

+0

フラグメントのビューをリフレッシュする方法ダイアログリスナーを解除する –

答えて

3
abc.show(); 
abc.setOnDismissListener(new DialogInterface.OnDismissListener() { 
    @Override 
    public void onDismiss(DialogInterface dialog) { 
     fragmentInstance.refreshEditTextValue(value); 
    } 
}); 
の私のダイアログです//

あなたの断片に:

public refreshEditTextValue(String value) { 
//TODO set value in edittext 
    } 
+0

ありがとうman.itが働いています –

関連する問題