2012-08-31 74 views
10

我使用列表来填充ListView()。用户可以将项目添加到列表中。但是,我需要将项目显示在ListView的顶部。如何在我的列表开头插入一个项目以便以相反顺序显示它?在ListView顶部显示新项目

+0

数据的来源是什么?如果您正在从数据库中读取数据,则可以随时通过'order by'或同等方式更改查询返回的顺序。 – MrLore

回答

10

您应该使用ArrayAdapter并使用insert(T, int)方法。

例:

ListView lv = new ListView(context); 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.id...); 
lv.setAdapter(adapter); 
... 
adapter.insert("Hello", 0); 
+0

我刚刚放弃了我的清单,并使用适配器。工作很棒!谢谢。 – rphello101

+0

不客气! – aymeric

20

默认情况下,列表添加底部的元素。这就是为什么你添加的所有新元素都会显示在底部。如果你想在相反的顺序,可设置为listadapter /视图扭转名单

喜欢的东西之前:

Collections.reverse(yourList); 
+0

好主意,但我不断向列表中添加项目并不断反转,我基本上是以交替的方式添加项目到列表的每一端。由于我需要使用仅保存基本数据类型的SharedPreferences保存数据,因此跟踪两个列表将会非常麻烦。 – rphello101

+0

找到我正在寻找的whati。 thankx – raj

+0

是好的,但不适用于当您尝试使用对象自定义+标记时! – delive

3

ListView控件显示数据,因为它是存储在您的数据源。

当您添加数据库时,它必须在最后添加元素。因此,当您通过Cursor对象获取所有数据并将其分配给ArrayAdapter时,它仅以该顺序排列。基本上你应该试着把数据放在数据库的开头,而不是最后,也许有一些时间戳。

使用ArrayList中,您可以通过Collections.reverse(arrayList)做到这一点,或者如果你正在使用SQLite,您可以使用order by

+0

不,我认为这是错误的。对于listview,你需要传递数组/列表作为输入。在数据库中,除非你有插入的时间戳,你什么都不能做。 – kosa

+0

@Nambari:对不起。我的意思是通过使用Cursor对象从数据库中检索数据,然后将其放入ArrayAdapter中。像这样的东西http://stackoverflow.com/questions/5717732/how-do-i-display-data-from-my-database-in-a-listview。将更新我的答案。谢谢你的评论。 – Swayam

0

你总是可以在你的对象邮戳和排序基于您的ListView ..

public class CustomComparator implements Comparator<YourObjectName> { 
     public int compare(YourObjectName o1, YourObjectName o2) { 
      return o1.getDate() > o2.getDate() // something like that.. google how to do a compare method on two dates 
     } 
    } 

现在排序列表

Collections.sort(YourList, new CustomComparator()); 

这应该对你的清单进行排序,使最新的产品排在前列

0

您可以始终使用LinkedList,然后使用addFirst()方法将元素添加到列表中,并且它将具有所需的行为(ListView顶部的新项目)。

14

而不修改原始列表的另一解决方案,重写的getItem()方法在所述适配器

@Override 
public Item getItem(int position) { 
    return super.getItem(getCount() - position - 1); 
} 

更新:实施例

public class ChatAdapter extends ArrayAdapter<ChatItem> { 
public ChatAdapter(Context context, List<ChatItem> chats) { 
    super(context, R.layout.row_chat, chats); 
} 

@Override 
public Item getItem(int position) { 
    return super.getItem(getCount() - position - 1); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = inflater.inflate(R.layout.row_chat, parent, false); 
    } 

    ChatItem chatItem = getItem(position); 
    //Other code here 

    return convertView; 
} 

}

+1

这是一个非常优雅的解决方案。我做了一个小改动,所以我可以在匿名ArrayAdapter中使用它,但不公开数据项:'return super.getItem(getCount() - 1 - position);' –

+0

这可以工作,但滚动列表中的第一项时看起来像重复到最后一个项目列表。我认为这个问题与listview项目的回收(不确定)有关。 –

+0

你能告诉如何覆盖适配器中的getItem()方法吗? – Apurva

1

mBlogList是一个回收器视图...

mBlogList=(RecyclerView) findViewById(R.id.your xml file); 
mBlogList.setHasFixedSize(true); 


LinearLayoutManager mLayoutManager = new LinearLayoutManager(this); 
mLayoutManager.setReverseLayout(true); 
mLayoutManager.setStackFromEnd(true); 
mBlogList.setLayoutManager(mLayoutManager);//VERTICAL FORMAT 
0

您可以在列表开头处添加元素:如

arraylist.add(0, object) 

然后它将始终在顶部显示新元素。