简体   繁体   中英

add Item to custom ListView with custom custom Adapter

I have a custom Adapter (ArrayAdapter) and i want to add items dynamically in Code.

My ArrayAdapter looks like this:

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;
}
}

And i want to add items in this Code:

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);
                        }
                    }
                }
            });

Is there a way to do that? The problem is i am getting this Error Message:

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)

You shouldn't be adding your items to your adapter directly. Add them to your data array which is passed to an adapter (when adapter is creating you pass this array to constructor) and then call adapter.notifyDataSetChanged() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM