创建自定义简单光标适配器

时间:2012-03-13 19:08:26

标签: android android-listview simplecursoradapter android-cursoradapter android-listfragment

我想创建一个非常简单的游标自定义游标适配器,以便于在点击时更改行项目的颜色。使用以下代码

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的警告,建议者已被弃用。

1 个答案:

答案 0 :(得分:18)

你应该能够这样做:

class YourListFragment extends ListFragmentOrSomethingElse {
    private AreaCursorAdapter mAdapter;

    @Override    
    public void onCreate() {
        mAdapter = new AreaCursorAdapter(this, null);
        setListAdapter(mAdapter);
    }

    @Override
    public void onListItemClick(ListView parent, View v, int position, long id) { 
        mAdapter.setSelectedPosition(position);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.swapCursor(cursor);
        // should reset that here maybe
        mAdapter.setSelectedPosition(-1);
    }
}

public class AreaCursorAdapter extends CursorAdapter {
    private Context context;
    private int mSelectedPosition;
    LayoutInflater mInflater;

    public AreaCursorAdapter(Context context, Cursor c) {
        // that constructor should be used with loaders.
        super(context, c, 0);
        mInflater = LayoutInflater.from(context);
    }

    public void setSelectedPosition(int position) {
        mSelectedPosition = position;
        // something has changed.
        notifyDataSetChanged();
    }

    @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)));
        int position = cursor.getPosition(); // that should be the same position
        if (mSelectedPosition == position) {
           view.setBackgroundColor(Color.RED);
        } else {
           view.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        // edit: no need to call bindView here. That's done automatically
        return v;
    }

}