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,

4

4 回答 4

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

于 2013-07-23T19:25:09.263 回答
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
 }
于 2013-07-23T19:19:58.917 回答
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.

于 2013-07-23T19:26:03.177 回答
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.

于 2014-04-06T06:25:12.233 回答