1

I am trying to multi-select list-view by setting drawable in OnItemClickListener,but when I scroll the list-view selected items loses drawable which I have changed on listview.OnitemClickListener in ItemChecked method.

What all I have tried :

1.Using xml selector in drawable

2.Tried changing drawable of selecteditems in custom adapter.

None of the above is working neither any of solution on stackoverflow.

Here is my adapter:

public class QuestionsOptionsAdapterMultipleSelectionLV extends BaseAdapter {
    Context context;
    ArrayList<QuestionOptionsModel> items;
    String which;
    CollegepondSandbox cs;
    ArrayList<String> arr;
    boolean isTouch = false;
    int positionselected;
    QuestionsOptionsAdapterMultipleSelectionLV adapter1;



 public QuestionsOptionsAdapterMultipleSelectionLV(Context context, 
        ArrayList<QuestionOptionsModel> items) {
        this.context = context;
        this.items = items;
        this.which = which;
        this.arr = arr;
        cs = new CollegepondSandbox();


    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
       // positionselected =items.indexOf(items.get(position));
        return items.indexOf(items.get(position));
    }

    @Override
    public int getViewTypeCount() {
        return getCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup 
    parent) {
            if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) 
    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.custom_options_multiple, 
     null);

        WebView tvOptiontext;
        ImageView ivOption;

        LinearLayout llOptionsMultiple;

        ivOption = (ImageView) convertView.findViewById(R.id.cbOption);
        tvOptiontext = (WebView) convertView.findViewById(R.id.tvOptiontext);
        llOptionsMultiple = (LinearLayout) convertView.findViewById(R.id.llOptionsMultiple);
        tvOptiontext.loadData(items.get(position).QOption, "text/html", "utf-8");

        return convertView;
        }
       }

here is my OnItemClickListener:

  lvOption.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            lvOption.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {

                    ivOption = (ImageView) view.findViewById(R.id.cbOption);

                       if (lvOption.isItemChecked(position)) {
                            ivOption.setImageResource(R.drawable.tick_2);
       multipleSelectedIDs.add(answersal.get(position).QIsCorrect);
                        } else {
                            ivOption.setImageResource(R.drawable.tick_1);
      multipleSelectedIDs.remove(answersal.get(position).QIsCorrect);
                        }
                });
abbasalid
  • 43
  • 14

1 Answers1

0

Found a similar issue on stackoverflow @Shaiful and modified as per my needs.

Steps:

1.Store the positions as string in Arraylist

2.pass the arraylist to adapter.

3.Change drawable in adapter and not in activity as show in code below:

Changes in listview.OnItemClick in activity :

  if (lvOption.isItemChecked(position)) {
                        multipleSelectedIDs.add(answersal.get(position).QIsCorrect);
                        selected.add(String.valueOf(position));
                    } else {
                        multipleSelectedIDs.remove(answersal.get(position).QIsCorrect);
                        selected.remove(String.valueOf(position));
                    }
                    adapterMultipleListview.setSelectedDrawable(selected);

Now changes in adapter :

//add this method in adapter
 public void setSelectedDrawable(ArrayList<String> ind) {
    selectedIndex = ind;
    notifyDataSetChanged();
}
//change drawable in getview() method
 if (selectedIndex.contains(String.valueOf(position))) {
        ivOption.setBackgroundResource(R.drawable.tick_2);
    } else {
        ivOption.setBackgroundResource(R.drawable.tick_1);
    }
abbasalid
  • 43
  • 14