這就是我要實現的onClickListener在類:如何爲綁定到自定義適配器的ListView設置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;
}}
我曾嘗試實施唱首歌,但我得到的是一堆錯誤。我已經閱讀了許多教程和類似的問題,但沒有一個答案似乎適合我的情況,或者我不完全理解它們。任何幫助將非常感激!
是否有你想要的每一行,而不是在一個按鈕理由使每行可點擊? – JRaymond 2012-04-05 20:19:57
我需要這兩件事情發生。每行中的按鈕都可以工作,但我也需要ListView行可點擊。 ListView行點擊方法是我無法工作的。 – user1316202 2012-04-05 20:21:22