2012-07-17 29 views
-2

當我點擊我的ListView中的不同列表項時,我想得到不同的值。例如;當代碼= 1時,我想獲取不同的值,當代碼= 2時,我想獲得不同的值。我怎樣才能做到這一點?如何在Android中單擊不同的列表項時獲取不同的值?

這是我的代碼。


    Element e = (Element) nl.item(i); 


     map.put(KEY_CODE, conParser.getValue(e, KEY_CODE)); 
     map.put(KEY_NAME, conParser.getValue(e, KEY_NAME)); 
     map.put(KEY_COVERS, conParser.getValue(e, KEY_COVERS)); 
     map.put(KEY_TABLE, conParser.getValue(e, KEY_TABLE)); 
     map.put(KEY_SALES, conParser.getValue(e, KEY_SALES)); 

     items.add(map); 


    } 


       final ListAdapter adapter = new SimpleAdapter(this, items, 
      R.layout.list_item, 
      new String[] {KEY_CODE,KEY_NAME,KEY_COVERS,KEY_TABLE,KEY_SALES}, 
     new int[] {R.id.kod, R.id.name,R.id.person,R.id.table,R.id.sale}); 


       setListAdapter(adapter); 

      ListView list= getListView(); 
      reportList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // TODO Auto-generated method stub 


       startActivity(new Intent(NewActivity.this,SingleNewActivity.class)); 

     } 
    }); 
+0

什麼是代碼?它是KEY_CODE嗎? – 2012-07-17 13:24:20

回答

0

目前尚不清楚你想要什麼,但我想指出,你想點擊的項目。 因此,在onItemClick方法中,父參數是當前列表。所以你必須做的是從列表中獲取適配器,然後獲得點擊的項目,你知道這是因爲你有位置。所以基本上,你必須在onItemClick裏面寫:

parent.getAdapter()的getItem(位置)

,你會得到取決於項目情況選擇這樣的項目,你會得到的ID值例如。 最好的

+0

我有一個listView。當我點擊該項目時,我想查看有關此項目的所有信息。當我點擊另一個項目時,我想查看此項目的信息。 – 2012-07-17 13:32:39

+0

好吧然後上面的答案正是你想:) – rallat 2012-07-17 13:33:18

+0

我不知道,我是否錯了或不:在OnItemCLick方法我會寫list.getAdapter().getITem(位置),然後startActivity(新的意圖(NewActivity.this ,SimpleNewActiivty.class)),「所有?我還要添加更多嗎? – 2012-07-17 13:35:40

相關問題