How do I access to ListView from the adapter

后端 未结 3 1833
萌比男神i
萌比男神i 2021-01-01 10:00

I have a custom ListView with my own adapter. I\'m handling the click on a Button in my ListView\'s item, and I want the ListVi

相关标签:
3条回答
  • 2021-01-01 10:35

    ViewGroup parent holds reference to the parent of the View returned by getView(), which in your case is your custom listview.

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        ...
        details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                parent.setVisibility(View.INVISIBLE); // or View.GONE
            }
        });
        ...
        return v;
    }
    
    0 讨论(0)
  • 2021-01-01 10:38

    In my case GridItemClickListner didn't worked for some reason. I access the Gridview from Adapter class using this

    public View getView(final int position, View convertView, ViewGroup parent) {
            convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              ((ClockGridView) v.getParent()).performItemClick(v, position, v.getId());
                             (OR)
               ((ClockGridView)parent).performItemClick(v, position, v.getId());
       }
     });
    }
    

    "ClockGridView" which extends "GridView" Class.

    0 讨论(0)
  • 2021-01-01 10:52

    Instead of this you can pass the reference of your Listview through constructor of adapter and store that in local Listview variable. You can use this for access your Listviews method.Like this

    public ViewPackagesAdapter(Activity mActivity, ListView cmgListView) {
            this.mActivity = mActivity;
            this.mListView=cmgListView;
        }
    

    Now access Activity's Listview through mListView..

    0 讨论(0)
提交回复
热议问题