gpt4 book ai didi

java - BaseAdapter OnItemClickListener 从未被调用

转载 作者:行者123 更新时间:2023-12-01 21:37:28 25 4
gpt4 key购买 nike

我有一个自定义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?!适配器内有 OnClickListener,或者最佳实践是将 OnItemClickListener 设置为 ListView 或您在 Activity/fragment 中使用的任何 AdapterView。按照您现在的做法,由于以下几个原因,它不会起作用:

  1. 您没有为 View 设置监听器。
  2. 不能将 OnItemClickListener 设置为 TextView。
  3. 将 onClickListener 设置为适配器类内的 TextView,意味着您必须单击 TextView 本身,否则,监听器将不会被调用。

关于java - BaseAdapter OnItemClickListener 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com