2011-12-05 34 views
1

對不起,在onclick事件期間發生錯誤。我想將選定的文本傳回給啓動活動。我在這個新這樣下去容易請:)將列表查看選定的文本傳遞給另一個活動

public class selectTee extends ListActivity{ 

    String[] tees_list; 
    String value = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     tees_list = getResources().getStringArray(R.array.tees_array); 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, tees_list)); 
     final ListView teelist = getListView(); 
     teelist.setChoiceMode(1); 
     teelist.setTextFilterEnabled(false); 
     teelist.setOnItemClickListener(new OnItemClickListener(){ 

      public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) { 

       //Error occurs during the onclick event 
       Intent data = new Intent(); 
       data.putExtra(value, selectedInt); 
       setResult(RESULT_OK, data); 
       finish(); 
      } 
     }); 
    } 
} 

好,我得到它的工作我改變了下面的代碼:

公共無效onActivityResult(INT requestCode,INT resultCode爲,意圖數據) {

  if (requestCode == request_Code) { 
      if (resultCode == RESULT_OK){ 
       Button revisedButton = (Button) findViewById(R.id.button1); 
       String btext = data.getData().toString(); 
       revisedButton.setText((CharSequence) btext); 

      } 

      } 
     } 

及選舉活動

public void onItemClick(AdapterView<?> teeAdapter, View arg1, int selectedInt, long selectedLong) { 
      String selection =((TextView) arg1).getText().toString(); 
      System.out.println(selection); 
      Intent data = new Intent(); 
      Uri uri = Uri.parse(selection); 
      System.out.println(uri); 
      data.setData(uri); 
      setResult(RESULT_OK, data); 
      finish(); 
     } 



    }); 
+0

有什麼錯誤。日誌會有所幫助。 – asenovm

+0

12-05 17:47:40.781:錯誤/ AndroidRuntime(1310):java.lang.RuntimeException:傳遞結果失敗ResultInfo {who = null,request = 1,result = -1,data = Intent {cmp = com.prophecysoftware .GolfProphecy/.SetupNewCourse(extras)}}給活動{com.prophecysoftware.GolfProphecy/com.prophecysoftware.GolfProphecy.SetupNewCourse}:android.content.resourcesResources $ NotFoundException:String資源ID#0xffffffff – Mashie

回答

0

它看起來像你從來沒有設置「價值」,所以它仍然是空的。我認爲這可能是你的問題。你可以在調用putExtra之前嘗試設置該字符串值嗎?

此外,從點擊的視圖中獲取選定的文本。做到以下幾點:

String textOfSelectedItem =((TextView) arg1).getText().toString(); 
1

使用

Intent intent = new Intent(getBaseContext(), YOUCLASS.class); 
intent.putExtra("TEXT", selectedInt); 
//startActivity(intent) if you want to start an activity when its clicked 
相關問題