user7113351
user7113351

Reputation:

Listview button click handling

I am using buttons in listview... My xml code...

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

How can i handle the each button click in listview.? Activity class

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

How can i manage on button click for every item in listview.?I tried many ways.. How can i resolve this issue.? Please anybody help me...

Upvotes: 0

Views: 70

Answers (3)

user7266217
user7266217

Reputation:

refer the below link

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

Athul (https://stackoverflow.com/users/6201929/athul) answer is the best for ur question.

otherwise if your need the event action on activity.

refer the below

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

Activity

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

Press Alt + Enter

onClickButtonInItem listener will created. U do anything with this position.

Upvotes: 0

Athul
Athul

Reputation: 821

Try This,

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

It may help you....

Upvotes: 1

MichaelStoddart
MichaelStoddart

Reputation: 5639

In your listview adapter, initialise the buttons as normal and set an onclicklistener.

Take a look at this SO answer for more information: Android: ListView elements with multiple clickable buttons

Upvotes: 0

Related Questions