java 排序 Android ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2635708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting Android ListView
提问by Paul Jenkins
I'm only starting with Android dev, and while the Milestone is a nice device Java is not my natural language and I'm struggling with both the Google docs on Android SDK, Eclipse and Java itself. Anyway...
我只是从 Android 开发开始,虽然 Milestone 是一款不错的设备,但 Java 不是我的自然语言,而且我正在为 Android SDK、Eclipse 和 Java 本身的 Google 文档苦苦挣扎。反正...
I'm writing a microblog client for Android to go along with my Windows client (MahTweets). At the moment I've got Tweets coming and going without fail, the problem is with the UI.
我正在为 Android 编写一个微博客户端,以配合我的 Windows 客户端 (MahTweets)。目前我的推文来来去去都没有失败,问题出在用户界面上。
The initial call will order items correctly (as in highest to lowest)
初始调用将正确排序项目(从最高到最低)
- 3
- 2
- 1
- 3
- 2
- 1
When a refresh is made
进行刷新时
- 3
- 2
- 1
- 6
- 5
- 4
- 3
- 2
- 1
- 6
- 5
- 4
After the tweets are processed, I'm calling adapter.notifyDataSetChanged();Initially I thought that getItem() on the Adapter needed to be sorted (and the code below is what I ended up with), but I'm still not having any luck.
处理完推文后,我正在调用adapter.notifyDataSetChanged();最初我认为需要对适配器上的 getItem() 进行排序(下面的代码是我最终得到的),但我仍然没有任何运气。
public class TweetAdapter extends BaseAdapter
{
private List<IStatusUpdate> elements;
private Context c;
public TweetAdapter(Context c, List<IStatusUpdate> Tweets)
{
this.elements = Tweets;
this.c = c;
}
public int getCount() {
return elements.size();
}
public Object getItem(int position) {
Collections.sort(elements, new IStatusUpdateComparator());
return elements.get(position);
}
public long getItemId(int id) {
return id;
}
public void Remove(int id)
{
notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent)
{
RelativeLayout rowLayout;
IStatusUpdate t = elements.get(position);
rowLayout = t.GetParent().GetUI(t, parent, c);
return rowLayout;
}
class IStatusUpdateComparator implements Comparator
{
public int compare(Object obj1, Object obj2)
{
IStatusUpdate update1 = (IStatusUpdate)obj1;
IStatusUpdate update2 = (IStatusUpdate)obj2;
int result = update1.getID().compareTo(update2.getID());
if (result == -1)
return 1;
else if (result == 1)
return 0;
return result;
}
}
}
Is there a better way to go about sorting ListViews in Android, while still being able to use the LayoutInflater? (rowLayout = t.GetParent().GetUI(t, parent, c)expands the UI to the specific view which the microblog implementation can provide)
有没有更好的方法可以在 Android 中对 ListViews 进行排序,同时仍然可以使用 LayoutInflater?(rowLayout = t.GetParent().GetUI(t, parent, c)将UI扩展到微博实现可以提供的具体视图)
回答by Jim Blackler
I wouldn't recommend sorting the underlying list in getItem(), in my opinion a get..() method should be 'idempotent' and not have any externally-visible effect on its parent's state.
我不建议在 getItem() 中对底层列表进行排序,在我看来,get..() 方法应该是“幂等的”,并且对其父级的状态没有任何外部可见的影响。
In the case of your problem I would tackle it at the point at which the tweets are added to the container. You haven't included this part of your app source but it seems likely you are using an ArrayList. Perhaps you are querying the Twitter API for a list of tweets ordered most recently down, and calling add() on your ArrayList for each result, which is adding the tweets to the bottom of the list.
对于您的问题,我会在将推文添加到容器时解决它。您尚未在应用程序源代码中包含这部分内容,但您似乎正在使用 ArrayList。也许您正在查询 Twitter API 以获取最近排序的推文列表,并为每个结果调用 ArrayList 上的 add(),这会将推文添加到列表底部。
Did you know you can insert at any point into an ArrayList? I would recommend reverseiterating through the results from the Twitter API and inserting each result at the top of your ArrayList with add(0, ....) rather than adding to the back of the list.
您知道可以随时插入到 ArrayList 中吗?我建议反向迭代来自 Twitter API 的结果,并使用 add(0, ....) 将每个结果插入到 ArrayList 的顶部,而不是添加到列表的后面。

