我使用创建的自定义 XML 文件将我的数据库游标绑定(bind)到 ListActivity 中。 XML 文件中的每个项目都有 2 个按钮。我想捕获按钮的点击事件和在列表中的位置。
这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/smListName" android:paddingTop="2dip" android:paddingBottom="3dip" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textSize="22dip" />
<Button android:id="@+id/smListCompleted" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:textStyle="bold" android:textColor="#0000ff" />
<Button android:id="@+id/smListNotCompleted" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:textColor="#ff0000"
android:textStyle="bold" />
</LinearLayout>
这就是我绑定(bind)的方式
db = openOrCreateDatabase("ITC", MODE_PRIVATE, null);
Cursor outlets = db.rawQuery("Select s.salesmanid as _id, s.name ...", null);
this.setListAdapter(new SimpleCursorAdapter(this, R.layout.salesmanlist, outlets, new String[] { "name", "complete", "incomplete" }, new int[] {
R.id.smListName, R.id.smListCompleted, R.id.smListNotCompleted }));
db.close();
我没有使用自定义适配器。现在我想捕捉 smListNotCompleted 和 smListCompleted 的点击以及行位置。
谢谢
最佳答案
您将不得不使用新的适配器。在实现之前尝试理解其背后的概念:
class YourNewAdapter extends SimpleCursorAdapter
{
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
btn = (Button)v.findViewById(R.id.yourbutton);
btn.setOnClickListener(YourActivity.this);
btn.setId(position);
btn.setText("sometext");
v.setLongClickable(true);
}
return v;
}
}
在你的 Activity 中
public void onClick(View v)
{
if(v.getId() == R.id.yourbutton id)
{
// do what you want you can also put this on click listener in the getview fn
}
}
关于android - 捕获点击Android中的自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5062803/