为ListView中的按钮制作监听器

时间:2012-07-15 14:11:34

标签: android android-listview

复制我只知道一半的其他人的代码,我成功制作了一个listview,每个元素都包含三个TextView和一个CheckBox。

该代码涉及以下两个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>

然后&#34; customlistview.xml&#34;:

<?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()!

但无法解决如何做到这一点。我知道之前有关于SO的相关问题,但我无法理解答案: - (

编辑:看到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);

使用空指针异常...不确定原因: - (

2 个答案:

答案 0 :(得分:4)

首先,您需要扩展自己的适配器,可能来自ArrayAdapter。 ArrayAdapter有一个名为getView的方法,您需要覆盖该方法并为特定位置的列表视图行提供UI。 e.g。

    @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,因此您有机会在按钮上设置单击处理程序。

此外,您还需要将其添加到Button的xml标记中:

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

如果不这样,listview中的按钮在按下时不会触发OnClick事件。

答案 1 :(得分:1)

尝试сhange

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

 view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
祝你好运!

相关问题