2017-10-13 117 views
-1

我想使一個文本視圖顯示我選擇的項目,但我試圖實現的代碼沒有工作。我也想弄清楚如何顯示最終選項 - 有一個主要列表,並且在從主列表中選擇一個項目後,它將帶您到另一個列表,您可以在其中進行最終選擇(Main2activity和main3activity是具有最終列表選擇的活動的名稱)。這裏是我的代碼:顯示從列表視圖到文本視圖選擇的項目

public class MainActivity extends AppCompatActivity { 

    String items[] = new String[]{ 
"Apple", "Orange", "Pear" 
}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent myintent = getIntent(); 
     String value = myintent.getStringExtra("myintent"); 
     TextView textview = (TextView)findViewById(R.id.textView2); 
     textview.setText(value); 



     ListView listView = (ListView) findViewById(languageselector); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); 
     listView.setAdapter(adapter); 
     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 


      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

       if (position==0){ 
       Intent myintent = new Intent(view.getContext(), Main2Activity.class); 
       startActivityForResult(myintent, 0); 
      } 
       if (position==1){ 
        Intent myintent = new Intent(view.getContext(), Main3Activity.class); 
        startActivityForResult(myintent, 1); 
       } 



      } 
      }); 
}} 
+0

你得到任何錯誤? –

+0

爲什麼你用「startActivityForResult」?是你通過適配器的位置與「startActivityForResult(myintent,1);」這個代碼? –

+0

您只顯示您的MainActivity,我認爲這是您啓動應用程序時啓動的Activity。那麼你期望從這段代碼中'value'找到什麼?String value = myintent.getStringExtra(「myintent」);'? – Barns

回答

0

請注意,您不能直接使用此示例代碼。您需要根據您在其他活動Main2ActivityMain3Activity中所做的更改進行更改。

首先從MainActivity中刪除這兩行代碼,因爲我沒有看到您發送MainActivity任何東西。

Intent myintent = getIntent(); 
String value = myintent.getStringExtra("myintent"); 

在任一Main2ActivityMain3ActivityonItemClick方法檢索您選擇的值:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     // I use yourList because I do not know how you call 
     // your list in either Main2Activity or Main3Activity 
     // In MainActivity you called it items 
     selectionResult = yourList[position]; 

     //Now return that value back to the activity which called this activity 
     Intent intent = new Intent(); 
     intent.putExtra("myintent", selectionResult); 
     setResult(RESULT_OK, intent); 
     finish(); 
    }); 
} 

在你MainActivity你需要從其他的活動之一獲得的結果。所以你需要使用重寫的方法onActivityResult

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    try{ 
     if (requestCode == REQUEST_CODE_MAIN) { 
      if(resultCode == RESULT_OK){ 
       // Here is where you get your results from Main2Activity or Main3Activity 
       String s = data.getStringExtra("myintent"); 
       if(!s.isEmpty){ 
        textview.setText(value); 
       } 
       else{ 
        Toast.makeText(MainActivity.this, "Don't know what happened", Toast.LENGTH_SHORT).show(); 
       } 
      } 
      if(resultCode == RESULT_CANCELED){ 
        Toast.makeText(MainActivity.this, "I was canceled", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
    catch (Exception ex){ 
     Log.e(TAG, ex.getMessage()); 
    } 
} 

你應該在你MainActivity設置請求代碼:

private static final String REQUEST_CODE_MAIN = 1524; 

1524是隨機的,但它應該是唯一的。

您還需要在MainActivity類中定義,以便它對onActivityResult可見。

private TextView textview; 

然後裏面onCreate設置這樣的:

textview = (TextView)findViewById(R.id.textView2); 

從您的MainActivity在onItemClick用它來調用其他活動:

Intent myintent = new Intent(view.getContext(), Main2Activity.class); 
startActivityForResult(myintent, REQUEST_CODE_MAIN); 
相關問題