2013-02-06 121 views
1

進出口新到Android ..如何將選定的值傳遞給另一個活動?

林意圖面臨的問題..

我的問題是...我想從一個活動的字符串數組列表選擇的值傳遞用戶到另一個活動...值是通過JSON從數據庫中處理的字符串。和值存儲在一個ArrayList中..

現在我需要從一個活動傳遞值使用意圖another..by ..

 lvForDialog = (ListView) viewList.findViewById(R.id.List_view); 
     ArrayAdapter<String> adapter = (new ArrayAdapter<String>(Nexttopic.this, R.layout.row_topic, R.id.child_row,tnamelist)); 
     lvForDialog.setAdapter(adapter);   
     lvForDialog.setOnItemClickListener(new OnItemClickListener() 
    { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position,long id)   
      {    
     Intent intent = new Intent(Nexttopic.this,Question.class);  
    intent.putExtra(TAG_TOPICNAME, tname); 

我想通過TAG_TOPICNAME到另一個活動..哪個主題名稱用戶選擇我想通過的名稱...

如何做到這一點?

非常感謝..

+0

你一定要看到這個鏈接: - duggu

+0

@hcd thats for button ...我需要onItemclickListener ... –

+0

感謝all..for回答我的問題..並指導我... –

回答

3

試試這個:

i.putExtra("Name", tname); 
startActivity(i); 

在你的第一個活動,然後你可以在下面你的第二個活動添加代碼,並讓您的數據。

Intent intent = getIntent(); 
String Name = intent.getExtras().getString("Name"); 

編輯:用這個作爲一個例子來獲得所選的項目,onitemclick監聽

@Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) 
{ 
    Cursor GettName = (Cursor)viewList.getItemAtPosition(position);//Get a Cursor from the selected position to access the selected Item 
    String tname = GettName.getString(GettName.getColumnIndex(CustomerDBAdapter.KEY_TNAME)); // get approporiate String from that cursor 
    i.putExtra("Name", tname); 
    startActivity(i); 
} 
+0

我想通過用戶選擇的值.. –

+0

什麼用戶選擇該值應該在列表視圖中通過 –

+0

例如在用戶點擊列表項時應該選擇帶有實現onitemclick監聽器的selecteditem,然後您可以使用它傳遞給第二個Activity.it,在其他實現中不會影響,這裏tname是一個字符串,你只需要用用戶選擇來填充它cted項 –

1

你應該叫startActivity(learnintent);這是缺少在你的代碼後,下面的線

Intent learnintent = new Intent(Nexttopic.this,Question.class);  
learnintent.putExtra(TAG_TOPICNAME, tname); 

我希望你在使用它之前還定義了常量TAG_TOPICNAME

然後在Question活動做類似下面的訪問值。

String topicName = intent.getIntent().getStringExtra(TAG_TOPICNAME); 

這裏TAG_TOPICNAME應該是相同的值,我分配在以前的活動,更好地定義一個class Constants,並把這個字符串常量出現,並在兩地使用它。

+0

不,我沒有得到..我想要什麼用戶選擇的價值,我應該通過...請指導我 –

0

做到這將是會話ID傳遞給在意圖signout活動,您正在使用啓動活動的最簡單方法:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class); 
intent.putExtra("EXTRA_SESSION_ID", sessionId); 
startActivity(intent) 

docs的意圖有更多的信息(看標題爲「額外」的部分)。

+0

我想獲得用戶選擇值... onItemclickListener ... –

0

可以通過使用一個bundles.You可以捆綁它並與intent.A示例代碼傳遞給它下面示出通過值....

束=新軟件包(); data1 = Double.valueOf(myEditText.getText()。的toString());得到它形成的意圖

Bundle b = getIntent().getExtras(); 
     noqs = b.getDouble("data1"); 
     mTvCat.setText("hhhhhh"+point+noqs); 

bundle.putDouble("data1", data1); 

    Intent intent = new Intent(this, AnotherActivity.class); 
    intent.putExtras(bundle); 
    startActivity(intent); 
    finish(); 

例子也如果你那麼好,你可以將它存儲在一個靜態變量。您可以從應用程序的任何位置訪問它。

+0

我想從onItemclickListener獲得用戶選擇的值... –

+0

值的類型是什麼?您可以將它存儲在一個常數值中。我認爲這會有所幫助,因爲如果你將它存儲在一個常量值中,你不需要通過bundle來傳遞它,你也可以使用類名直接從其他活動中調用它。 –

相關問題