使用游标适配器和加载器时动态编辑列表视图项

时间:2014-07-25 12:44:00

标签: android android-listview android-cursoradapter android-cursorloader

我正在编写一个小小的消息传递应用程序,而且我最近遇到了这个问题。

我有一个ListFragment子类,它实现了CursorLoader回调。内置ListView绑定到SimpleCursorAdapter。此列表中的每个行视图都有一个可绘制的级别作为背景。我想根据数据库信息(是否发送消息)在级别之间切换。

我的问题是在这种情况下如何更改单行背景?我是否必须通过适配器访问它?我很确定这应该在onLoadFinished方法中完成。但是这段代码给了我NullPointerException。如果你能指出我正确的方向,我将非常感激。

public class MessagesFragment extends ListFragment implements 
                              LoaderManager.LoaderCallbacks<Cursor>{

private static MessagesFragment fragment;
private static MainActivity activity;
private static SimpleCursorAdapter adapter;

public static MessagesFragment getInstance() {
    if (fragment == null)
        fragment = new MessagesFragment();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    activity = (MainActivity) getActivity();

    adapter = new SimpleCursorAdapter(activity, R.layout.list_item, null,
            new String[] {DataProvider.KEY_NUMBER, DataProvider.KEY_MESSAGE, DataProvider.KEY_TIME},
            new int [] {R.id.item_field_number, R.id.item_field_message, R.id.item_field_time}, 0);
    setListAdapter(adapter);

    getLoaderManager().initLoader(0, null, this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_messages, null);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    activity.showDetails(Long.valueOf(id).intValue());
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new CursorLoader(activity, DataProvider.CONTENT_URI, null, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    adapter.swapCursor(data);

    while (data.moveToNext()) {
        int id = data.getInt(data.getColumnIndex(DataProvider.KEY_ID));
        int sent = data.getInt(data.getColumnIndex(DataProvider.KEY_SENT));

        this.getListView().getChildAt(id).getBackground().setLevel(sent);
    }
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    adapter.swapCursor(null);
}

}

2 个答案:

答案 0 :(得分:2)

这应该有帮助!

public class SampleAdapter extends SimpleCursorAdapter {

    private LayoutInflater inflater;
    private Cursor cursor;
    private Context context;

    public SampleAdapter(Context context, int layout, Cursor c, String[] from, int[]            to, int flags) {
        super(context, layout, c, from, to, flags);
        this.context = context;
        cursor = c;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return cursor == null ? 0 : cursor.getCount();
    }

    @Override
    public Object getItem(int position) {
       return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (cursor != null) {
            ViewHolder holder;
            cursor.moveToPosition(position);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            convertView.getBackground().setLevel(cursor.getInt(cursor.getColumnIndex(DataProvider.KEY_SENT)));
            holder.number.setText(cursor.getString(cursor.getColumnIndex(DataProvider.KEY_NUMBER)));
            holder.message.setText(cursor.getColumnIndex(DataProvider.KEY_MESSAGE)));
            holder.time.setText(cursor.getColumnIndex(DataProvider.KEY_TIME)));
        }

        return convertView;
    }

    @Override
    public Cursor swapCursor(Cursor c) {
        cursor = c;
        return super.swapCursor(c);
    }

    private class ViewHolder {

        TextView number;
        TextView message;
        TextView time;

        public ViewHolder(View view) {
            number = (TextView) view.findViewById(R.id.item_field_number);
            message = (TextView) view.findViewById(R.id.item_field_message);
            time = (TextView) view.findViewById(R.id.item_countries_time);
        }
    }
}

答案 1 :(得分:0)

事实证明我应该实现SimpleCursorAdapter而不是使用回调方法。我要感谢@bbaker :)如果有人对此感兴趣,请参阅以下代码:

private class CustomAdapter extends SimpleCursorAdapter {

    private LayoutInflater inflater;
    private int layout;

    public CustomAdapter(Context context, int layout, Cursor cursor,
            String[] from, int[] to, int flags) {
        super(context, layout, cursor, from, to, flags);
        inflater = LayoutInflater.from(context);
        this.layout = layout;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return inflater.inflate(layout, null);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        if (cursor != null && cursor.getCount() > 0) {
            TextView timeView = (TextView) view.findViewById(R.id.item_field_time);
            int sent = cursor.getInt(cursor.getColumnIndex(DataProvider.KEY_SENT));
            timeView.getBackground().setLevel(sent);
        }
    }
}