lukassz
lukassz

Reputation: 3340

Button listener in ListView

I used AsyncTask to fulfill my ListView.

public class SIPSettingsFragment extends ListFragment implements View.OnClickListener, AsyncResponse {

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_sipsettings, container, false);

new DownloadJSON().execute();

return rootView;
}

 public class DownloadJSON extends AsyncTask<Void, Void, Void> {

        /*
        some code
        */
        @Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();

            listView = (ListView) getActivity().findViewById(android.R.id.list);

            adapter = new SimpleAdapter(
                    getActivity(),
                    usersList,
                    R.layout.sipuser_list_item,
                    new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
                    new int[] { R.id.username, R.id.addr, R.id.state}
            );
            ((SimpleAdapter) adapter).notifyDataSetChanged();
            setListAdapter(adapter);

            Log.d("lab", "Done");
        }

And my xml

fragment_sipsettings.xml

 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/userListLayout"
        android:layout_gravity="center_horizontal">
           <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/list" />

    </LinearLayout>

sipuser_list_item.xml

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Username: "
                android:layout_alignParentLeft="true"
                android:id="@+id/usernameSIP" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/username"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/usernameSIP" />

            <Button
                android:id="@+id/deleteSIPUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Delete user"
                android:layout_alignParentRight="true"/>


        </RelativeLayout>

List with buttons are display correctly. How to implement button OnClickListerer for button from sipuser_list_item.xml?

Edit:

I solve my question by extend SimpleAdapter and override getView() in AsyncTask @Krupal Shah and @cylon

Upvotes: 2

Views: 72

Answers (2)

Krupal Shah
Krupal Shah

Reputation: 9187

You can either extend SimpleAdapter or use Anonymous inner class like below, In the extending or anonymous class, you have to override getView() method. Inside getView method, you can find button by id and set click listener on that.

 SimpleAdapter k=new SimpleAdapter(
                    getActivity(), 
                    usersList, 
                    R.layout.sipuser_list_item,
                    new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
                    new int[] { R.id.username, R.id.addr, R.id.state}
            )
    { 
        @Override 
        public View getView (int position, View convertView, ViewGroup parent)
        { 
            View view = super.getView(position, convertView, parent);

             Button btn=(Button)v.findViewById(R.id.sipuser_list_item);
             btn.setOnClickListener(new OnClickListener() {

                @Override 
                public void onClick(View v) {
                    // click of the button
                } 
            }); 
            return view;
        } 

    }; 

Upvotes: 2

cylon
cylon

Reputation: 733

Try to overwrite the getView() method of the SimpleAdapter or write your own adapter that extends the BaseAdapter class.

adapter = new SimpleAdapter(...) {
     @Override
     public View getView (int position, View convertView, ViewGroup parent){
          View view = super.getView(position, convertView, parent);
          // get your button here
          Button button = (Button) view.findViewById(R.id.button);
          button.setOnClickListener(....); // set your onclick listener
          return view;
     }
 }

Upvotes: 2

Related Questions