0

这是我要在其中实现 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;
}}

我已经尝试实现点击器,但我得到的只是一堆错误。我已经阅读了许多教程和类似的问题,但似乎没有一个答案适合我的情况,或者我不太了解它们。任何帮助将非常感激!

4

2 回答 2

0

ListView 并没有真正设置为其中包含可点击控件,onClick() 方法使用的是 touchEvent 而不是行。不过有一个解决方法,您可以在 XML 中为您的按钮或其他可点击元素指定一个自定义回调,就像在这个示例中一样。

于 2012-04-05T20:27:46.633 回答
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-05T20:28:43.437 回答