RecyclerView 与游标 Adapter 的实现
Implementation of RecyclerView with cursor Adapter
如何实现带光标的RecyclerView。我试图实现 this code but this code uses some class to store each row but I did not want to make some class for the sake of storing each row of the cursor. And also I found this article ,它说我们在使用光标时不需要制作 class 。我很困惑使用哪种方法以及如何使用它。我试图实现 Whosebug 上存在的大部分代码,但它们几乎都是一样的。
我也用这个尝试了我的代码:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
private Cursor cursor;
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView title,text;
CardView cd;
LinearLayout ll;
public MyViewHolder(View v){
super(v);
title = v.findViewById(R.id.childTitleView);
text = v.findViewById(R.id.childTextView);
cd = v.findViewById(R.id.parentCardView);
ll = v.findViewById(R.id.ll);
}
}
public RecyclerViewAdapter(Cursor c){
this.cursor = c;
}
@Override
public int getItemCount() {
return cursor.getCount();
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
cursor.moveToNext();
String t = cursor.getString(cursor.getColumnIndex(RecordContract.RecordEntry.COLUMN_TITLE));
String d = cursor.getString(cursor.getColumnIndex(RecordContract.RecordEntry.COLUMN_TEXT));
holder.title.setText(t);
holder.text.setText(d);
holder.cd.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(view.getContext(), ""+position, Toast.LENGTH_SHORT).show();
//holder.ll.setBackgroundColor(Color.parseColor("#000"));
holder.cd.setBackgroundColor(Color.parseColor(view.getResources().getString(R.color.blueGreen)));
return true;
}
});
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_child,parent,false);
return new MyViewHolder(itemView);
}
}
这是我的 MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mdb = new RecordDbHelper(getApplicationContext());
RecyclerView rv = findViewById(R.id.recylerViewId);
rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
rv.setItemAnimator(new DefaultItemAnimator());
Cursor c = getCursor();
RecyclerViewAdapter rva = new RecyclerViewAdapter(c);
rv.setAdapter(rva);
}
public Cursor getCursor(){
SQLiteDatabase db = mdb.getReadableDatabase();
String[] projection = {
RecordContract.RecordEntry._ID, RecordContract.RecordEntry.COLUMN_TEXT,
RecordContract.RecordEntry.COLUMN_TITLE};
String selection = null;
String[] selectionArgs = null;
String tableName = RecordContract.RecordEntry.TABLE_NAME;
Cursor c = db.query(tableName,projection,selection,selectionArgs,null,null,null);
return c;
}
好吧,这会在 RecyclerView 中显示文本但是当将新数据添加到数据库时,应用程序需要重新启动。它不会自行刷新。
Well this show the text in the RecyclerView
它不会可靠地这样做。在 onBindViewHolder()
中,将 moveToNext()
替换为 moveToPosition(position)
。现在,您忽略了 position
参数,这意味着一旦您开始滚动(特别是向后滚动),您就会 运行 遇到问题。
But when a new data is added to the database then the app requires to start again. It does not refresh itself.
这与 CursorAdapter
与 AdapterView
的工作方式没有什么不同,例如 ListView
。
当你更新数据库时,你需要得到一个新的Cursor
并交给你的RecyclerViewAdapter
。您的 RecyclerViewAdapter
然后可以调用 notifyDataSetChanged()
告诉 RecyclerView
重绘自己。
如何实现带光标的RecyclerView。我试图实现 this code but this code uses some class to store each row but I did not want to make some class for the sake of storing each row of the cursor. And also I found this article ,它说我们在使用光标时不需要制作 class 。我很困惑使用哪种方法以及如何使用它。我试图实现 Whosebug 上存在的大部分代码,但它们几乎都是一样的。
我也用这个尝试了我的代码:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
private Cursor cursor;
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView title,text;
CardView cd;
LinearLayout ll;
public MyViewHolder(View v){
super(v);
title = v.findViewById(R.id.childTitleView);
text = v.findViewById(R.id.childTextView);
cd = v.findViewById(R.id.parentCardView);
ll = v.findViewById(R.id.ll);
}
}
public RecyclerViewAdapter(Cursor c){
this.cursor = c;
}
@Override
public int getItemCount() {
return cursor.getCount();
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
cursor.moveToNext();
String t = cursor.getString(cursor.getColumnIndex(RecordContract.RecordEntry.COLUMN_TITLE));
String d = cursor.getString(cursor.getColumnIndex(RecordContract.RecordEntry.COLUMN_TEXT));
holder.title.setText(t);
holder.text.setText(d);
holder.cd.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(view.getContext(), ""+position, Toast.LENGTH_SHORT).show();
//holder.ll.setBackgroundColor(Color.parseColor("#000"));
holder.cd.setBackgroundColor(Color.parseColor(view.getResources().getString(R.color.blueGreen)));
return true;
}
});
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_child,parent,false);
return new MyViewHolder(itemView);
}
}
这是我的 MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mdb = new RecordDbHelper(getApplicationContext());
RecyclerView rv = findViewById(R.id.recylerViewId);
rv.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
rv.setItemAnimator(new DefaultItemAnimator());
Cursor c = getCursor();
RecyclerViewAdapter rva = new RecyclerViewAdapter(c);
rv.setAdapter(rva);
}
public Cursor getCursor(){
SQLiteDatabase db = mdb.getReadableDatabase();
String[] projection = {
RecordContract.RecordEntry._ID, RecordContract.RecordEntry.COLUMN_TEXT,
RecordContract.RecordEntry.COLUMN_TITLE};
String selection = null;
String[] selectionArgs = null;
String tableName = RecordContract.RecordEntry.TABLE_NAME;
Cursor c = db.query(tableName,projection,selection,selectionArgs,null,null,null);
return c;
}
好吧,这会在 RecyclerView 中显示文本但是当将新数据添加到数据库时,应用程序需要重新启动。它不会自行刷新。
Well this show the text in the RecyclerView
它不会可靠地这样做。在 onBindViewHolder()
中,将 moveToNext()
替换为 moveToPosition(position)
。现在,您忽略了 position
参数,这意味着一旦您开始滚动(特别是向后滚动),您就会 运行 遇到问题。
But when a new data is added to the database then the app requires to start again. It does not refresh itself.
这与 CursorAdapter
与 AdapterView
的工作方式没有什么不同,例如 ListView
。
当你更新数据库时,你需要得到一个新的Cursor
并交给你的RecyclerViewAdapter
。您的 RecyclerViewAdapter
然后可以调用 notifyDataSetChanged()
告诉 RecyclerView
重绘自己。