我为自己的BaseAdapter
自定义ListView
,其中实现了AdapterView.OnItemClickListener
。
问题是onItemClick(AdapterView<?>, View, int, long)
方法从不被调用。我想我需要在主interface
中而不是在Activity
自定义适配器中实现该ListView
。
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。
您目前的操作方式因以下几个原因而无法使用:
您没有将侦听器设置为视图。
您不能将OnItemClickListener设置为TextView。
在适配器类内部将onClickListener设置为TextView,意味着您将必须单击TextView本身,否则,将不会调用侦听器。
关于java - 从不调用BaseAdapter OnItemClickListener,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36814378/