排序的Andr​​oid的ListView [英] Sorting Android ListView

查看:101
本文介绍了排序的Andr​​oid的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是开始Android开发,并同时Milestone是一款不错的设备Java是不是我的自然语言和我挣扎既在Android SDK,Eclipse和Java本身的谷歌文档。反正...

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版附和我的Windows客户端(MahTweets)。目前,我已经得到了鸣叫人来人往没有失败,问题是用户界面。

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.

最初的通话将正确排序的项目(如最高到最低)

The initial call will order items correctly (as in highest to lowest)

  • 3
  • 2
  • 1

当刷新由

  • 3
  • 2
  • 1
  • 6
  • 5
  • 4

在该微博被处理,我打电话 adapter.notifyDataSetChanged(); 起初我以为的getItem()的适配器需要进行排序(和code以下是我结束了),但我仍然没有任何运气。

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.

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;
    }
}
}

有没有更好的方法去整理在Android的列表视图,同时仍然能够使用LayoutInflater? (的RowLayout = t.GetParent()的getUI(T,父母,C)扩展用户界面的特定视图,它的微博实现可以提供)

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)

推荐答案

我不会推荐排序的基础列表中的getItem(),在我看来得到..()方法应该是幂等,并没有任何的其母公司的状态外部可见的效果。

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.

在你的问题的情况下,我会处理这个问题将在本微博被添加到容器中的点。您还没有包含这部分您的应用程序源代码,但它很可能你正在使用一个ArrayList。也许你要查询的Twitter的API的鸣叫责令最近下跌的名单,并要求加()在您的ArrayList中的每个结果,这是增加的鸣叫到列表的底部。

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?我会推荐的逆转的通过来自微博API的结果迭代,并在您的ArrayList与附加的顶部插入每个结果(0,...),而不是添加到列表的后面。

Did you know you can insert at any point into an ArrayList? I would recommend reverse iterating 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.

这篇关于排序的Andr​​oid的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆