复制别人的代码,我只懂一半,成功制作了一个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>
然后“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() !
但不知道该怎么做。我知道以前有关于 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);
有一个空指针异常......不知道为什么:-(