ListView重复填充适配器中的第一项

时间:2015-11-15 14:38:25

标签: android listview android-listview

您好我的listView对象只有填充了我的适配器对象中的第一个项目。

下面的代码显示了meastySend()从文本字段中获取文本,然后调用addMessage()。 addMessage()将此消息对象正确添加到适配器。但是,不是将适配器的最后一个对象添加到listView,而是每次都添加第一个对象

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

}

适配器类

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

2 个答案:

答案 0 :(得分:0)

只需将您的设定代码移出{}即可。

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

答案 1 :(得分:0)

将下面的注释块移出if(convertView == null)解决了这个

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