开发者

Android ListView Filtering Without Repeatedly Instantiating ArrayAdapters

开发者 https://www.devze.com 2023-01-10 12:37 出处:网络
I have a ListView filled with twitter tweets. I made a custom Tweet class so I could store a special type number. So, I have the Tweet class and custom ArrayAdapter:

I have a ListView filled with twitter tweets. I made a custom Tweet class so I could store a special type number. So, I have the Tweet class and custom ArrayAdapter:

class Tweet {
    private int typeNumber; // My unique type number which does not come from Twitter
    private String message;
    // ... other tweet data
}

class TweetsAdapter extends ArrayAdapter<Tweet> {
   private ArrayL开发者_C百科ist<Tweet> tweets;

   TweetsAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
       super(context, textViewResourceId, tweets);
       this.tweets = tweets;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
     // Inflate my own view and set my own tweet row information
   }
}

There is a nav bar which has 4 Buttons representing the 4 special type numbers. I want to display a filtered tweet ListView when a person clicks on one of the type number buttons without destroying the data in the ArrayAdapter.

One approach I came up with is to break up my main tweets ArrayList into sub-ArrayLists and set a new ArrayAdapter with the corresponding sub-ArrayList on the ListView every time you click one of the Buttons. That seems inefficient to instantiate a new ArrayAdapter every time.

Is there a better way to display a filtered ListView while maintaining all the tweet data on the ArrayAdapter(I also tried looking at the filter class and it seems to destroy the data on the adapter to "filter")?


It seems like your approach of making 4 ArrayLists is fine- just make an array of 4 ArrayAdapters. You could even go one step further and make 4 ListViews and simply set the visibility to GONE or VISIBLE depending on which one is selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号