我有一个列表视图,显示使用像这样的简单游标适配器从数据库中获取的某些名称 -
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>
请指导我。
答案 0 :(得分:3)
您必须创建一个扩展 SimpleCursorAdapter 的自定义适配器类,然后在bindView()方法中,您可以实际实现按钮侦听器。
像这样,
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 强>
使其适用于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
}
}
答案 1 :(得分:1)
@覆盖 public void bindView(View v,Context context,Cursor c){
selection = ((ChampionActivity) context).getListViewSelection();
int position = c.getPosition();
Cursor myCursor = c;
int nameColumn = myCursor.getColumnIndex(DatabaseHelper.KEY_NAME);
int oneColumn = myCursor.getColumnIndex(DatabaseHelper.KEY_ONE);
int twoColumn = myCursor.getColumnIndex(DatabaseHelper.KEY_TWO);
String firstName = myCursor.getString(nameColumn);
TextView textView = (TextView) v.findViewById(R.id.primary_champ_text);
TextView editTextView = (TextView) v.findViewById(R.id.editTextView);
TextView deleteTextView = (TextView) v.findViewById(R.id.deleteTextView);
if(textView != null) {
textView.setText(firstName);
}
editTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
deleteTextView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});