【发布时间】:2016-10-16 09:43:55
【问题描述】:
对于食谱应用程序,我有一个屏幕,其中包含用户保存的食谱标题的列表视图。当用户单击其中一个标题(列表视图项)时,它将打开一个新屏幕并显示完整的食谱。 我已将所有配方信息存储在 sqlite 数据库中。所以一行由标题、类别、图像路径和食谱文本组成。我似乎无法解决的是如何使用配方标题在新活动中显示相应的配方文本。
所以这是标题为 ListView 的活动的 onCreate:
// Analyze cursor object: Is there data available on the cursor object?
if (cursor!=null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
// Get information from the cursor object
String imagePath;
String titles;
titles = cursor.getString(0);
imagePath = cursor.getString(1);
// Get the titles and image paths from the database
RecipeDataProvider recipeDataProvider = new RecipeDataProvider(convertSrcToBitmap(imagePath), titles);
// Add them to the listview
adapter.add(recipeDataProvider);
}
while (cursor.moveToNext());
}
}
// Set onclicklistener for the recipe titles.
viewTitlesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the index of the title in the database
String index = ((TextView) findViewById(R.id.recipeTitle)).getText().toString();
// TODO get corresponding recipe text
Bundle dataBundle = new Bundle();
dataBundle.putString("id", index);
// Go to ShowRecipeActivity
Intent showIntent = new Intent(view.getContext(), ShowRecipeActivity.class);
showIntent.putExtras(dataBundle);
startActivity(showIntent);
}
});
}
这是我检索数据的活动。目前它只再次显示配方的标题:
// Get the recipe title from the previous activity
Bundle extras = getIntent().getExtras();
mRecipeTitle = extras.getString("id");
recipeTitleView.setText(mRecipeTitle);
// TODO: Get the corresponding recipe text from the database
这是我的 DatabaseHelper 中的游标方法:
public Cursor getRecipeInfo(SQLiteDatabase db){
// Create object of Cursor
Cursor cursor;
// Create some projections: the needed column names.
String[] projections = {RecipeContract.NewRecipeInfo.RECIPE_TITLE,
RecipeContract.NewRecipeInfo.RECIPE_PHOTO, RecipeContract.NewRecipeInfo.RECIPE_TEXT};
cursor = db.query(RecipeContract.NewRecipeInfo.TABLE_NAME, projections, null, null, null, null, null);
return cursor;
}
我在这个问题上纠结了一段时间,希望你能帮帮我!
【问题讨论】:
-
这样试试。单击列表视图项时,将其位置发送到下一个活动。根据收到的位置对第二个活动进行数据库查询。
标签: java android sqlite listview android-intent