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