Sebek
Sebek

Reputation: 682

Update listview from inside the list adapter

Here's my problem: I have a listview with several rows. The rows have a single type of listadapter(though there are several types of rows). If the user selects a row, another row needs to be un-selected. The way a row is selected is dependent on the row type. Some rows are selected when they have something written in an edittext field,etc. Suffice to say, the row selection algorithm must be inside the listadapter class.

One solution I used is to have the "selection" display algorithm at the getView() row initialization part and when a row is selected, to call notifiy this.notifyDataSetChanged(); refreshing the entire listview. This, of course, is too costly and causes a whole other set of issues.

So I need a way to update the other rows inside the listview from within my custom adapter class. How do I access the currently selected row's "brothers" ?

Any input would be very appreciated! Thanks!

Upvotes: 1

Views: 1293

Answers (1)

Sebek
Sebek

Reputation: 682

Well, my astute colleague, I believe I have found a solution to your problem:

Use the listView you need as a constructor for your custom adapter!

So in your custom adapter your constructor will be :

    public SubEventListAdapter(Context context, ArrayList<MobileSubEvent> items,  MobileSubActivity parentSubActivity, ListView listView)
        {
            super(context, R.layout.view_select_event_item, items);
            this._items = items;
            this._context = context;
            this.parentSubActivity = parentSubActivity;
            this.listView = listView;
        }

and in your activity you will set the adapter like so:

listView.setAdapter(new SubEventListAdapter(listView.getContext(), subActivity.get_subEventsList(), subActivity, listView));

Now, in your adapter, you can use the listView as you please; for example:

    private void AddAllDots()
    {
        for(int i = 0 ; i< listView.getChildCount(); i++)
        {
            View v = listView.getChildAt(i);
            ImageView dots = (ImageView) v.findViewById(R.id.ivMandSelection);
            dots.setVisibility(View.VISIBLE);
        }
    }

Upvotes: 1

Related Questions