簡體   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