开发者

Show specific part of ListView Android

I need a little help with getting the specific part of list view based on current time.I have a list view with times as string like : "09:30" - "10:00" - "10:45".I'm using digital clock to show the current time in one activity and I want to show specific part of that listview based on current time. Example :

It's 10:30 and I want to show 开发者_运维技巧the list view items which are bigger than 10:30 like : 10:45, 10:55 and etc.

Any suggestions how to do that?


I'm not sure how you're maintaining your data. Suppose each item is in a Time object that implements the Comparable interface, and you have List<Time> with all your data. Also, assume that the ListView is populated with an ArrayAdapter backed by a Time[] which you build from your List<Time>. (In MVC terminology, the List<Time> would be your model, the ListView would be the view, and your Activity class would be the controller.)

The following code could be used to populate the ListView with data:

private void updateTimes(List<Time> list) {
    Time[] times = new Time[list.size()];
    list.toArray(times);
    mListView = (ListView) findViewById(R.id.listview);
    mListView.setAdapter(new ArrayAdapter<Time>(this, R.layout.listitem, times));
}

When you wanted to filter the list to only show times after 10:30 (or any other time), you could do this (suppose List<Time> mAllTimes is the master list of times, and the Time class has a constructor that takes a String):

private void filterTime(String onOrAfter) {
    Time time = new Time(onOrAfter);
    List<Time> filtered = new ArrayList<Time>();
    for (Time t : mAllTimes) {
        if (t.compareTo(time) >= 0) {
            filtered.add(t);
        }
    }
    updateTimes(filtered);
}

Calling filterTime("10:30") would limit the list to only times on or after 10:30. Calling updateTimes(mAllTimes) would show the complete, unfiltered list.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜