捕获单击Android中的自定义列表视图
作者:互联网
我使用自定义XML文件在ListActivity中创建绑定我的db游标. 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>
这就是我的约束力
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;
}
}
在你的活动中
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,listadapter 来源: https://codeday.me/bug/20190715/1462960.html