【问题标题】:Adding new elements to the top of the ListView将新元素添加到 ListView 的顶部
【发布时间】:2012-07-01 18:27:56
【问题描述】:

我正在编写一个 IM 客户端,我需要下载(从文件系统或网络)并在 ListView 的顶部显示新元素(它是消息的历史 - 旧消息在顶部,较新 - 在底部)。我为 ListView 实现了自己的适配器,但我无法在列表的开头添加新元素并重绘它。 (notifyDataSetChanged() 对我不好,因为ListView中的消息索引发生变化,android无法正常重绘)。

其他应用如何做类似的事情?

我没有为它创建特殊代码,我只是为我的 ListView 创建新的适配器:

messagesListView.setAdapter(new MessagesListAdapter(this));

并在 MessagesListAdapter 中重新定义 getView() 和 getCount() 方法(现在扩展 ArrayAdapter)。

我的 ListView XML 是

<ListView
  android:id="@+id/dialog_messages_list"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:layout_marginTop="@dimen/title_height">
</ListView>

我的一个元素(一条消息)的 XML 是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <TextView
      android:id="@+id/dialogMessageText"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text=""
      android:background="@drawable/dialog_message_in"
      />

  <TextView
    android:id="@+id/dialogMessageDatetime"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""/>

</LinearLayout>

您可能需要其他代码吗?

编辑:我试过了

    messagesListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));
    (new Thread() {
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            arrayList.add(0, "qwer");
        }
    }).start();

但它似乎也不是很好。我尝试在线程中调用((ArrayAdapter&lt;String&gt;)messagesListView.getAdapter()).notifyDataSetChanged();,但它异常。

【问题讨论】:

  • 请发布相关代码和您收到的任何错误。有许多方法可以做到这一点。我会关注历史的排序方式,而不是 ListView 或 Adapter。
  • 对不起,@Sam,我很早就精通 StackOverflow cmets。对不起。例如,历史只是 ArrayList,但有时我想在它的开头添加新元素。如果它很简单,它可能是 Deque。我理解你的问题正确吗?
  • 好的,你在哪里创建 ArrayList 来填充 ListView?
  • 它在另一个线程中创建,ListView访问它抛出另一个类的静态字段。
  • 您会发布您创建和填充 ArrayList 的位置吗?

标签: android listview


【解决方案1】:

我建议颠倒 List 的顺序,先显示最新的结果。

运行这个例子:

public class Example extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] array = {"oldest", "older", "old", "new", "newer"};
        List<String> list = new ArrayList<String>();
        Collections.addAll(list, array);
        Collections.reverse(list);

        // When you want to add new Strings, put them at the beginning of list
        list.add(0, "newest");

        ListView listView = (ListView) findViewById(R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
    }
}

您不必以这种方式覆盖 ArrayAdapter 或 ListView 中的任何内容。

【讨论】:

    【解决方案2】:

    您可以以编程方式在与列表视图关联的类中添加一些视图。例如:

    向布局添加内容并创建新元素:

    TextView tv = new TextView(getApplicationContext());
    EditText edit = new EditText(getApplicationContext());
    relative.addView(tv);
    relative.addView(edit);
    

    这是在 xml 布局中操作现有元素:

    final TextView tv = (TextView) v.findViewById(R.id.listItem);
        tv.setText("This an item I am changing");
    

    如果您查看一些相关问题,他们会为您提供更多相关信息。但您也可以在线查看其他人的自定义列表视图和适配器。这个真不错:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2015-08-18
    • 1970-01-01
    相关资源
    最近更新 更多