【问题标题】:Listview button click handlingListview 按钮点击处理
【发布时间】:2016-12-16 11:58:24
【问题描述】:

我在列表视图中使用按钮... 我的xml代码...

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

如何处理列表视图中的每个按钮单击。? 活动类

 String[] from = new String[]{DBHelper.ID, DBHelper.FNAME, DBHelper.LNAME, DBHelper.ADDRESS, DBHelper.REGID};
int[] to = new int[]{R.id.id,R.id.fname,R.id.lname,R.id.address,R.id.regid};
  adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, from, to, 0);
  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        //listview click event handling
            id = (TextView) arg1.findViewById(R.id.id);
            int id_To_Search = Integer.valueOf(id.getText().toString());

            Bundle dataBundle = new Bundle();
            dataBundle.putInt("_id", id_To_Search);

            Intent intent = new Intent(getApplicationContext(), AddPatient.class);

            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });

如何管理列表视图中每个项目的按钮单击。我尝试了很多方法。我该如何解决这个问题。?请任何人帮助我...

【问题讨论】:

  • 对每个按钮使用android:onClick 属性。

标签: android listview


【解决方案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 ;
}

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

【讨论】:

  • 你能解释一下吗?
  • 您可以在getView中处理您想要实现的按钮的功能
  • 你可以在listitem中编写相应按钮的onClick函数的函数
【解决方案2】:

在您的列表视图适配器中,正常初始化按钮并设置 onclicklistener。

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

【讨论】:

    【解决方案3】:

    参考以下链接

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

    Athul (https://stackoverflow.com/users/6201929/athul) 的答案最适合您的问题。

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

    参考下面

    public class UsersAdapter extends BaseAdapter {
    
        Context context;
        OnClickButtonOnList onClickButtonOnList;
        public UsersAdapter(Context context, ArrayList<User> users, OnClickButtonOnList onClickButtonOnList) {
           this.context = context;
           this.users = users;
           this.onClickButtonOnList = onClickButtonOnList;
        }
    
        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) 
           {
               onClickButtonOnList.onClickButtonInItem(position);
           }
    
       });
    
    
       return convertView ;
        }
    
       public interface OnClickButtonOnList {
            public void onClickButtonInItem(int position);
       }
    }
    

    活动

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

    按 Alt + Enter

    onClickButtonInItem 监听器将被创建。你可以对这个职位做任何事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多