2012-07-15 60 views
0

抄襲別人的代碼,我只有一半的理解中,我已經成功地使一個列表視圖,其中的每個元素包含三個TextViews和一個複選框。使聽衆的按鈕一個ListView

的代碼涉及以下兩個XML文件。首先 「customrowview.xml」:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="142dp" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 
      <TextView android:id="@+id/text1"   

       android:layout_width="match_parent"   
       android:layout_height="fill_parent"/> 
      <TextView android:id="@+id/text2"   

       android:layout_width="match_parent"   
       android:layout_height="fill_parent"/> 
      <TextView android:id="@+id/text3" 

       android:layout_width="match_parent" 
       android:layout_height="wrap_content"/> 

     </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" > 



    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="" /> 
     <Button 
      android:id="@+id/editbut" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Edit" /> 
     </LinearLayout> 
    </LinearLayout> 



</LinearLayout> 

然後 「customlistview.xml」:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ListView android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#000fff" 
    android:layout_weight="2" 
    android:drawSelectorOnTop="false"> 
    </ListView> 
<TextView android:id="@id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFff00" 
    android:text="No data" 
    /> 
</LinearLayout> 

我得到通過訪問列表:

listView = (ListView) findViewById(R.id.mylist); 

代碼還包括:

SimpleAdapter adapter = new SimpleAdapter(
      this, 
      list, 
      R.layout.custom_row_view, 
      new String[] {"label","daysordate","time"}, 
      new int[] {R.id.text1,R.id.text3, R.id.text2} 
      ); 
listView.setAdapter(adapter); 

現在我想做的是索姆就像listView.setlistenerforbuttonwithinlist()!

但不能工作了如何做到這一點。我知道有過那麼之前對相關的問題,但我不明白:-(

編輯答案:看到azgolfers的答案後...我犯了一個自定義適配器如下:

public class myadapter extends SimpleAdapter 
{ 
    LayoutInflater mLayoutInflater; 


    public void myadapter(Context context) 
    { 
     mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     View view = null; 

     if (convertView != null) 
     { 
      view = convertView; 
     } 
     else 
     {  
      view = mLayoutInflater.inflate(R.layout.custom_row_view, null); 
     } 

     Button buttonEdit = (Button) view.findViewById(R.id.editbut); 

     buttonEdit.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View arg0) 
      { 
       Log.i("xx","Button pressed!"); 
      } 
     }); 

     return super.getView(position, convertView, parent); 
    } 

    public myadapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to) 
    { 
     super(context, data, resource, from, to); 
     // TODO Auto-generated constructor stub 
    } 

} 

不幸的是這個崩潰在該行

view = mLayoutInflater.inflate(R.layout.custom_row_view, null); 

有了一個空指針異常......不知道爲什麼:-(

回答

4

首先,需要擴展自己的適配器,可能來自ArrayAdapter。 ArrayAdapter有一個名爲getView的方法,您需要覆蓋併爲某個位置的列表視圖行提供UI。例如

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = null; 
     if (convertView != null) { 
      view = convertView; 
     } else { 
      view = mLayoutInflater.inflate(R.layout.custom_row_view, null); 
     } 
     Button buttonEdit = (Button) view.findViewById(R.id.editbut); 
     buttonEdit.setOnClickListener(...); 
    } 

在getView(),因爲你正在構建該行的UI,你這裏有一個機會來設置單擊處理程序上的按鈕。

此外,你還需要添加到您的按鈕的XML標籤:

android:focusable="false" 
android:focusableInTouchMode="false" 

不這樣做,按鈕列表視圖向內側按壓時將不觸發onclick事件。

1

嘗試сhange

view = mLayoutInflater.inflate(R.layout.custom_row_view, null); 

view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false); 

祝你好運!

+0

null是不允許的,所以更改它太虛假了。但後來還是得到了在該行一個空指針異常:-( – Mick 2012-07-16 11:28:32

+0

是的,當然虛假如果趕上NPE,我覺得LayoutInflater == NULL嘗試通過這條線附近等調用構造:。 LayoutInflater infalter =(LayoutInflater) \t \t \t \t \t的getContext()。getSystemService(上下文。LAYOUT_INFLATER_SERVICE); \t \t \t convertView = infalter.inflate(R.layout.choose_color_listview_item,parent,false); – 2012-07-16 11:35:05

+0

「我認爲LayoutInflater == null」是的,你是對的。它讓我更早地發現了一個問題。謝謝你的幫助。 – Mick 2012-07-16 11:48:18