带有 CursorAdapter 的 Recyclerview 适配器
Recyclerview adapter with CursorAdapter
我正在尝试使用 CusrorAdapter
实现 Recyclerview
的适配器,如下面的解决方案之一 here 中所建议的那样。
我是 Android 的新手,我不太清楚应该如何覆盖 CursorAdapter
的 newView 方法和 bindView
方法。此外,我猜测我的适配器将在 ViewHolder
中有多个变量而不是一个(View 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,而应使用布局上的所有对象(您需要的所有元素)创建 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 进行持久化。不过,如果您有可能在适配器外部访问数据库,那就更好了。您将从数据库中获取必要的信息并将该信息传递给适配器构造函数。
如果您需要示例,请告诉我。
尽管我不了解它的详细工作原理,但我还是设法让它工作了。基本上我添加了一个 ViewHolder
变量作为 class 变量并更改了代码的某些部分。当布局文件中有 2 个 TextView
项(name
和 date
)时,解决方案看起来像这样 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;
}
}
如果有人知道它为什么有效,请随时加入讨论。谢谢。
我正在尝试使用 CusrorAdapter
实现 Recyclerview
的适配器,如下面的解决方案之一 here 中所建议的那样。
我是 Android 的新手,我不太清楚应该如何覆盖 CursorAdapter
的 newView 方法和 bindView
方法。此外,我猜测我的适配器将在 ViewHolder
中有多个变量而不是一个(View 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,而应使用布局上的所有对象(您需要的所有元素)创建 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 进行持久化。不过,如果您有可能在适配器外部访问数据库,那就更好了。您将从数据库中获取必要的信息并将该信息传递给适配器构造函数。
如果您需要示例,请告诉我。
尽管我不了解它的详细工作原理,但我还是设法让它工作了。基本上我添加了一个 ViewHolder
变量作为 class 变量并更改了代码的某些部分。当布局文件中有 2 个 TextView
项(name
和 date
)时,解决方案看起来像这样 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;
}
}
如果有人知道它为什么有效,请随时加入讨论。谢谢。