我有一个自定义BaseAdapter我ListView在我农具AdapterView.OnItemClickListener。
BaseAdapter
ListView
AdapterView.OnItemClickListener
问题是该onItemClick(AdapterView<?>, View, int, long)方法从未调用过。我想我需要interface在我的主适配器Activity而不是ListView自定义适配器中实现它。
onItemClick(AdapterView<?>, View, int, long)
interface
Activity
MyListViewAdapter.java :
public class MyListViewAdapter extends BaseAdapter implements AdapterView.OnItemClickListener { private Context context; private ArrayList<MyListViewRow> rowsList = new ArrayList<MyListViewRow>(); // All one row items private TextView a; public MyListViewAdapter(Context context,ArrayList<MyListViewRow> rowsList) { this.context = context; this.rowsList = rowsList; } @Override public int getCount() { return this.rowsList.size(); } @Override public Object getItem(int position) { return this.rowsList.get(position); } @Override public long getItemId(int position) { return position; } /** * Returns a new row to display in the list view. * * Position - position of the current row. * */ @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); /** * Inflating the root view and all his children and set them to a View Object. * */ View row = layoutInflater.inflate(R.layout.list_view_row,null); // Get all the views in my row this.a = (TextView) row.findViewById(R.id.a_id; MyListViewRow myListViewRow = rowsList.get(position); // Set values to all the views in my row this.a.setText(myListViewRow.getA()); return row; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(context, "onItemClick LV Adapter called", Toast.LENGTH_LONG).show(); } } // End of MyListViewAdapter class
我对吗?
为什么在适配器内部会有OnItemClickListener?您可能在Adapter内有OnClickListener,或者最佳实践是将OnItemClickListener设置为ListView或您在活动/片段中使用的任何AdapterView。您目前的操作方式因以下几个原因而无法使用: