Listview 在选择模式下滚动时不保持可绘制 "CHOICE_MODE_MULTIPLE"

Listview not holding drawable on scroll in choice mode "CHOICE_MODE_MULTIPLE"

我正在尝试通过在 OnItemClickListener 中设置可绘制对象来实现多select 列表视图,但是当我滚动列表视图时 selected 项目丢失了我在 [=27] 上更改的可绘制对象=] 在 ItemChecked 方法中。

我都试过了:

1.Using xml select或在 drawable 中

2.Tried 在自定义适配器中更改 select 编辑项的可绘制对象。

None 上面的方法在 Whosebug 上的任何解决方案都不起作用。

这是我的适配器:

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;
        }
       }

这是我的 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);
                        }
                });

在 Whosebug @Shaiful 上发现了类似的问题,并根据我的需要进行了修改。

步骤:

1.Store Arraylist

中字符串的位置

2.pass 数组列表到适配器。

3.Change 可在适配器中绘制,而不是在 activity 中绘制,如下面的代码所示:

listview.OnItemClick 在 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);

现在适配器发生变化:

//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);
    }