如何在使用游标适配器的 RecyclerView 中制作多张卡片?
How to make more than one card in RecyclerView that uses cursor adapter?
我在 RecyclerView 中使用网格布局,其中每张卡片使用游标适配器和游标加载器显示来自 SQLite 数据库的一些信息。我使用 library 来实现这一点。
问题是所有卡片都没有显示。当我第一次 运行 应用程序时,只显示一张卡片,显示数据库最后一行的数据。但我希望它显示第一行的数据。如果我关闭并再次 运行 应用程序,则会显示两张卡片,但两张卡片具有相同的数据,即再次来自数据库的最后一行。我希望第二张卡片显示第二行的数据。第三次,出现三张牌,模式继续。
请帮我每次从数据库中的相应行中获取数据时显示所需的确切卡片数量。
MainActivity.java
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
ContentResolver contentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentResolver = getContentResolver();
insertRecipes();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new RecyclerViewAdapter(getBaseContext());
Log.v("MainActivity", "instantiating adapter");
recyclerView.setAdapter(adapter);
Log.v("MainActivity", "setting adapter");
recyclerView.setLayoutManager(new GridLayoutManager(getBaseContext(), 2));
Log.v("MainActivity", "setting layout manager for recycler view");
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
protected void onStart() {
super.onStart();
}
private void insertRecipes()
{
ContentValues values = new ContentValues();
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 1");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 2");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 3");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 4");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 5");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
Uri returnedUri = contentResolver.insert(AppytizerEntry.CONTENT_URI, values);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {
AppytizerEntry._ID,
AppytizerEntry.COLUMN_APPYTIZER_NAME
};
Uri CONTENT_URI = AppytizerEntry.CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader(this, CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerViewCursorAdapter<RecyclerViewAdapter.MyViewHolder> {
public RecyclerViewAdapter(Context context)
{
super(context);
setupCursorAdapter(null, 0, R.layout.custom_row, false);
Log.v("Adapter", "RecyclerViewAdapter constructor");
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.v("Adapter", "onCreateViewHolder");
return new MyViewHolder(mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent));
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.v("Adapter", "onBindViewHolder");
mCursorAdapter.getCursor().moveToFirst();
Log.v("Adapter", "moving to position " + position);
setViewHolder(holder);
mCursorAdapter.bindView(null, mContext, mCursorAdapter.getCursor());
}
public class MyViewHolder extends RecyclerViewCursorViewHolder {
public final TextView title;
public MyViewHolder(View view)
{
super(view);
Log.v("Adapter", "MyViewHolder constructor");
title = (TextView) view.findViewById(R.id.title);
}
int i = 0;
@Override
public void bindCursor(Cursor cursor) {
int titleColumnIndex = cursor.getColumnIndex(AppytizerContract.AppytizerEntry.COLUMN_APPYTIZER_NAME);
title.setText(cursor.getString(titleColumnIndex));
if(i<4)
{
cursor.moveToNext();
i++;
}
Log.v("Adapter", "bindCursor");
}
}
}
我对ContentValues不是很熟悉,但是你不是应该在每个测试记录之间有一个database.insert(TABLE_NAME, null, contentValues);
吗?
Code example for ContentValues
这可以解释您第一次只看到最后一条记录,第二次看到重复的 运行 应用程序。
更好的方法是使用 ContentResolver.bulkInsert。
我在 RecyclerView 中使用网格布局,其中每张卡片使用游标适配器和游标加载器显示来自 SQLite 数据库的一些信息。我使用 library 来实现这一点。
问题是所有卡片都没有显示。当我第一次 运行 应用程序时,只显示一张卡片,显示数据库最后一行的数据。但我希望它显示第一行的数据。如果我关闭并再次 运行 应用程序,则会显示两张卡片,但两张卡片具有相同的数据,即再次来自数据库的最后一行。我希望第二张卡片显示第二行的数据。第三次,出现三张牌,模式继续。
请帮我每次从数据库中的相应行中获取数据时显示所需的确切卡片数量。
MainActivity.java
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
ContentResolver contentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentResolver = getContentResolver();
insertRecipes();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new RecyclerViewAdapter(getBaseContext());
Log.v("MainActivity", "instantiating adapter");
recyclerView.setAdapter(adapter);
Log.v("MainActivity", "setting adapter");
recyclerView.setLayoutManager(new GridLayoutManager(getBaseContext(), 2));
Log.v("MainActivity", "setting layout manager for recycler view");
getSupportLoaderManager().initLoader(0, null, this);
}
@Override
protected void onStart() {
super.onStart();
}
private void insertRecipes()
{
ContentValues values = new ContentValues();
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 1");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 2");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 3");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 4");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
values.put(AppytizerEntry.COLUMN_APPYTIZER_NAME, "test name 5");
values.put(AppytizerEntry.COLUMN_APPYTIZER_INGREDIENTS, "chips, salt, pepper");
values.put(AppytizerEntry.COLUMN_APPYTIZER_DIRECTIONS, "mix chips, salt and pepper. eat it");
values.put(AppytizerEntry.COLUMN_APPYTIZER_TOFDISH, AppytizerEntry.TOFDISH_VEGETARIAN);
values.put(AppytizerEntry.COLUMN_APPYTIZER_COURSE, AppytizerEntry.COURSE_SNACKS);
values.put(AppytizerEntry.COLUMN_APPYTIZER_TIME, 2);
values.put(AppytizerEntry.COLUMN_APPYTIZER_CALORIES, 100);
//values.put(AppytizerEntry.COLUMN_APPYTIZER_IMAGE, );
Uri returnedUri = contentResolver.insert(AppytizerEntry.CONTENT_URI, values);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {
AppytizerEntry._ID,
AppytizerEntry.COLUMN_APPYTIZER_NAME
};
Uri CONTENT_URI = AppytizerEntry.CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader(this, CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerViewCursorAdapter<RecyclerViewAdapter.MyViewHolder> {
public RecyclerViewAdapter(Context context)
{
super(context);
setupCursorAdapter(null, 0, R.layout.custom_row, false);
Log.v("Adapter", "RecyclerViewAdapter constructor");
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.v("Adapter", "onCreateViewHolder");
return new MyViewHolder(mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent));
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Log.v("Adapter", "onBindViewHolder");
mCursorAdapter.getCursor().moveToFirst();
Log.v("Adapter", "moving to position " + position);
setViewHolder(holder);
mCursorAdapter.bindView(null, mContext, mCursorAdapter.getCursor());
}
public class MyViewHolder extends RecyclerViewCursorViewHolder {
public final TextView title;
public MyViewHolder(View view)
{
super(view);
Log.v("Adapter", "MyViewHolder constructor");
title = (TextView) view.findViewById(R.id.title);
}
int i = 0;
@Override
public void bindCursor(Cursor cursor) {
int titleColumnIndex = cursor.getColumnIndex(AppytizerContract.AppytizerEntry.COLUMN_APPYTIZER_NAME);
title.setText(cursor.getString(titleColumnIndex));
if(i<4)
{
cursor.moveToNext();
i++;
}
Log.v("Adapter", "bindCursor");
}
}
}
我对ContentValues不是很熟悉,但是你不是应该在每个测试记录之间有一个database.insert(TABLE_NAME, null, contentValues);
吗?
Code example for ContentValues
这可以解释您第一次只看到最后一条记录,第二次看到重复的 运行 应用程序。
更好的方法是使用 ContentResolver.bulkInsert。