2015-05-25 58 views
0

我當我點擊在活動項目的錯誤,他們都告訴我他的名字,相同的字符串:「NAME_0」列表視圖項開放活動

這是開放的活動,當我們點擊:

public void handle() { 
    TextView var3 = (TextView) this.findViewById(R.id.name); 
    TextView var2 = (TextView) this.findViewById(R.id.meaning); 
    TextView var5 = (TextView) this.findViewById(R.id.proof); 
    Intent var4 = this.getIntent(); 
    int var1 = var4.getIntExtra("name", 0); 
    switch (var1) { 
    case 0: 
     var3.setText(R.string.name_0); 
     var2.setText(R.string.meaning_0); 
     var5.setText(R.string.proof_0); 
     break; 
    case 1: 
     var3.setText(R.string.name_1); 
     var2.setText(R.string.meaning_1); 
     var5.setText(R.string.proof_1); 
     break; 
    case 2: 
     var3.setText(R.string.name_2); 
     var2.setText(R.string.meaning_2); 
     var5.setText(R.string.proof_2); 
     break;  

      } 
}  

,誰開的onclick項目的活動:

String[] values = { "test", "list", "ok", "bon" }; 

protected void onCreate(Bundle var1) { 
    super.onCreate(var1); 
    this.setContentView(R.layout.activity_main); 
    this.listView = (ListView) this.findViewById(R.id.list); 
    listView.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_item, 
      R.id.textView_list, values)); 
    listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
       Intent Start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class); 
       startActivity(Start); 
      } 
     }); 

回答

0

的問題是,你不發送單擊項目的位置

Intent start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class); 
start.putExtra("name", position); 
startActivity(start); 

這應該有效。

您總是得到0,因爲您在這裏設置0(var4.getIntExtra("name", 0);)作爲缺少「名稱」鍵時的默認值。

+0

感謝的它工作 – Markop

0

你永遠不設置「名稱」額外你的意圖,當你在setOnItemClickListener創建它,所以當你嘗試在檢索詮釋「手柄()」方法,你總是會得到0

在執行'startActivity'之前,您必須在意圖上設置額外的內容。

Intent startIntent = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class); 
startIntent.putExtra("name", position); 
startActivity(startIntent); 
+0

謝謝對你也: d – Markop

0

爲了得到你需要從該視圖您從itemclicklistener得到的值項的文本

 listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
       String text = ((TextView)view.findViewById(R.id.name)).getText(); 
       Intent Start = new Intent(MainActivity.this, Asma2Ma3anihaActivity.class).putExtra("name",text); 
       startActivity(Start); 
     } 
    }); 
相關問題