我在相对布局中有一个 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;
}
}