我正在使用ArrayAdapter
ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
问题是,当我从listview
中选择第一项时,它会自动选择另一项。
我没有选择第12项
所有代码
final ListView listView1 = (ListView) findViewById(R.id.listView);
Product[] items = {
new Product(1, "Milk", 21.50),
new Product(2, "Butter", 15.99),
new Product(3, "Yogurt", 14.90),
new Product(4, "Toothpaste", 7.99),
new Product(5, "Ice Cream", 10.00),
new Product(6, "Milk", 21.50),
new Product(7, "Butter", 15.99),
new Product(8, "Yogurt", 14.90),
new Product(9, "Toothpaste", 7.99),
new Product(10, "Ice Cream", 10.00),
new Product(11, "Milk", 21.50),
new Product(12, "Butter", 15.99),
new Product(13, "Yogurt", 14.90),
new Product(14, "Toothpaste", 7.99),
new Product(15, "Ice Cream", 10.00),
new Product(16, "Milk", 21.50),
new Product(17, "Butter", 15.99),
new Product(18, "Yogurt", 14.90),
new Product(19, "Toothpaste", 7.99),
new Product(20, "Ice Cream", 10.00),
};
ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView) view).getText().toString();
view.setSelected(true);
listView1.setItemChecked(position, true);
view.setBackgroundColor(Color.GREEN);
}
});
答案 0 :(得分:1)
这是因为您的适配器重用了膨胀的视图。
一种解决方案是覆盖您的getView()
ViewHolder
方法,以便正确呈现您的观看次数,并且(甚至更好)实施if (modeldata[variable] ....
,这样您就不会失去任何表现一次又一次地膨胀!
答案 1 :(得分:0)
在链接http://paste.ubuntu.com/14848762/分享的CustomArrayAdapter
中,您正在relativeLayout
上更改Click Listener
的颜色,当您滚动或查看回收时,您应该重置此布局,并且应该为您单击的项启用颜色。例如,您可以使用此方法。
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int color;
if (tt8.getText().toString().equals("1")){
color = Color.RED;
relativeLayout.setBackgroundColor(color);
}else {
color = Color.GREEN;
relativeLayout.setBackgroundColor(color);
}
p.setColor(color);
}
});
relativeLayout.setBackgroundColor(p.getColor());
希望这有帮助。
答案 2 :(得分:-1)
尝试进行此更改
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = listView1.getItemAtPosition(position);
Log.d("position", position+", item");
}
});
在下面的评论中更新你在logcat中获得的值,以便我可以建议更好的代码。