How do I set up onClickListener for ListView binded to custom adapter?

Go To StackoverFlow.com

0

This is the class I want to implement the onClickListener in:

private void updateUserListView(DatabaseHandler dbh) {
    List<User> users = dbh.getAllUsers();
    ListView listView = (ListView) findViewById(R.id.userslistview);
    listView.setAdapter(new UserArrayAdapter(BeerFriendActivity.this, users));      
}

The adapter code is:

public class UserArrayAdapter extends ArrayAdapter<User> {
private final Context context;
private final List<User> values;

public UserArrayAdapter(Context context, List<User> values) {
    super(context, R.layout.userrow, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
    final Context con = context;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.userrow, parent, false);
    Button button = (Button) rowView.findViewById(R.id.userrowbutton);
    TextView textView = (TextView) rowView.findViewById(R.id.userrownametext);
    textView.setText(values.get(position).getName());

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(context,BeerSearchActivity.class); 
            Bundle b = new Bundle();
            b.putInt("id", values.get(pos).getId());
            myIntent.putExtras(b); //Put your id to your next Intent
            con.startActivity(myIntent);
        } 
    });

    User user = values.get(position);
    return rowView;
}}

I have tried implementing the clicker, but all I get is a bunch of errors. I've read many tutorials and similar questions, but none of the answers seem to fit my situation or I'm not quite understanding them. Any help would be much appreciated!

2012-04-05 20:14
by user1316202
Is there a reason you want a button in each row instead of making each row clickable - JRaymond 2012-04-05 20:19
I need both to happen. The button in each row works, but I also need the ListView row to be clickable. The ListView row click method is what I can't get to work - user1316202 2012-04-05 20:21


0

ListView isn't really set up to have clickable controls in it, the onClick() methods consume the touchEvent instead of the row. There is a workaround though, wherein you specify a custom callback in XML for your button or other clickable element, like in this example.

2012-04-05 20:27
by JRaymond
Should I think about doing it differently then? It sounds like a button inside of a listview is not the correct way to do it - user1316202 2012-04-05 20:50
Probably... I only use that workaround for favoriting. What are you trying to do with onItemClick vs. the button click - JRaymond 2012-04-05 20:52
I have 2 listviews on a main activity. I need the bottom listview to be based on whatever is selected in the first listview. There also has to be a button or maybe a longclick to do different actions on each row of the first listview - user1316202 2012-04-05 20:59
Long Click is easy to implement, if you like how it feels as a User - ListView has both a setOnItemClickListener() and setOnItemLongClickListener, but like I said, it really depends on if you feel like that's easy to use - JRaymond 2012-04-05 21:23
Okay, I'll check that out. Thanks for your help - user1316202 2012-04-05 23:35


0

The problem is in focus of button.You should use other View instead of button in each row (ImageView for example) and set onItemClickListener to your listView

2012-04-05 20:28
by Alex Klimashevsky
Should I think about doing it differently then? It sounds like a button inside of a listview is not the correct way to do it - user1316202 2012-04-05 20:50
@user1316202 yes, button inside listview prevent rowclicking. But you can set onClickListener to any View, so replcating button to imageView helps m - Alex Klimashevsky 2012-04-05 20:55
Ads