android - 捕获点击Android中的自定义 ListView

标签 android listadapter

我使用创建的自定义 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/

相关文章:

android - 从另一个适配器调用一个适配器的 notifydatasetchanged

Android:检查多个项目ListView

android - Android 2.3 中崩溃的服务重启后不调用 onStartCommand()

java - 如何将jar文件转换成apk?

删除列表项上的 Android 平滑过渡

Android - 试图从 fragment 调用父方法

java - 具有共享代码库和资源的 Swing 和 Android 应用程序

android - 联系人 ListView

android - 为什么 android 数据绑定(bind)库没有按预期工作?

java - 尝试在空对象引用上调用虚拟方法 'android.view.View android.view.View.findViewById(int)'