捕捉点击Android中的自定义列表视图 [英] Capturing Click on a custom listview in Android

查看:118
本文介绍了捕捉点击Android中的自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一个ListActivity我分贝光标绑定创建一个自定义XML文件。在XML文件中的每个项目都有2个按钮。我想捕捉按钮的单击事件,并在列表中的位置。

I used a custom XML file created bind my db cursor in a ListActivity. Each item in the XML file has 2 buttons. I want to capture the click event of the button and the position in the list.

这是我的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>

这是我如何结合

And this is how I'm binding

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的点击与该行的位置。

I'm not using a custom adapter. Now I want to capture the click of smListNotCompleted and smListCompleted along with the row position.

感谢

推荐答案

您将必须使用新的适配器。尝试实现这个之前,了解这背后的理念:

You will have to use a new adapter. Try to understand the concept behind this before implementing this :

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中的自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆