【发布时间】:2010-04-14 07:48:38
【问题描述】:
我只是从 Android 开发开始,虽然 Milestone 是一款不错的设备,但 Java 不是我的自然语言,我在 Android SDK、Eclipse 和 Java 本身的 Google 文档方面都在苦苦挣扎。总之……
我正在为 Android 编写一个微博客户端,以配合我的 Windows 客户端 (MahTweets)。目前,我的推文不断来来去去,问题出在 UI 上。
初始调用将正确排序项目(从最高到最低)
- 3
- 2
- 1
刷新时
- 3
- 2
- 1
- 6
- 5
- 4
推文处理完毕后,我打电话给adapter.notifyDataSetChanged();
最初我认为需要对 Adapter 上的 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;
}
}
}
有没有更好的方法在 Android 中对 ListView 进行排序,同时仍然能够使用 LayoutInflater? (rowLayout = t.GetParent().GetUI(t, parent, c)将UI扩展为微博实现可以提供的具体视图)
【问题讨论】: