这是我要在其中实现 onClickListener 的类:
private void updateUserListView(DatabaseHandler dbh) {
List<User> users = dbh.getAllUsers();
ListView listView = (ListView) findViewById(R.id.userslistview);
listView.setAdapter(new UserArrayAdapter(BeerFriendActivity.this, users));
}
适配器代码为:
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;
}}
我已经尝试实现点击器,但我得到的只是一堆错误。我已经阅读了许多教程和类似的问题,但似乎没有一个答案适合我的情况,或者我不太了解它们。任何帮助将非常感激!