クリック時に行アイテムの色を簡単に変更できるように、非常に単純なカーソルカスタムカーソルアダプターを作成したいと思います。次のコードを使用する
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
このスレッドからコードを取得しましたhttps://stackoverflow.com/a/7649880/498449
単純なカーソルアダプターを使用してコードをonClickに配置しましたが、ListFragmentのデフォルトのリストはビューを再利用するため、スクロールすると複数のビューが強調表示されます。IRCについて話すと、カスタムカーソルアダプターを作成することが提案されました。ただし、これを行う方法と、上記のコードスニペットがどこに収まるかについてのベストプラクティスを見つけることができないようです。助けていただければ幸いです。
public class AreaCursorAdapter extends CursorAdapter {
private Context context;
public AreaCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView list_item = (TextView)view.findViewById(android.R.id.text1);
list_item.setText(cursor.getString(cursor.getColumnIndex(INDICATOR_NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
bindView(v, context, cursor);
return v;
}
}
オンラインで見つけたコードでカーソルアダプタを更新しました。しかし、私には2つの問題があります。1.カーソルローダーを使用しているので、コンストラクターに渡す「カーソル」オブジェクトがありません。2.コンストラクターが非推奨になったことを示す警告がEclipseから表示されます。