如何在简单游标适配器的列表视图中添加按钮动作 [英] How to add Button action in list view in simple cursor adapter
问题描述
我有一个列表视图,其中显示了使用像这样的简单游标适配器从数据库中获取的某些名称-
wordsDB ob=new wordsDB(this.getActivity(),"DBA",null,1);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this.getActivity(),R.layout.list_row,c,new String[]{ob.val},new int[]{R.id.textView1},0);
ListView lv=(ListView)v.findViewById(R.id.list);
lv.setAdapter(adapter);
现在,我想在每个联系人(即每一行)前面都具有诸如编辑和删除之类的按钮.为此,我在布局文件list_row.xml中添加了按钮,但是如何为这些编辑"和删除"按钮的单击添加侦听器.
我尝试了很多搜索,但是找不到合适的解决方案.我尝试使用自定义光标适配器,但最终在将数据添加到列表视图的位置上感到困惑,即在哪里应该在newView()或bindView()方法中使用以上代码.
这是新的list_row.xml文件带有按钮.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
请指导我.
您必须创建一个扩展 SimpleCursorAdapter 的自定义适配器类,然后在bindView()方法中,实际上可以实现按钮侦听器. /p>
类似这样的东西
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
return convertView;
}
return LayoutInflater.from(context).inflate(R.layout.listform_item);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Button yourButton = (Button) findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
已编辑
要使其与CursorAdapter一起使用,
public class CustomCursorAdapter extends CursorAdapter{
public CustomCursorAdapter(Context context, Cursor c, int flags){
super(context,c,flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
Button yourButton = (Button) findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
// DO your stuff
}
}
I have a list view which shows certain names fetching from a database using simple cursor adapter like this-
wordsDB ob=new wordsDB(this.getActivity(),"DBA",null,1);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this.getActivity(),R.layout.list_row,c,new String[]{ob.val},new int[]{R.id.textView1},0);
ListView lv=(ListView)v.findViewById(R.id.list);
lv.setAdapter(adapter);
Now, I want to have buttons like edit and delete in front of each contact i.e in each row. For that I added buttons in my layout file list_row.xml but how do I add Listeners for clicks on those "edit" and "delete" buttons.
I tried searching alot but could not find a suitable solution. I tried using custom cursor adapter but ended up confusing as to where should I add my data to the list view i.e. where should I use the above code in newView() or bindView() method.
This is new list_row.xml file with buttons.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Kindly guide me .
You have to create a custom adapter class extending SimpleCursorAdapter and then in bindView() method, you can actually implement button listeners.
Something like this,
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
return convertView;
}
return LayoutInflater.from(context).inflate(R.layout.listform_item);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Button yourButton = (Button) findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
EDITED
To make it work with CursorAdapter,
public class CustomCursorAdapter extends CursorAdapter{
public CustomCursorAdapter(Context context, Cursor c, int flags){
super(context,c,flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor){
Button yourButton = (Button) findViewById(R.id.ibtnDelete);
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
// DO your stuff
}
}
这篇关于如何在简单游标适配器的列表视图中添加按钮动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!