2

Suppose I have a ListView which items are layouts who contain three buttons. How can I know efficiently when the user has clicked the, for example, second button of the item number 5?

And also, a second question: if I have an adaptor with that getView method, how can I handle the click events in the activity class instead of the adaptor?

Cheers,

Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
  • Check this may answer your question http://stackoverflow.com/questions/17813864/android-listview-style-and-colors – Noob Jul 23 '13 at 19:23

4 Answers4

4

There are actually lots of ways to do this.

  • You could create a Click adapter for each button that knew which Button it was attached to.
  • You could put a unique tag on each button that the click handler identified and acted on.
  • You could build an ArrayList linking each Button to a code telling the click handler what to do ...  - As Alexander says, you can create a custom adapter for the ListView which handles the buttons. If you use this method, then you need to create the onClick handlers in

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Create all the Views, Buttons, etc
    
        // Create the click handlers:
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                doButtonOneClickActions(position);
            }
         });
    }
    
    private void doButtonOneClickActions(int rowNumber) {
        // Do the actions for Button one in row rowNumber (starts at zero)
    }
    

It rather depends on the rest of your code (and your preferred coding style) which solution to go for ..

Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
  • Thanks Neil. That's it, I was worried about the coding style... I don't really know which is the best way. Thanks four your answer. – Didac Perez Parera Jul 23 '13 at 20:28
  • Please let me clarify one thing. If I have an adaptor with that getView method, how can I handle the click events in the activity class instead of the adaptor? Thanks, – Didac Perez Parera Jul 24 '13 at 08:39
  • @DídacPérez just create a listener in your activity and pass it to your adapter after initialization, but before assign it to listview. – AXSM Nov 07 '14 at 21:29
2

For this purposes you should better create custom adapter for a ListView. Then override getView:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//here you define your button's click events.
//use position parameter to get actual position of current row layout in 
// the list
 }
Oleksandr Karaberov
  • 12,573
  • 10
  • 43
  • 70
  • Thanks four your answer. I actually have implemented a custom adapter and the getView method, but I do need to know how to handle events over the items of the list view in a efficient way. – Didac Perez Parera Jul 23 '13 at 20:26
1

You should create custom adapter and override getView(int position, View convertView, ViewGroup parent) method and there reference your buttons and set them onClickListeners. There you have position of list row so you have everything you need.

Koso
  • 3,200
  • 2
  • 20
  • 22
1

how wonderful that you put three buttons in the list item just like I am doing now! I too am looking for a coding style (not necessarily a solution). Anyway, I would aim at:

  1. Performance
  2. Re-usability
  3. Loosely coupled (yet highly cohesive)

I will delegate the click handlers to an external instance which shall be defined and wired to the adapter using:

public class MyListAdapter extends BaseAdapter {
    View.OnClickListener listener = null;

    public View getView(...) {
        buttonA.setOnClickListener(listener);
        buttonB.setOnClickListener(listener);
        buttonC.setOnClickListener(listener);
    }
    public void setItemClickListener(View.OnClickListener listener) {
        this.listener = listener;
    }
}

It's the listener's job to identify which button has been click. More issues must be address, for example, passing data from MyListAdapter to the listener, and streamlining the listener code as a part of the application logics.

Khanh Hua
  • 1,086
  • 1
  • 14
  • 22