我有一个带有自定义适配器的ListView,其中包含一个Button。我的活动我创建了这个列表视图:
rListView = (ListView) findViewById(R.id.result_list_view);
RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this, allRequests);
rListView.setAdapter(adapter);
在我的适配器中,我有以下方法填充列表中的项目:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = mInflater.inflate(R.layout.list_item_request, parent, false);
TextView titleTextView =
(TextView) rowView.findViewById(R.id.textview_origin);
Button acceptRequestButton = (Button) rowView.findViewById(R.id.acceptRequestButton);
Request request = (Request) getItem(position);
acceptRequestButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
return rowView;
}
所以我在列表的每个项目中都有一个Button acceptRequestButton
,我想为它们添加一个监听器。现在我必须在getView
方法中添加侦听器,如上所示,或者在活动中添加:
AdapterView.OnItemClickListener itemClickListener = ...
rListView.setOnItemClickListener(itemClickListener);