@Override
public void onCheckBoxClick(int position) {
mTaskList.remove(position);
adapter.notifyDataSetChanged();
}
就像点击被转移到下一个项目一样,因为项目被删除得太快了。
修改
这是CustomAdapters getView()方法。它是一个RPG主题任务列表。
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View taskItemView = convertView;
if (taskItemView == null) {
taskItemView = LayoutInflater.from(getContext()).inflate(R.layout.task_item, parent, false);
}
TaskItem currentTaskItem = getItem(position);
//set all Item Texts accordingly
TextView taskName = (TextView) taskItemView.findViewById(R.id.task_name_xml);
taskName.setText(currentTaskItem.getName());
TextView difficultyText = (TextView) taskItemView.findViewById(R.id.task_difficulty_xml);
difficultyText.setText(currentTaskItem.getDifficultyText());
TextView expGainText = (TextView) taskItemView.findViewById(R.id.exp_win_xml);
expGainText.setText("" + currentTaskItem.getExpAmountGained() + " EXP");
TextView goldGainText = (TextView) taskItemView.findViewById(R.id.currency_win_xml);
goldGainText.setText("" + currentTaskItem.getGoldAmountGained());
CheckBox checkbox = (CheckBox) taskItemView.findViewById(R.id.task_checkbox_xml);
checkbox.setChecked(false);
//set Checkbox Listener
checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mListener.onCheckBoxClick(position);
}
});
return taskItemView;
}
答案 0 :(得分:1)
您需要改变一些事情。
为您的任务添加一个View holder类:
class TaskViewHolder
{
TextView taskName;
TextView difficultyText;
TaskViewHolder(View root)
{
taskName = (TextView) taskItemView.findViewById(R.id.task_name_xml);
difficultyText = (TextView) taskItemView.findViewById(R.id.task_difficulty_xml);
}
}
然后像这样更改你的getView():
TaskViewHolder holder;
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.task_item, parent, false);
holder = new TaskViewHolder(convertView);
convertView.setTag(holder);
}
else
holder = (TaskViewHolder)convertView.getTag();
TaskItem currentTaskItem = getItem(position);
holder.taskName.setText(currentTaskItem.getDifficultyText());
holder.difficultyText.setText(currentTaskItem.getDifficultyText());
return convertView;
我只包含了2条您的观点,但您明白了。
设置oncheckchanged侦听器时,请将其设置为:
holder.checkBox.setTag(位置); holder.setOnClickListener(本);
然后在你的onClick(View v)回调中执行以下操作:
//check if this is the correct view
int position = (int)v.getTag();
TaskItem t = getItem(position);
//do what you like here
同样,我还没有包含持有人的所有观点,所以你应该添加它们。这应该可以解决您的问题。