仅替换片段而不传递数据

时间:2015-05-20 16:30:48

标签: java android android-intent android-fragments android-activity

我试图让我的第二个列表片段打开,但是下面有不完整的代码,它认为我想从DummyContent传递数据以显示'项目x'当我真的不喜欢的时候。需要在if和/或else语句中进行更改以便列表片段打开而不是'项目x'出现?我知道ARG_ITEM_ID是罪魁祸首,但我不知道该怎么改变它。

@Override
public void onItemSelected(String id) {
    if("1".equals(id)){
        if (mTwoPane) {
            Bundle arguments = new Bundle();
            arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
            ContinentsListFragment fragment = new ContinentsListFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.item_list, fragment)
                    .commit();

        } else {
            Intent detailIntent = new Intent(this, ContinentsListActivity.class);
            detailIntent.putExtra(ContinentsListFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

这似乎太容易但让我们开始理解这个问题。 示例代码修复:

console.log("Message here");

注意:

  • 删除了带有putExtra的代码。
  • 如果您坚持调用putExtra,请从技术角度对ContinentsListFragment.ARG_ITEM_ID 静态进行访问或使用任何可访问的常量。
相关问题