配列のリストがあり、アダプターを介して渡してリストを埋めました。
実は、クリックされたアイテムに色を設定したかったのです。
問題は、最初のアイテムをクリックするたびに、最初と最後のアイテムの両方が同じ背景色になることです。
コード: Test.java
//To keep track of previously clicked textview
TextView last_clicked=null;
ListView lv=(ListView)findViewById(R.id.my_activity_list);
//My test array
String[] data={"one","two","three","four","five","six"};
list=new ArrayList<String>(data.length);
list.addAll(Arrays.asList(data));
//evolist is my custom layout
adapter= new ArrayAdapter<String>(c,R.layout.evolist,list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> ad, View v, int pos, long id){
//set black for other
if(last_clicked!=null)
last_clicked.setBackgroundColor(Color.BLACK);
//set red color for selected item
TextView tv=(TextView)v;
//I also tried TextView tv=(TextView)v.findViewById(R.id.tvo)
//I tried printing tv.getId() and noticed that both first and last had same IDs
tv.setBackgroundColor( Color.RED );
last_clicked=tv;
}
});
レイアウト: evolist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/white"
android:padding="10dp"
android:textSize="20sp">
</TextView>
どこが間違っているのですか、またはこの種のエラーが発生するのは私だけですか? (Samsung galaxy y duos; 2.3.7)