Hi,

I have an app with multiple tabs, each with its own ListActivity,
let's call them SubActivities. The MainActivity that houses the
SubActivities creates a list, and the SubActivities display a subset
of the list based on some parameters. I'm currently implementing this
with separate adapters in each of the SubActivities, and adding items
from the list based on the criteria.

I'm wondering if it's possible/better to use ArrayAdapter's Filter
interface instead to do this. From my reading, Filter only supports
CharSequence parameters, while I may need to filter based on other
primitives such as int.

I tried using filters, but couldn't get things to work. Here's what I
did

In MainActivity.onCreate()
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState)
  setcontentView(R.layout.main)
  isDone = false;
  myList = new ArrayList<ListInfo>();
  viewItems = new Runnable() {
    public void run() {
      myList = generateList();
      synchronized(this) {
        isDone = true;
        notifyAll();
      }
    }
  };
  Thread thread = new Thread(null, viewItems, "Background");
  thread.start();
  // Tab initialization stuff...
}

In SubActivity.onCreate()
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState)
  setcontentView(R.layout.main)
  subsetAdapter = new MyAdapter(this, R.layout.myList),
MainActivity.myList);
  setListAdapter(subsetAdapter);
  ListView list = getListView();
  list.setTextFilterEnabled(true);
  viewSubsetItems = new Runnable() {
    public void run() {
      synchronized(MainActivity.viewItems) {
        if (!MainActivity.getIsDone()) {
          try {
            MainActivity.viewItems.wait(); // Wait for MainActivity to
finish generating list
          } catch (InterruptedException e) {}
        }
        runOnUiThread(returnSubsetItems); // returnSubsetItems adds
items from myList to subsetAdapter
      }
      Thread thread = new Thread(null, viewSubsetItems, "Background");
      thread.start();
}

static Runnable returnSubsetItems = new Runnable() {
  public void run() {
    // Add subset of myList to subsetAdapter here...
    subsetAdapter.getFilter().filter("foo");
    subsetAdapter.notifyDataSetChanged();
  }
}

In MyAdapter, I'm implementing it similar to the classes defined in
http://stackoverflow.com/questions/2519317/how-to-write-a-custom-filter-for-listview-with-arrayadapter.

The problem is that I get an empty list when I run getFilter() in
returnSubsetItems. Even though I pass it "foo", getFilter is detecting
it as null.

How can I fix my problems, and in the first place, is filters a good
way of accomplishing what I'm looking for?

Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to