从未调用过BaseAdapter OnItemClickListener [英] BaseAdapter OnItemClickListener never called
问题描述
我的 ListView
我有一个自定义的 BaseAdapter
,其中我实现了 AdapterView.OnItemClickListener
。
I have a custom BaseAdapter
for my ListView
in which i implements AdapterView.OnItemClickListener
.
问题是 onItemClick(AdapterView<?>,View,int,long)$永远不会调用c $ c>方法。我想我需要在我的主
活动
中实现接口
而不是我的 ListView
自定义适配器。
The problem is the onItemClick(AdapterView<?>, View, int, long)
method is never called. I guess I need to implements that interface
in my main Activity
and not in my ListView
custom adapter.
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?在适配器内部有OnClickListener,或者最佳做法是将OnItemClickListener设置为ListView或您在activity / fragment中使用的任何AdapterView。
你现在这样做的方式,由于以下几个原因不起作用:
Why would you have an OnItemClickListener inside the Adapter?! Either you have OnClickListener inside the Adapter, or the best practice is to set OnItemClickListener to the ListView or whatever AdapterView you use in your activity/fragment. The way you are doing it right now, it won't work for several reasons:
- 你没有设置听取视图。
- 你不能将OnItemClickListener设置为TextView。
- 将onClickListener设置为TextView里面适配器类,意味着您必须单击TextView本身,否则,将调用侦听器
- You don't set the listener to the view.
- You CAN NOT set an OnItemClickListener to a TextView.
- Setting onClickListener to the TextView inside the adapter class, means that you will have to click the TextView itself, otherwise, the listener WILL NOT be invoked.
这篇关于从未调用过BaseAdapter OnItemClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!