小编典典

从不调用BaseAdapter OnItemClickListener

java

我有一个自定义BaseAdapterListView在我农具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

我对吗?


阅读 204

收藏
2020-11-30

共1个答案

小编典典

为什么在适配器内部会有OnItemClickListener?您可能在Adapter内有OnClickListener,或者最佳实践是将OnItemClickListener设置为ListView或您在活动/片段中使用的任何AdapterView。您目前的操作方式因以下几个原因而无法使用:

  1. 您没有将侦听器设置为视图。
  2. 不能 将OnItemClickListener设置为TextView。
  3. 在适配器类内部将onClickListener设置为TextView,意味着您将必须单击TextView本身,否则, 将不会 调用侦听器。
2020-11-30