0

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)
2
  • What is type of chatList ? Commented Feb 16, 2013 at 10:28
  • It is the custom ArrayAdapter ChatList. Commented Feb 16, 2013 at 10:36

1 Answer 1

2

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().

Sign up to request clarification or add additional context in comments.

2 Comments

Hello. I changed the type to List<ChatListItem> but it still tells me: java.lang.UnsupportedOperationException in Line dataarr.add(i);. Thye dataarr is defined as this: public java.util.List<ChatListItem> dataarr = null; and gets set by dataarr = Arrays.asList(data); where data is ChatListItem[] data
I have got it working by changing it from List to ArrayList, thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.