我试图通过在OnItemClickListener中设置drawable来多选列表视图,但是当我滚动列表视图时,所选项目丢失了drawable,我在ItemChecked方法中的listview.OnitemClickListener上更改了。
我所做的一切:
1.在drawable中使用xml选择器
2.在自定义适配器中改变了选择项的可绘制性。
以上都不适用于stackoverflow上的任何解决方案。
这是我的适配器:
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);
}
});
这是我的OnItemClickListener:
a = [{'employee_name': 'A', 'total_duration': 0,},
{'employee_name': 'B', 'total_duration': 11.0},
{'employee_name': 'C', 'total_duration': 9.0},
{'employee_name': 'D', 'total_duration':4.0,},
{'employee_name': 'E', 'total_duration': 0}]
b = [{'employee_name': 'A', 'actual_duration': 28},
{'employee_name': 'C', 'actual_duration': 22},
{'employee_name': 'D', 'actual_duration': 15}]
final_a = [{'employee_name':i['employee_name'], 'total_duration':i['total_duration'], 'actual_duration':0 if not any(i['employee_name'] == c['employee_name'] for c in b) else [c['actual_duration'] for c in b if c['employee_name'] == i['employee_name']][0]} for i in a]
答案 0 :(得分:0)
在stackoverflow @Shaiful上发现了类似的问题,并根据我的需要进行了修改。
步骤:
1.将位置存储为Arraylist中的字符串
2.将arraylist传递给适配器。
3.更改适配器中的drawable而不是活动,如下面的代码所示:
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);
现在更改适配器:
//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);
}