我使用下面的代码来禁用ListView
项。现在,问题是在禁用一个项目后,如果用户点击另一个项目,它会禁用当前项目,但会从禁用中删除最后一项。
如何防止这个问题?
int pos;
SimpleAdapter adapter = new SimpleAdapter(this, arrlist, R.layout.topicwisequestion, new String[] { "option" }, new int[] { R.id.option }) {
public boolean isEnabled(int position) {
if (position != 0) {
if (position == pos) {
return false;
} else {
return true;
}
} else {
return true;
}
}
};
lvTWOptions.setAdapter(adapter);
lvTWOptions.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pos = position;
}
});
答案 0 :(得分:1)
您需要维护已禁用的项目列表,并查看isEnabled列表中是否存在该项目。
如下:
ArrayList<Integer> pos=new ArrayList<Integer>();
SimpleAdapter adapter = new SimpleAdapter(this, arrlist, R.layout.topicwisequestion, new String[] { "option" }, new int[] { R.id.option }) {
public boolean isEnabled(int position) {
if (position != 0) {
if (pos.contains(position)) {
return false;
} else {
return true;
}
} else {
return true;
}
}
};
lvTWOptions.setAdapter(adapter);
lvTWOptions.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pos.add(position);
}
});