0

Ich möchte die Werte der 3 Edittexte ändern, nachdem der Benutzer im Alarmdialog bestätigt hat. Aber es wird nicht aktualisiert. Die Werte ändern sich erst nach dem Verschieben zu einem anderen Fragment und zurück zu diesem Fragment kommendenWie aktualisiert man den Edittext, nachdem der Alertdialog geschlossen wurde

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)); 
} 

}

hier // ist mein Dialog in der Tätigkeit

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

Sie können Dialog ablehnen Listener verwenden, um Ihre Ansicht zu aktualisieren. –

+0

Wo ist dein Dialog in deinem Code? –

+0

Wie aktualisiert man die Ansichten des Fragmentdialogs Listener löschen –

Antwort

3
abc.show(); 
abc.setOnDismissListener(new DialogInterface.OnDismissListener() { 
    @Override 
    public void onDismiss(DialogInterface dialog) { 
     fragmentInstance.refreshEditTextValue(value); 
    } 
}); 

In Ihrem Fragment:

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

Danke man.it funktionierte –

Verwandte Themen