我在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");
}
}
}
答案 0 :(得分:3)
我对ContentValues不太熟悉,但是每个测试记录之间不应该有database.insert(TABLE_NAME, null, contentValues);
吗?
Code example for ContentValues
这可以解释为您第一次只看到最后一条记录,第二次运行应用程序时看到重复记录。
答案 1 :(得分:0)
更好的方法是使用ContentResolver.bulkInsert。