ArrayAdapter unchangeable after text filter?

Put your problem here if it does not fit any of the other categories.

ArrayAdapter unchangeable after text filter?

Postby preisl » Tue Sep 29, 2009 6:43 pm

Hi all,

I'm having a problem with a custom ArrayAdapter. The affected code parts consist of a ListView (containing TwoLineListItems) and an extended class of base ArrayAdapter. The ArrayAdapter contains Strings, initialized from ArrayList<String>.

So, I modify the list of strings (basically I apply different sort orders / filters based on class properties outside the ArrayAdapter) and re-set the new strings in the extended ArrayAdapter class by

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private void updateAll(ArrayList<String> mystring)
  3.  
  4. {
  5.  
  6.     this.clear();
  7.  
  8.     for (String s : mystrings)  
  9.  
  10.     {
  11.  
  12.         this.add(s);
  13.  
  14.     }
  15.  
  16. }
  17.  
  18.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


This does work like a charm.

Now, the ListView does have setTextFilterEnabled(true), which does work as expected as well. BUT: after I have used this filter function (independent of wether I clear the filter or not), the strings in the ArrayAdapter can't be changed any more. The proper data is fed into the clear()/add() function above, but the data in the class doesn't change - If I call this.isEmpty() after this.clear(), it returns false (it does return true before using text filters).

I tried a couple of things (clearTextFilter(), enable / disable textfilter, manually calling notifyDataSetChanged(), keeping a reference to the original ArrayList<String> and changing data in that ArrayList) with no luck at all.

There's a lot of code included, so I didn't post the whole code here ... any ideas someone?

best regards & thanks,

Martin
preisl
Freshman
Freshman
 
Posts: 3
Joined: Fri Sep 25, 2009 7:30 am
Location: Austria

Top

Postby wiederke » Wed Sep 30, 2009 1:19 pm

Hi,

the ArrayFilter of the ArrayAdapter copies the data of the ArrayAdapter and performs the filter on the copied data. You can see this behavior in the method android.widget.ArrayAdapter$ArrayFilter.performFiltering.

I think this copy action is the reason for your problem.


Cheers,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby preisl » Wed Sep 30, 2009 1:25 pm

Hi,

is there any way to reset that filter?
I did have a look at the internal data of the ArrayAdapter. There is a "mObjects" object, which does contain the data. Before using the filter .clear() does remove all elements of mObjects, after using the filter, mObjects seems to be locked or whatever ...

regards,

Martin
preisl
Freshman
Freshman
 
Posts: 3
Joined: Fri Sep 25, 2009 7:30 am
Location: Austria

Postby wiederke » Thu Oct 01, 2009 8:50 am

Hi,

you need to run the filter action again to update it. Try something like:

Code: Select all
private void updateAll(ArrayList<String> mystring)
{
    this.clear();
    for (String s : mystrings) 
    {
        this.add(s);
    }
    this.getFilter().filter(listView.getTextFilter());
}



Bye,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby preisl » Thu Oct 01, 2009 6:11 pm

Thanks, that did the trick!

best regards,

Martin

P.S. ein Bier steht, wenn ich mal in der Gegend bin ;-)
preisl
Freshman
Freshman
 
Posts: 3
Joined: Fri Sep 25, 2009 7:30 am
Location: Austria

Postby wiederke » Thu Oct 01, 2009 7:57 pm

Ein Bier geht immer :D
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests