我有一个相对布局的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"));
Logcat文字
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
MyAdapter.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;
}
}
答案 0 :(得分:0)
要将数据添加到列表视图,您需要向数据集添加数据,然后通过调用adapter.notifyDataSetChanged()
来告知适配器数据集,如上所述。因此,要使列表工作,您需要数据集(即数组)listview(在布局中)和绑定这两个togeter的适配器(即ArrayAdapter
)。您还需要通过调用list.setAdapter()
告诉list其适配器的位置。 ListActivity
只是一个帮助类,而不是一个要求,没有它,ListView的工作完全正常。
答案 1 :(得分: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"));
}
});
<强> EDIT2:强>
您不需要在适配器中拥有值的成员,因为这些值已经是扩展arrayadapter的成员。要访问getView
中的一条特定邮件,您可以致电getItem(position)
。这样您就不需要覆盖listAdapter.add(T item)
答案 2 :(得分: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);