インフレータを使用する次のリスト アダプターがあります。ここに色の変更を追加しようとしましたが、クリックされたアイテムだけでなく、すべてのアイテムが変更されます。
private class ListAdapter extends ArrayAdapter<jsonData>{
private List<jsonData> jList;
public ListAdapter(Context context, int resource,List<jsonData> jList) {
super(context, resource, jList);
this.jList = jList;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listviewact_layout, null);
v.setBackgroundResource(R.layout.list_selector);
}
jsonData jd = jList.get(position);
if (jd != null) {
TextView name = (TextView) v.findViewById(R.id.memberName);
TextView dateJoined = (TextView) v.findViewById(R.id.dateJoined);
if (name != null) {
name.setText("Member Name: " + jd.name);
}
if (dateJoined != null) {
dateJoined.setText("Joined: " + getNewDate(jd.joined));
}
}
return v;
}
アイテムの位置も取得できます。色以外のほとんどは正常に機能します。セレクター用のリソースファイルも追加してみましたが、同じ結果になります。
更新:これはうまくいくようです。アイテムをスクロールすると色がおかしくなりますが、グリッチがあります。
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(selectedItemPosition != position){
//Resets old item to original color
parent.getChildAt(selectedItemPosition).setBackgroundColor(Color.BLACK);
view.setBackgroundColor(Color.BLUE);
selectedItemPosition = position;
}
}