Julio Silva
Julio Silva

Reputation: 23

Android Listview with buttons/checklist

I'm new with ListViews on Android.

I need a layout with 4x5 (buttons, checklist and textbox), and I was thinking of using ListViews with a layout of one line of each (4 buttons with 4 checklists and 4 textbox), then make 5 items.

If I do only one line (without ListView) I've managed to make it work.

But when I mess with ListViews I do not know how to use the "onClick" on buttons and checklist, so I know what line the button was pressed...

Can anyone point me an example or code for this?

Thanks

Upvotes: 0

Views: 1040

Answers (1)

GVillani82
GVillani82

Reputation: 17429

The position will be managed automatically. If your Activity extends ListActivity or ListFragment, you just need to override onListItemClick.

@Override  
    protected void onListItemClick(ListView l, View v, int pos, long id) {  
        super.onListItemClick(l, v, pos, id);
        // your action 
}

Likewise, if your Activity does not extend ListActivity or ListFragment, you have to set the listener over the ListView:

listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

            }
        });

Of course this event is related to the whole item in the list. Instead if you want set a listener for each of view inside your list item, you should do it inside your adapter. Supposing to use an ArrayAdapter:

public class CustomAdapter extends ArrayAdapter<MyObject> {
        private final Context context;        
        private ArrayList<MyObject> list;

        public CustomAdapter(Context context, ArrayList<MyObject> list)
        {
            super(context, R.layout.adapter_row_path, list);
            this.context = context;     
            this.list = list;  
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View rowView = convertView;
            PathHolder holder;  

            if(rowView ==null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);           
            rowView = inflater.inflate(R.layout.adapter_row_path, parent, false);

            holder = new ViewHolder();
            holder.b1 = (Button) rowView.findViewById(R.id.b1);
            holder.b2 = (Button) rowView.findViewById(R.id.b2);

            rowView.setTag(holder);

            }else {
                holder = (ViewHolder) rowView.getTag();
            }

            setRow(holder, position);
            return rowView;
            }

          private void setRow(Holder holder, position){
              holder.b1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // action on button 1 (you know also position here)
                }
            });

            holder.b2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // action on button 2 (you know also position here)
                }
            });
          }

    }


    static class ViewHolder {
        Button b1;
        Button b2;     
    }

Upvotes: 1

Related Questions