将sqlite行转移到新活动

时间:2016-06-15 10:22:53

标签: java android sqlite listview android-intent

对于食谱应用程序,我有一个屏幕,其中包含用户已保存的食谱标题的列表视图。当用户点击其中一个标题(列表视图项目)时,它将打开一个新屏幕并显示完整的配方。 我已将所有配方信息存储在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;
}

我一直坚持这个问题一段时间,我希望你能帮助我!

1 个答案:

答案 0 :(得分:0)

您应该创建另一个活动来实现此目的。点击食谱列表后,:会带您进入下一个SELECT * FROM tbl ORDER BY col1, col2 ASC/DESC; 你必须通过onclick listener

activity