Android 为ListView中的按钮生成侦听器
复制别人的代码,我只懂一半,我成功地制作了一个listview,其中每个元素包含三个TextView和一个复选框 代码涉及以下两个xml文件。第一个“customrowview.xml”: 守则亦包括:Android 为ListView中的按钮生成侦听器,android,android-listview,Android,Android Listview,复制别人的代码,我只懂一半,我成功地制作了一个listview,其中每个元素包含三个TextView和一个复选框 代码涉及以下两个xml文件。第一个“customrowview.xml”: 守则亦包括: SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.custom_row_view, new String[] {"
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
}
}
使用空指针异常…不确定原因:-(首先,您需要扩展自己的适配器,可能来自ArrayAdapter。ArrayAdapter有一个名为getView的方法,您需要覆盖该方法并在特定位置为listview行提供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"
否则,listview中的按钮在按下时将不会触发OnClick事件。首先,您需要扩展自己的适配器,可能是从ArrayAdapter扩展的。ArrayAdapter有一个名为getView的方法,您需要覆盖该方法,并在某个位置为listview行提供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"
如果不这样做,listview中的按钮在按下时将不会触发OnClick事件。尝试更改
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
到
祝你好运!试着改变
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
到
祝你好运!不允许空值,所以将其更改为false。但在该行仍然出现空指针异常:-(是的,当然是false。如果捕获NPE,我认为LayoutInflater==null。请尝试调用此行附近的构造函数,如:LayoutInflater infalter=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);convertView=infalter.inflate(R.LAYOUT.choose\u color\u listview\u item,parent,false);“我认为LayoutInflater==null”是的,你是对的。这让我在前面发现了一个问题。感谢你的帮助。null是不允许的,所以将其更改为false。但在那一行仍然出现了null指针异常:-(是的,当然是假的。如果你抓住了NPE,我认为LayoutFlater==null。试着调用这行附近的构造函数,比如:LayoutFlater=(LayoutFlater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);convertView=infalter.inflate(R.LAYOUT.choose\u color\u listview\u item,parent,false);“我认为LayoutFlater==null”是的,你说得对。这让我早些时候发现了一个问题。谢谢你的帮助。
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);