Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have defined a custom view (xml) for my ArrayAdapter for a ListView, which adds a Button. I'd like to create an OnClickListener for this Button per row, but without creating a custom adapter. Is that possible, or does Android force me to create a custom ArrayAdapter for my ListView?

Here's a snippet of what I'm doing:

glAdapter = new ArrayAdapter<Group>(getActivity(), R.layout.fragment_grouprow, R.id.groupRowText, ListOfGroups);
ListView groupListView = (ListView)mainView.findViewById(R.id.listViewGroupMain);
groupListView.setAdapter(glAdapter);
groupListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          // Call function
      }
});

Now I'd like to add an OnClickListener for the Button in my custom view.

Thanks in advance,

Lali

share|improve this question
    
Post the code of your adapter, it depends of your implementation. You can handle the clicklistener in your adapter or in your object class directly. –  marshallino16 Jul 9 '13 at 9:47
    
Thanks for your reply. I just added a code snippet. –  LaVomit Jul 9 '13 at 9:54
    
@LaVomit you need to override getview. its better to have a custom adapter –  Raghunandan Jul 9 '13 at 10:01

1 Answer 1

up vote 1 down vote accepted

You have to implement you own Adapter and in getView() method set OnClickListener on your button. Here kick off example:

public class CustomAdapter extends ArrayAdapter<Integer> {
    private ListView listView;

    public CustomAdapter(Context context, int textViewResourceId, Integer[] objects, ListView listView) {
        super(context, textViewResourceId, objects);
        this.listView = listView;
    }


    static class ViewHolder {
        TextView text;
        Button btn;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Integer color = getItem(position);

        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_view_row, parent, false);
            ViewHolder h = new ViewHolder();
            h.text = (TextView) rowView.findViewById(R.id.item_text);
            h.btn = rowView.findViewById(R.id.btn);
            rowView.setTag(h);
        }

        ViewHolder h = (ViewHolder) rowView.getTag();

        h.text.setText(color);
        h.indicator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // DO what you want to recieve on btn click there.
            }
        });

        return rowView;
    }
}

ViewHolder here for optimization. You can read about this there.

share|improve this answer
    
Off to writing a custom ArrayAdapter again, which I deleted before, hehehe. Thanks!! –  LaVomit Jul 9 '13 at 11:20
    
You are welcome. –  Divers Jul 9 '13 at 11:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.