As long as an EditText with ListView is difficult to figure out, I tried another solution : A ListView with TextViews and when you click on one, I catch the focused TextView with a listener (a OnItemClickListener on the parent ListView) and I open an AlertDialog.
ISSUE : When I press the OK button on the AlertDialog, I want the focused TextView to get the text value of the alert dialog’s EditText, but it doesn’t work, he keeps the same text value. On activity :
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
id ++;
t = new TextView(activity);
t = (TextView) v;
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(activity);
input.setText(t.getText());
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Changer la valeur dans la base et dans la liste
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS,0);
t.setText(input.getText());
System.out.println("input avant : " + t.getText());
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
});
I think this code is enough for you to know what’s wrong in my code. Just to let you know :
ListViewand t (TextView) are declared as private variables on the activity class- there is an
Adapterclass that builds automatically theTextViewlist - if I change the value of an
EditTexton theListViewlistener, it works, but in the dialog alert listener, it doesn’t.
Ask if you want more info.
I don’t know how you built your adapter so I will guess an answer. You’ll have to put your text in the adapter data(for example in the
ArrayListthat you give to your adapter) and callnotifyDataSetChanged()on the adapter.