I am using a ListView with a custom row wich has 2 TextViews. I have made my own Adapter and it’s working fine with my list. Now, I want the user to input 2 texts and then insert a new row (with user’s inputs) in my ListView. I’ve tried with add method but I’m getting UnsupportedOperationException. Do I have to override add method also? if so, what do I need to do in it? Thank you.
I’m going to paste a fragment of the code. Let me know if you need further information.
public class ChatAdapter extends ArrayAdapter<ChatItems>{
Context context;
int textViewResourceId;
ChatItems[] objects;
public ChatAdapter(Context context, int textViewResourceId,
ChatItems[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ChatHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, null);
holder = new ChatHolder();
holder.user = (TextView) row.findViewById(R.id.textUser);
holder.msg = (TextView) row.findViewById(R.id.textText);
row.setTag(holder);
}else
holder = (ChatHolder) row.getTag();
ChatItems items = objects[position];
holder.msg.setText(items.msg);
holder.user.setText(items.user);
return row;
}
static class ChatHolder{
TextView user;
TextView msg;
}
}
public class ChatItems {
String user;
String msg;
public ChatItems(String user, String msg){
this.user = user;
this.msg = msg;
}
}
If you want to add another items to your
ArrayAdapteruseArrayListinstead ofArrayas your back-end data holder. If you are usingArray, thanArrayAdapterwill useListinternally which can’t be modified later.Remove
objectsfield from yourChatAdapter.Rewrite your constructor like
to get the item in
getView()useChatItems items = getItem(position)instead ofChatItems items = objects[position];And finally create your adapter like
adapter = new ChatAdapter(this, R.layout.chat_item, new ArrayList<ChatItems>());