我想创建一个非常简单的光标自定义光标适配器,以方便在单击时更改行项的颜色。使用以下代码
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;
}
}
我已经用我在网上找到的一些代码更新了光标适配器。但是,我有两个问题。1. 我正在使用游标加载器,所以我没有“游标”对象传递给构造函数。2. 我收到来自 Eclipse 的警告,构造函数已被弃用。