gpt4 book ai didi

android - 带有 CursorAdapter 的 Recyclerview 适配器

转载 作者:行者123 更新时间:2023-11-30 01:48:17 24 4
gpt4 key购买 nike

我正在尝试使用 CusrorAdapterRecyclerview 实现一个适配器,如下面的解决方案之一所建议的那样 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 变量作为类变量并更改了部分代码。当您在名为 row.xml< 的布局文件中有 2 个 TextView 项(namedate)时,解决方案看起来像这样:

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;
}
}

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

关于android - 带有 CursorAdapter 的 Recyclerview 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com