views:

43

answers:

1

Hello!

Im 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, im 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 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 alertDialogs of the other 2 items, the option selected replaces the subtitle of the first item!

Any idea of how can i fix that?

A: 

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;


[...]
Kitinz
The real answer is to use a better Adapter to populate the list, then update the adapter on click.
Christopher
Can you explain yourself a little more? In this code, the listView is already populated. "ventana" is defined as a AlertDialog.Builder, and the issue was about changing the listView content depending on the alertDialog choice. Maybe you can tell me something about this adapter? :D
Kitinz