繁体   English   中英

具有CursorAdapter的Recyclerview适配器

[英]Recyclerview adapter with CursorAdapter

我想实现一个适配器Recyclerview与使用CusrorAdapter如下作为解决方案的一个建议这里

我是Android的新手,我不太了解如何重写CursorAdapter的newView方法和bindView方法。 我也是猜我的适配器将有多个变量ViewHolder ,而不是一个(查看V1)因为有几个textViews在我的布局文件,但我只是不知道如何将它们中的代码。

public class MyRecyclerAdapter extends Adapter<MyRecyclerAdapter.ViewHolder {

// PATCH: Because RecyclerView.Adapter in its current form doesn't natively support
// cursors, we "wrap" a CursorAdapter that will do all teh job for us
CursorAdapter mCursorAdapter;
Context mContext;

public MyRecyclerAdapter(Context context, Cursor c) {
    mContext = context;
    mCursorAdapter = new CursorAdapter(mContext, c, 0) {

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // Inflate the view here
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Binding operations

        }
    };
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    View v1;
    public ViewHolder(View itemView) {
        super(itemView);
        v1 = itemView.findViewById(R.id.v1);
    }
}

@Override
public int getItemCount() {
    return mCursorAdapter.getCount();
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // Passing the binding operation to cursor loader
    mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Passing the inflater job to the cursor-adapter
    View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
        return new ViewHolder(v);
    }
}

尽管我不了解它的细节,但还是设法使它起作用。 基本上,我添加了ViewHolder变量作为类变量,并更改了代码的某些部分。 当您在名为row.xml的布局文件中有2个TextView项( namedate )时,解决方案如下row.xml

public class MyRecyclerAdapter extends Adapter<MyRecyclerAdapter.ViewHolder>{

    // PATCH: Because RecyclerView.Adapter in its current form doesn't natively support
    // cursors, we "wrap" a CursorAdapter that will do all teh job for us
    private CursorAdapter mCursorAdapter;
    private Context mContext;
    private ViewHolder holder;

    public MyRecyclerAdapter(Context context, Cursor c) {
        mContext = context;
        mCursorAdapter = new CursorAdapter(mContext, c, 0) {

            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                // Inflate the view here
                View v = LayoutInflater.from(context)
                    .inflate(R.layout.row_rv, parent, false);
                return v;
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                // Binding operations
                String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                String date = cursor.getString(cursor.getColumnIndexOrThrow("date"));

                holder.tvName.setText(name);
                holder.tvDate.setText(date);
            }
        };
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public TextView tvName;
        public TextView tvDate;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.name);
            tvDate = (TextView) itemView.findViewById(R.id.date);
        }
    }

    @Override
    public int getItemCount() {
        return mCursorAdapter.getCount();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // Passing the binding operation to cursor loader
        mcursorAdapter.getCursor().moveToPosition(position);
        mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Passing the inflater job to the cursor-adapter
        View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
        holder = new ViewHolder(v);
        return holder;
    }
}

如果有人知道它为什么起作用,请随时加入讨论。 谢谢。

而不是使用View创建ViewHolder,而应使用布局上具有的所有对象(所需的所有元素)创建ViewHolder。 必须在onCreateViewHolder方法中创建(填充)布局视图。

像这样:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView1;
        TextView textView2;

        public ViewHolder(View itemView) {
            super(itemView);
            textView1 = (TextView) itemView.findViewById(R.id.textView1);
            textView2 = (TextView) itemView.findViewById(R.id.textView2);
        }

        public void insertView(String result1, String result2) {
            textView1.setText(result1);
            textView2.setText(result2);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_name, parent, false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).insertView(results.get(position).getResult1(), results.get(position).getResult2());
    }

    @Override
    public int getItemCount() {
        return results.size();
    }
}

我认为这可以帮助您

您可以使用ORMSugar进行持久化。 不过,如果您可以访问适配器外部的数据库,那就更好了。 您将从数据库中获取必要的信息,并将该信息传递给适配器构造函数。

如果您需要一个例子,只需要告诉我即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM