【问题标题】:Android Insert new row in between cursor adapterAndroid 在光标适配器之间插入新行
【发布时间】:2015-02-08 17:18:26
【问题描述】:

我有 2 个光标,光标 1 和光标 2。光标适配器已用于显示光标 1 的结果。但是,我想为每 5 个光标 2 项目插入光标 2 的项目。我怎样才能做到这一点?

目前我已经为光标 1 的列表视图扩展了一个 CursorAdapter。现在我想为每 5 个光标 1 的项目将光标 2 项目插入光标 1。

我查看了 MatrixCursor 和 MergeCursor,它们会合并并添加行,但我想在两者之间插入。有什么想法我们可以如何做到这一点?

【问题讨论】:

  • 我想你的意思是你想要来自 cursor2 的 1 个项目来自 cursor1 的 5 个项目。是这样吗?
  • 是的,对于来自光标 1 的每 5 个项目,我想从光标 2 插入 1 个项目。

标签: android sqlite


【解决方案1】:

从您的解释中很难理解您想要实现的目标。 您应该扩展 BaseAdapter 类,因为它更容易适应您的需求。 这应该适合你。

public class CursorAdapterTwo extends BaseAdapter{

    private final Context context;
    LayoutInflater layoutInflater;
    String[] dataTypes;
    Cursor cursor1;
    Cursor cursor2;


    public CursorAdapterTwo(Context context, Cursor cursor1, Cursor cursor2) {
        this.context = context;
        this.cursor1 = cursor1;
        this.cursor2 = cursor2;
        layoutInflater = LayoutInflater.from(context);
    }

    static class ViewHolder {
        TextView textView;
    }

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

        cursor1.moveToPosition(position);

        if((position % 5) == 0)
        {
            cursor2.moveToPosition(position / 5);
        }

        if (convertView == null) {
            convertView = layoutInflater.inflate(layout.list_item, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) convertView.findViewById(id.text_view);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        String columnType = dataTypes[position];

        viewHolder.textView.setText("New text");

        return convertView;
    }

    @Override
    public int getCount() {

        if (cursor1 != null) {
            return cursor1.getCount() + cursor2.getCount();
        } else {
            return 0;
        }

    }

    @Override
    public Object getItem(int position) {
        if (cursor1 != null) {
            cursor1.moveToPosition(position);
            return cursor1;
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

}

【讨论】:

  • getCount 不是光标 1 和 2 的和吗?或者 cursor1.count + (cursor1.count / 5) ?
猜你喜欢
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多