【发布时间】:2014-09-29 05:16:54
【问题描述】:
面临一个非常奇怪的问题。我在每个项目上都有一个listview 和checkbox,当我单击listview 的第一个项目时,它被选中,但是与这个项目一起,列表视图的最后一个项目也被选中! 当我滚动列表视图时。没有滚动它可以完美运行。
另外,当我选择最后一个项目时,滚动后第一个项目也随之被选中。提醒一下,第一个和最后一个之间的所有其他项目都可以正常工作。有趣的是,在选择第一个或最后一个项目后,如果我不滚动 listview 2-3 秒,它就会完美运行。所以我期待这可能是一个问题视图渲染或有点。谁能指点我这里到底发生了什么..
Cursor cursor = queryDatabase();
// The desired columns to be bound
String[] columns = new String[] {
DataBaseHelper.ROW_PROFILE_NAME,
DataBaseHelper.ROW_PROFILE_TYPE,
DataBaseHelper.ROW_ID
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.profileName,
R.id.profileStatus
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
mAdapter = new SimpleCursorAdapter(
this, R.layout.profile_listview_delete_item,
cursor,
columns,
to,
0);
listView = (ListView) findViewById(R.id.profile_listview_delete_main);
// Assign adapter to ListView
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
CheckBox cb;
Cursor c = mAdapter.getCursor();
String prodile_id = c.getString(c.getColumnIndex(DataBaseHelper.ROW_ID));
cb = (CheckBox)arg1.findViewById(R.id.checkbox);
cb.toggle();
if(cb.isChecked())
{
profileStack.add(prodile_id);
//Toast.makeText(getApplicationContext(), String.valueOf(profileStack), Toast.LENGTH_LONG).show();
counter++;
}
else if(!cb.isChecked())
{
profileStack.remove(prodile_id);
//Toast.makeText(getApplicationContext(), String.valueOf(profileStack), Toast.LENGTH_LONG).show();
counter--;
}
countSelectedItem.setText(String.valueOf(counter)+" items selected");
mAdapter.notifyDataSetChanged();
}
});
【问题讨论】:
-
您能否发布您的适配器类我认为这是视图持有者模式的问题?
-
@Rajesh,我正在使用 SimpleCursorAdapter,你可以看到上面的代码。
-
你的编码方式不对。您必须将自定义适配器与 viewholder 类一起使用,从该适配器更改复选框的值时,您必须从适配器更改字段的值,这与此复选框有关。单击列表视图后,获取位置并从适配器获取值。
标签: android listview simplecursoradapter onitemclick