列表视图按钮单击处理

时间:2016-12-16 11:58:24

标签: android listview

我在listview中使用按钮... 我的xml代码......

test.html

如何处理listview中的每个按钮点击。  活动类

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="false"
        android:layout_alignParentEnd="false">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/patient_info"
                android:id="@+id/textView"
                android:layout_alignParentTop="false"
                android:textSize="30sp"
                android:layout_alignParentLeft="false"
                android:layout_alignParentStart="false"
                android:layout_toLeftOf="@+id/button3"
                android:layout_toStartOf="@+id/button3"
                android:layout_marginRight="5dp"
                android:layout_gravity="center_vertical"
                android:layout_weight="1" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="right">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/add"
                    android:id="@+id/button1"
                    android:layout_alignParentRight="false"
                    android:layout_alignParentEnd="false" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sync"
                    android:id="@+id/button3"
                    android:layout_alignParentTop="false"
                    android:layout_toLeftOf="@+id/button1"
                    android:layout_toStartOf="@+id/button1" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>


    <ProgressBar
        style="@android:style/Widget.Material.Light.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar2" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText11"
            android:hint="Old Registration Number" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText10"
            android:hint="New Registartion Number" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/editText7"
            android:hint="Name" />

        <Button
            android:text="search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/button" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="SEARCH RESULTS"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

    </LinearLayout>

</LinearLayout>

如何管理listview中每个项目的按钮点击。我尝试了很多方法..我该如何解决这个问题。请有人帮助我......

3 个答案:

答案 0 :(得分:1)

试试这个,

public View getView(final int position, View convertView,ViewGroup parent) 
{
   if(convertView == null)
   {
        LayoutInflater inflater = getLayoutInflater();
        convertView  = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
   }

   Button Button1= (Button)  convertView  .findViewById(R.id.BUTTON1_ID);

   Button1.setOnClickListener(new OnClickListener() 
   { 
       @Override
       public void onClick(View v) 
       {
           // Your code that you want to execute on this button click
       }

   });


   return convertView ;
}

它可能对你有帮助....

答案 1 :(得分:0)

在listview适配器中,正常初始化按钮并设置onclicklistener。

请查看此SO答案以获取更多信息: Android: ListView elements with multiple clickable buttons

答案 2 :(得分:0)

参考以下链接

http://androidforbeginners.blogspot.in/2010/03/clicking-buttons-in-listview-row.html

Athul(https://stackoverflow.com/users/6201929/athul)回答是最好的问题。

否则,如果您需要对活动进行事件处理。

参考下面的

UsersAdapter arrayadapter = new UsersAdapter(this, users, this);
listview.setAdapter(arrayadapter);

活动

onClickButtonInItem

按Alt + Enter

将创建

.each侦听器。你对这个职位做了什么。