我有一个自定义适配器(ArrayAdapter),我想在代码中动态添加项目。
我的 ArrayAdapter 看起来像这样:
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.TextView;
public class ChatList extends ArrayAdapter<ChatListItem> {
Context context;
int layoutResourceId;
ChatListItem data[] = null;
public ChatList(Context context, int layoutResourceId, ChatListItem[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ChatHolder holder = null;
ChatListItem entry = data[position];
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ChatHolder();
holder.txtText = (TextView) row.findViewById(R.id.txtText);
holder.txtUser = (TextView) row.findViewById(R.id.txtUser);
holder.txtVersion = (TextView) row.findViewById(R.id.txtVersion);
holder.txtZeit = (TextView) row.findViewById(R.id.txtZeit);
row.setTag(holder);
} else {
holder = (ChatHolder) row.getTag();
}
holder.txtText.setText(entry.text);
holder.txtUser.setText(entry.user);
holder.txtVersion.setText(entry.v);
holder.txtZeit.setText(entry.time);
return row;
}
static class ChatHolder {
TextView txtText;
TextView txtUser;
TextView txtVersion;
TextView txtZeit;
}
}
我想在此代码中添加项目:
LoadChatData bdl = new LoadChatData(new OnTaskComplete() {
@Override
public void onTaskCompleted(ChatListItem[] url, int id) {
currid = id;
for (ChatListItem i : url) {
if (chatList != null) {
chatList.add(i);
}
}
}
});
有没有办法做到这一点?问题是我收到此错误消息:
02-16 11:17:57.287: W/System.err(24997): java.lang.UnsupportedOperationException
02-16 11:17:57.287: W/System.err(24997): at java.util.AbstractList.add(AbstractList.java:404)
02-16 11:17:57.287: W/System.err(24997): at java.util.AbstractList.add(AbstractList.java:425)
02-16 11:17:57.287: W/System.err(24997): at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
02-16 11:17:57.287: W/System.err(24997): at de.tecfriends.vbtsplash2013.ActivityNewChat$3$1.onTaskCompleted(ActivityNewChat.java:108)
02-16 11:17:57.287: W/System.err(24997): at de.tecfriends.vbtsplash2013.LoadChatData.doInBackground(LoadChatData.java:45)
02-16 11:17:57.287: W/System.err(24997): at de.tecfriends.vbtsplash2013.LoadChatData.doInBackground(LoadChatData.java:1)
02-16 11:17:57.287: W/System.err(24997): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-16 11:17:57.287: W/System.err(24997): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-16 11:17:57.287: W/System.err(24997): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-16 11:17:57.287: W/System.err(24997): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-16 11:17:57.287: W/System.err(24997): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-16 11:17:57.287: W/System.err(24997): at java.lang.Thread.run(Thread.java:856)