我有一个带有自定义适配器的ListView,每个项目都有一个标题和一个描述按钮,弹出一个Dialog,每个项目都有一个唯一的描述。一切似乎都在工作,除了滚动一点点后,描述按钮拉出了错误的描述。根据我的理解,ListView中的幕后必须发生一些事情,导致它在滚动后没有将正确的值绑定到项目。我怀疑问题可能是我在Activity类的ListView上设置OnItemClickListener,以及每个Button的行为。以下代码来自包含ListView的Activity。
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
//CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
//checkBox.setChecked(!checkBox.isChecked());
final View tempView = view;
LinearLayout countLayout = (LinearLayout) view.findViewById(R.id.counter_layout);
TextView count = (TextView) countLayout.findViewById(R.id.counter);
ImageView add = (ImageView) countLayout.findViewById(R.id.add_sign);
ImageView minus = (ImageView) countLayout.findViewById(R.id.minus_sign);
ImageView description = (ImageView) view.findViewById(R.id.info_button);
minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list.get(position).setCount(list.get(position).getCount() - 1);
LinearLayout countLayout = (LinearLayout) tempView.findViewById(R.id.counter_layout);
TextView count = (TextView) countLayout.findViewById(R.id.counter);
count.setText("" + list.get(position).getCount());
try {
adapter.notifyDataSetChanged();
writeObject(getApplicationContext(), "checklist", new Lists(list, customList));
} catch (Exception e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(), "Change not saved", Toast.LENGTH_SHORT);
toast.show();
}
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
list.get(position).setCount(list.get(position).getCount() + 1);
LinearLayout countLayout = (LinearLayout) tempView.findViewById(R.id.counter_layout);
TextView count = (TextView) countLayout.findViewById(R.id.counter);
count.setText("" + list.get(position).getCount());
try {
adapter.notifyDataSetChanged();
writeObject(getApplicationContext(), "checklist", new Lists(list, customList));
} catch (Exception e) {
e.printStackTrace();
Toast toast = Toast.makeText(getApplicationContext(), "Change not saved", Toast.LENGTH_SHORT);
toast.show();
}
}
});
description.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog alert = new AlertDialog.Builder(ChecklistActivity.this)
.setTitle("Description")
.setMessage(list.get(position).getDescription())
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Do Nothing
}
})
.create();
alert.setCanceledOnTouchOutside(true);
alert.show();
}
});
}
});