Bundle.putInt(),它是如何工作的?

时间:2015-04-17 14:56:44

标签: android

我还在教程中学习android和我

 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
         // TODO Auto-generated method stub
         int id_To_Search = arg2 + 1;
         Bundle dataBundle = new Bundle();
         dataBundle.putInt("id", id_To_Search);
         Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
         intent.putExtras(dataBundle);
         startActivity(intent);
     }
     });

我的问题在这里,我不明白这是如何工作的

dataBundle.putInt("id", id_To_Search);

如果id_To_Search = 0则id = 0?和id引用列名?

android网站解释

 putInt(String key, int value)
Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key. 

据我所知,如果int = 5那么key将是5? 那么'Id'会是= 5?

2 个答案:

答案 0 :(得分:2)

1)您可以与其他活动分享int

String YOUR_KEY = "id";
Intent intent = new Intent(getApplicationContext(), DisplayContact.class);
intent.putExtra(YOUR_KEY, id); 
startActivity(intent);

Bundle dataBundle = new Bundle();
dataBundle.putInt(YOUR_KEY, id);
intent.putExtras(dataBundle);

2)并在您的活动中获得int

int defaultValue = 0;
int id = getIntent().getIntExtra(YOUR_KEY, defaultValue);

Bundle extras = getIntent().getExtras(); 
int id = extras.getInt(YOUR_KEY);

答案 1 :(得分:1)

你所说的那么简单

  

据我所知,如果int = 5那么key将是5?然后&#39; Id&#39;它将是= 5?

所以,当你输入另一个活动时,这里

 Bundle dataBundle = new Bundle();
     dataBundle.putInt("id", id_To_Search);
     Intent intent = new Intent(getApplicationContext(),com.example.addressbook.DisplayContact.class);
     intent.putExtras(dataBundle);
     startActivity(intent);

你可以int id = getIntent().getIntExtra("id");,并且将是5

相关问题