【问题标题】:Capturing Click on a custom listview in Android在Android中捕获单击自定义列表视图
【发布时间】:2012-09-13 06:06:19
【问题描述】:

我使用创建的自定义 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 的点击以及行位置。

谢谢

【问题讨论】:

    标签: android listadapter


    【解决方案1】:

    您将不得不使用新的适配器。在实施之前尝试了解其背后的概念:

    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 
            }
        }
    

    【讨论】:

    • 谢谢 Reno,刚刚做了,还是没有收到 Toast 消息。
    • 您可以为自定义适配器本身的两个按钮添加点击事件。
    • @Tushar 是的,这就是我在评论中写的,请参阅 onclick fn
    • 但在你的情况下,你建议 ViewClickListener 而不是 Listview Item Click Listener?..
    【解决方案2】:

    我在listadapter中使用了一个click事件,并将位置id放入buttons标签中

    Button editButton = view.FindViewById(Resource.Id.editPaymentButton_2) as Button;
    editButton.Tag = position;
    editButton.Clickable = true;
    editButton.Click += editButton_Click;
    

    这是我与 GetView 中的事件绑定的事件

    void editButton_Click(object sender, EventArgs e)
        {
            if (editButtonClicked)
            {
                return;
            }
            editButtonClicked = true;
            var button = sender as Button;
            if (button == null)
                return;
            //OnEdit((Entities.PaymentTemplate)GetItem((int)button.Tag));
        }
    

    【讨论】:

      【解决方案3】:

      由于您需要自定义列表中的两个按钮的单击事件(带有 2 个按钮);您将需要创建一个自定义列表适配器,您可以在其中分别为两个按钮添加点击事件,并且您还将获得点击的位置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        • 1970-01-01
        • 2015-07-26
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多