2011-05-11 47 views
3

我做了一個自定義listView,它擴展了ListActivity,每行包含2個textview和一個按鈕。android ListView ItemClick獲取子文本

adapter = new SimpleAdapter(this, arraylist, R.layout.simplelistcustom, new String[] { "count","title"}, 
       new int[] {R.id.invisible, R.id.textView11 }); 
     setListAdapter(adapter); 

當在列表視圖排一下,我得到的選定行的索引,並通過使用索引 我得到的排子

protected void onListItemClick(ListView listView, View view, int position, long id) 
    { 
     super.onListItemClick(listView, view, position, id); 
     String selection = listView.getItemAtPosition(position).toString(); 
    } 

當我顯示在「選擇」輸出logcat的是

{count=6, title=etywewetr} 

問題是,我想單獨的內容....這怎麼可能.... plz幫助

thanx提前

+0

哪些內容? – 2011-05-11 05:20:47

+0

{count = 6,title = etywewetr}是兩個單獨的TextView.I想要分別得到標題的值和計數 – 2011-05-11 05:25:27

回答

4

getItemAtPosition方法從您的arraylist參數返回一個元素;你只需要將其轉換爲正確的類型即可。假設你的ArrayList是一個列表<地圖<字符串,字符串>>(我懷疑它是):你想SAPARATE

protected void onListItemClick(ListView listView, View view, int position, long id) 
{ 
    super.onListItemClick(listView, view, position, id); 
    Map<String, String> selection = (Map<String, String>) listView.getItemAtPosition(position); 
    String count = selection.get("count"); 
    String title = selection.get("title"); 
} 
+0

它的工作原理...我使用HashMap ...謝謝 – 2011-05-11 05:59:29

+0

太棒了,很高興我可以幫忙! – 2011-05-11 06:07:34

+1

謝謝。我也用你的答案。 – Santhosh 2012-04-24 03:42:32

0
static class MyViewHolder extends ViewHolder { 

     public MyViewHolder(TextView t2,TextView t1) {   

      text1 = t1; 
      text2 = t2; 
     } 
     TextView text1; 
     TextView text2; 
    } 
private class MyClickableListAdapter extends ClickableListAdapter { 
     public MyClickableListAdapter(Context context, int viewid, 
       List<MyData> objects) { 
      super(context, viewid, objects); 
      // nothing to do 
     } 

     protected void bindHolder(ViewHolder h) { 

      MyViewHolder mvh = (MyViewHolder) h; 
      MyData mo = (MyData)mvh.data;  
      mvh.text.setText(mo.text); 
      mvh.text1.setText(mo.text1); 
     } 
相關問題