0

我在相对布局中有一个 Listview。如何从扩展 Activity 的 ManActivity 向 ListView 添加数据。如果我使用了扩展 ListActivity 的类,我可以使用 adapter.notifyDataSetChanged() ......我猜。有什么建议么?提前致谢

Message msg[] = new Message[]
                {
                    new Message(1, "Hello World", "Thank You"), new Message(1, "Hello World 2", "Thank You 2")
                };
adapter = new MyAdapter(context, R.layout.row, msg);
listview.setAdapter(adapter);

添加这将使我的应用程序崩溃

adapter.add(new Message(1, "Hello World3", "Thank You3"));

日志猫文本

07-07 20:22:53.680: E/AndroidRuntime(1512): FATAL EXCEPTION: AsyncTask #3
07-07 20:22:53.680: E/AndroidRuntime(1512): java.lang.RuntimeException: An error occured while executing doInBackground()
07-07 20:22:53.680: E/AndroidRuntime(1512):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.lang.Thread.run(Thread.java:856)
07-07 20:22:53.680: E/AndroidRuntime(1512): Caused by: java.lang.UnsupportedOperationException
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.AbstractList.add(AbstractList.java:404)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.AbstractList.add(AbstractList.java:425)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at com.abhishek.ally2.chat.doInBackground(chat.java:28)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at com.abhishek.ally2.chat.doInBackground(chat.java:1)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-07 20:22:53.680: E/AndroidRuntime(1512):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-07 20:22:53.680: E/AndroidRuntime(1512):     ... 4 more

我的适配器.java

package com.abhishek.ally2;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<Message>{
    private final Context context;
    private final Message data[];
    int layoutResourceId;

    public MyAdapter(Context context, int layoutResourceId, Message[] values) {
        // TODO Auto-generated constructor stub
        super(context, layoutResourceId, values);
        this.context = context;
        this.data = values;
        this.layoutResourceId = layoutResourceId;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        MessageHolder holder = null;
        if(row == null)
        {
            LayoutInflater inflator = ((Activity)context).getLayoutInflater();
            row = inflator.inflate(layoutResourceId, parent, false);
            holder = new MessageHolder();
            holder.pic = (ImageView)row.findViewById(R.id.pic);
            holder.msg = (TextView)row.findViewById(R.id.msg);
            holder.time = (TextView)row.findViewById(R.id.time);
            row.setTag(holder);

        }
        else
        {
            holder = (MessageHolder)row.getTag();
        }
            Message message = data[position];
            holder.msg.setText(message.msg);
            holder.time.setText(message.time);

        return row;

    }


    static class MessageHolder {
        ImageView pic;
        TextView msg;
        TextView time;
    }
}
4

3 回答 3

0

要将数据添加到您的列表视图,您需要将数据添加到您的数据集,然后通过调用adapter.notifyDataSetChanged()如您提到的那样告诉适配器数据集已更改。因此,要使列表正常工作,您需要数据集(即数组)listview(布局中)和绑定这两个togeter(即ArrayAdapter)的适配器。您还需要通过调用告诉 list 它的适配器在哪里list.setAdapter()ListActivity只是一个辅助类,不是必需的,没有它 ListView 工作得很好。

于 2013-07-07T20:02:34.043 回答
0

这里有一个例子:

Years years = readYearsList(); // return the Years array.

ListView listYears = (ListView)findViewById(R.id.list);
YearAdapter yearAdapter = new YearAdapter(MainActivity.this, R.layout.rowyears, years); // YearAdapter extends ArrayAdapter<Years>
listYears.setAdapter(yearAdapter);
于 2013-07-07T20:19:48.553 回答
0

与marcins的答案不同,您可以通过已设置的适配器管理数据,方法是直接向适配器添加/删除数据adapter.add(item)等。您用于创建适配器的基础列表(如果您使用过的话)将是更新到,您无需担心通知。notifyDataSetChanged()如果您向适配器添加任何观察者,则更有意义。

编辑:

我无法评论您的帖子,然后您编辑了您的示例代码。因为您尝试从 AsyncTask 更新您的列表(据我从堆栈跟踪中看到),您必须调用runOnUiThread(Runnable r)

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        adapter.add(new Message(1, "Hello World3", "Thank You3"));
    }
});

编辑2:

您不需要在适配器中为您的值添加一个成员,因为这些值已经是扩展数组适配器的成员。要访问getView您可以调用的特定消息getItem(position)。这样你就不需要覆盖你的listAdapter.add(T item)

于 2013-07-07T20:14:08.733 回答