1

我试图让我的第二个列表片段打开,但下面的代码不完整,它认为我想从DummyContent传递数据来显示'Item x',当我实际上不'吨。在if和/或else语句中需要改变什么,以便列表片段打开而不是出现'Item 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

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

Intent detailIntent = new Intent(this, ContinentsListActivity.class); 
startActivity(detailIntent); 

注:

与putExtra
  • 代码被删除。
  • 如果您坚持要求putExtra,请使用ContinentsListFragment.ARG_ITEM_ID 静态从技术上访问或使用任何可访问的常量。
+1

我的确记住了这一点,但想要仔细检查 – MacaronLover