0

I have a listview which contains TextView and ImageView.

Here it is:

enter image description here

OnClick of ImageView changes opens an alertdialog with buttons Yes and No.

enter image description here

if press Yes

imageView changes its background to blue,

if press no,

imageView background changes to red.

enter image description here

Till now it is working fine.

I have a search edittext above the listview, which searches based on the textview in the listview.

Now on searching, the imageview is not showing the changed background, but the default background only. See this:

enter image description here

What should I do to make the imageview show the image which was selected in the alert dialog.

Code

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
   View v = convertView;
  if (v == null) {
   LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   v = vi.inflate(R.layout.custom_lay, null);
  }else{
    v=convertView;
  }
  holder = new ViewHolder();
    holder.code = (TextView) v.findViewById(R.id.DealerName);
  holder.btnName = (ImageView) v.findViewById(R.id.BtnStreet); 
    holder.btnName.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(final View vi) {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage("select any one?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {



//       holder.btnName.setBackgroundResource(R.drawable.green_icon);
     vi.setBackgroundResource(R.drawable.blue_icon);
//     notifyDataSetChanged();
    }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int which) {

      holder.btnName.setImageResource(R.drawable.blue_icon);
      //vi.setBackgroundResource(R.drawable.green_icon);
//      notifyDataSetChanged();
     }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
   }
  });
     v.setTag(holder);
     ViewHolder hold = (ViewHolder) v.getTag();
    String lb = DisplayedValues.get(position);
    hold.code.setText(getItem(position));
//   hold.btnName.setBackgroundResource(getSelectedItemPosition());

  return v;
 }
K Guru
  • 1,292
  • 2
  • 17
  • 36
Aishvarya Jaiswal
  • 1,793
  • 6
  • 34
  • 72
  • You need to maintain a model class to know which button was pressed. Otherwise, the listview will forget which was pressed even on scroll. Please refer to this ansewr: http://stackoverflow.com/a/27014443/2389078 – DroidDev Jan 16 '15 at 07:13
  • please add the searching code – K Guru Jan 16 '15 at 07:36
  • and store the background changes Globally and use them while filling listview based on search criteria – K Guru Jan 16 '15 at 07:38

1 Answers1

0

This happens because the ListView re-uses the item layouts. It inflates only as many as are visible on a screen screen at a single time. That way it saves memory.

You need to save the state of every ListView item ImageView. For example in a boolean[].

The background (based on your state array) should be set in getView method of your adapter.

The switching and saving of your ImageView background state should happen in the OnClickListener.

Simas
  • 43,548
  • 10
  • 88
  • 116