How do I access to ListView from the adapter

眉间皱痕 提交于 2019-11-29 16:08:50

问题


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 ListView to become invisible on this click.

I have no idea how to get access to the ListView from the adapter.

public class ScheduleArrayAdapter extends ArrayAdapter<ScheduleListItem> {

    /*...*/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(id, null);
        }
        final ScheduleListItem o = items.get(position);
        if (o != null) {

        /*...*/         

            Button details=(Button)v.findViewById(R.id.btn_details);
            details.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //HOW TO MAKE (R.id.lv_schedule) TO BECOME INVISIBLE HERE?
                }
            });
        }
        return v;
    }
}

回答1:


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;
}



回答2:


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..




回答3:


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.



来源:https://stackoverflow.com/questions/10910638/how-do-i-access-to-listview-from-the-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!