Search code examples
androidlistviewandroid-listview

ListView is populated with first item in adaptor repeatedly


Hello I have a problem with my listView object only being populated with the first item from my adapter object.

The below code shows attemptySend() grabbing text from a Text Field which then calls addMessage().

addMessage() adds this message object correctly to the adaptor. But instead of the last object from adapter being added to listView the first object is added each time

public class ChatActivity extends ActionBarActivity {
private String mName;
private int mId;
private TextView mEditText;
private ArrayList<MessageDTO> mMessages;
private MessageListAdapter adapter ;
private ListView listView;
private String mUsername = ProfileDTO.sUserProfileDTO.getmName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);
    Intent intent = getIntent();
   // mSocket.on("new message", onNewMessage);
    mSocket.connect();
    mName= intent.getStringExtra("name");
    mId= intent.getIntExtra("id", 0);
    mEditText=(TextView)findViewById(R.id.editText);


    mMessages = new ArrayList<MessageDTO>();
    adapter = new MessageListAdapter(this, mMessages);
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);

public void sendMessage(View view) {
    attemptSend();
}

private void attemptSend() {
    if (null == mUsername) return;

    String message = mEditText.getText().toString().trim();
    if (TextUtils.isEmpty(message)) {
        return;
    }

    mEditText.setText("");
    addMessage(mUsername, message);
    mSocket.emit("new message", message);
}

private void addMessage(String username, String message) {
    MessageDTO messageDTO = new MessageDTO();
    messageDTO.setmMessage(message+", username:" + username);
    Log.d("ChatActivity:addMessage", messageDTO.getmMessage());
    adapter.add(messageDTO);        

}

Adapter class

public class MessageListAdapter extends ArrayAdapter<MessageDTO> {
    public MessageListAdapter(Context context, ArrayList<MessageDTO> messages) {
    super(context, 0, messages);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    MessageDTO message = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null  ) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.object_message_list, parent, false);
        convertView.setBackgroundResource(R.drawable.rectangle);
        // Lookup view for data
        TextView messageBody = (TextView) convertView.findViewById(R.id.messageBody);
        TextView messageInfo = (TextView) convertView.findViewById(R.id.messageInfo);


        // Populate the data into the template view using the data object
        messageBody.setText(message.getmMessage());
        messageInfo.setText(message.getmDate()+" "+message.getmTime());
        // Return the completed view to render on screen

        if (message.getmUserId() == ProfileDTO.sUserProfileDTO.getmId()){
            convertView.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
            messageBody.setTextColor(getContext().getResources().getColor(R.color.white));
            messageInfo.setTextColor(getContext().getResources().getColor(R.color.white));
        }else{
            convertView.setBackgroundColor(getContext().getResources().getColor(R.color.grey));
            messageBody.setTextColor(getContext().getResources().getColor(R.color.black));
            messageInfo.setTextColor(getContext().getResources().getColor(R.color.black));
        }
    }






    return convertView;
}

Solution

  • Just move your set code out of {}.

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Get the data item for this position
            MessageDTO message = getItem(position);
            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null  ) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.object_message_list, parent, false);
    
        }
    
            convertView.setBackgroundResource(R.drawable.rectangle);
            // Lookup view for data
            TextView messageBody = (TextView) convertView.findViewById(R.id.messageBody);
            TextView messageInfo = (TextView) convertView.findViewById(R.id.messageInfo);
    
    
            // Populate the data into the template view using the data object
            messageBody.setText(message.getmMessage());
            messageInfo.setText(message.getmDate()+" "+message.getmTime());
            // Return the completed view to render on screen
    
            if (message.getmUserId() == ProfileDTO.sUserProfileDTO.getmId()){
                convertView.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
                messageBody.setTextColor(getContext().getResources().getColor(R.color.white));
                messageInfo.setTextColor(getContext().getResources().getColor(R.color.white));
            }else{
                convertView.setBackgroundColor(getContext().getResources().getColor(R.color.grey));
                messageBody.setTextColor(getContext().getResources().getColor(R.color.black));
                messageInfo.setTextColor(getContext().getResources().getColor(R.color.black));
            }
    
    
    
    
        return convertView;
    }