繁体   English   中英

单击按钮后,如何从自定义列表视图中标识特定行

[英]How to identify a specific row from a custom listview once a button has been clicked

我是Android开发的新手,我正在尝试使用每行末尾的按钮创建一个多列列表视图。 我有以下xml来实现这种格式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/value_name"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_weight="1"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"  />
<TextView
    android:id="@+id/child_name"
    android:layout_width="70dp"
    android:layout_height="wrap_content"

    android:typeface="sans"
    android:textColor="#000000"
    android:layout_weight="1"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
    android:id="@+id/point_value"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_weight="1"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"   />

<Button

    android:id="@+id/claimChore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text = "Claim Chore"/>

我一直在网上搜索这个问题的答案,并找到了一个添加到代码的解决方案。 但是,此解决方案的按钮未定制以查找有关特定行的信息,因此我不知道在此方法中要放置什么来检索它。 如果有人能够对这个onClick方法中的内容有所了解,那将是一个很大的帮助。

 wrapper.mySimpleAdapter = new SimpleAdapter(Child_Chore.this, chores, R.layout.list_format, new String[]{"chore","points"},new int[]{R.id.value_name,R.id.point_value})
                {
                    @Override
                    public View getView (int position, View convertView, ViewGroup parent)
                    {
                        View v = super.getView(position, convertView, parent);

                        Button b=(Button)v.findViewById(R.id.claimChore);
                        b.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                               //Where I want to identify the current R.id.value_name;
                            }
                        });
                        return v;
                    }


                };

使用'getView'方法给你的变量'position'。

@Override
                public View getView (int position, View convertView, ViewGroup parent)
                {
                    View v = super.getView(position, convertView, parent);

                    Button b=(Button)v.findViewById(R.id.claimChore);
                    b.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                           //Where I want to identify the current 
                            doAnything(position);
                        }
                    });
                    return v;
                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM