Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access to ListView from the adapter

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;     } } 
like image 256
user1049280 Avatar asked Jun 06 '12 08:06

user1049280


People also ask

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

How do I set up a list adapter?

Obtain the reference of your ListView using findViewById(int) . Then call the method ListView. setAdapter(ListAdapter) on that reference with your adapter as the parameter.


2 Answers

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; } 
like image 140
Vladimir Avatar answered Sep 20 '22 15:09

Vladimir


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

like image 34
Pramod Avatar answered Sep 21 '22 15:09

Pramod