2012-07-06 65 views
0

我正在使用listview。我已經添加了一個Textview和按鈕到該列表視圖。如何知道哪個按鈕被點擊。當我點擊那個按鈕時,我需要獲取相關文本?列表視圖中的按鈕

任何人都可以幫助我嗎?

+0

請發佈使用自定義適配器您code..you?填充列表視圖? – 2012-07-06 08:01:38

回答

0

,如果你不想使用列表項點擊要添加的按鈕,當您生成細胞,並添加您可以將代碼設置爲它button.setTag(INT)的按鈕,就可以設置爲標記單元格的位置,所以在按鈕的點擊監聽器中,您可以檢索button.getTag()單擊的按鈕的位置,如果您有一個數據結構,並且文本行在

之內
1

請參見下面的示例代碼...你從這個獲取想法..

這裏list_v是列表視圖

list_v.setAdapter(new ListViewAdapter_test(
           this)); 

public class ListViewAdapter_test extends BaseAdapter { 

     private LayoutInflater mInflater; 

     public ListViewAdapter_test(Context con) { 
      // TODO Auto-generated constructor stub 
      mInflater = LayoutInflater.from(con); 
     } 

     public int getCount() { 
      // TODO Auto-generated method stub 
      return a_product_id.size(); 
     } 

     public Object getItem(int position) { 
      // TODO Auto-generated method stub 

      return position; 
     } 

     public long getItemId(int position) { 
      // TODO Auto-generated method stub 

      return position; 
     } 

     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      // TODO Auto-generated method stub 
      final ListContent holder; 
      View v = convertView; 
      if (v == null) { 
       v = mInflater.inflate(R.layout.scan_row1, null); 
       holder = new ListContent(); 

       holder.name = (TextView) v.findViewById(R.id.sc_textname); 


       holder.total_rate = (Button) v.findViewById(R.id.button1); 

       v.setTag(holder); 
      } else { 

       holder = (ListContent) v.getTag(); 
      } 

      holder.name.setId(position); 

      holder.total_rate.setId(position); 

      holder.total_rate.setOnClickListener(mOnTitleClickListener3); 

      try { 

       holder.name.setText("" + a_product_name.get(position)); 

       holder.total_rate.setText("Read " + a_totreviews.get(position) 
         + " reviews"); 

      } catch (Exception e) { 
       // TODO: handle exception 

      } 

      return v; 
     } 
    } 

    static class ListContent { 



     TextView name; 

     Button total_rate; 

    } 

public OnClickListener mOnTitleClickListener3 = new OnClickListener() { 
     public void onClick(View v) { 
      final int position = list_v 
        .getPositionForView((View) v.getParent()); 

      /** do your code here whatever you want */ 

     } 
    }; 
+0

感謝Dhaval其工作!!!!!! – user1471876 2012-07-10 12:23:18

+0

我的榮幸....讓它成真...... – 2012-07-10 14:17:40