【问题标题】:Transfer sqlite row to new activity将 sqlite 行转移到新活动
【发布时间】: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


【解决方案1】:

您应该创建另一个活动来实现这一点。一旦你点击食谱列表,你的onclick listener 应该带你到下一个activity 你必须通过recipe id

@Override
public void onClick(View v) {
     Intent intent= new Intent(this, SecondActivity.class);
     intent.putExtra("recipeID", "0000002");
}

【讨论】:

  • 我有一个新活动,但在传递配方 ID 时遇到了麻烦。我还找到了一些关于使用 Hashmap 的信息,也许这对我有用?
  • 你传递的数据中有键值对吗?
  • 我正在传递食谱标题,它与食谱文本在同一行。
  • 但我不知道如何将这个食谱标题用作键值对。
  • 您的目标是在下方显示标题和文字,对吗?如果是,为什么不从单行中单独获取所需的列并呈现它们
猜你喜欢
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多