Reputation: 1542
I'm using a custom ListView
with a Title
and a Subtitle
where you can read a brief explanation of the item.
For each item on the list, I'm displaying an AlertDialog
to select an option (different for each case). When the option is selected, i want to change the Subtitle for the option selected by the user.
This is what I've tried:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
case 1:
final CharSequence[] alertText2 = {"1", "2", "3", "5", "10", "20", "60"};
ventana.setTitle("Max. duration");
ventana.setItems(alertText2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText2[item]);
}
});
ventana.show();
break;
case 2:
final CharSequence[] alertText3 = {"3", "5", "10", "20", "30", "60"};
ventana.setTitle("Time between events");
ventana.setItems(alertText3, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText3[item]);
}
});
ventana.show();
break;
For the first item on the list it works fine, when I select an option, the subtitle get replaced by that option, but when I make a selection in the AlertDialog
s of the other 2 items, the option selected replaces the subtitle of the first item!
Any idea of how can I fix that?
Upvotes: 1
Views: 3019
Reputation: 1542
Since nobody answered the question and i find a solution, im going to publish it here to help other people who eventually can be facing the same problem or a similar one :D
I just remove the TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
from each case and added it before the switch starts but "taking" the view argument on the onClick function (the type final, is because Eclipse warned me about it :P) : final TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
The code looks like this:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
switch(position){
case 0: final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
[...]
Upvotes: 4