2012-07-15 58 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