【问题标题】:ListView is populated with first item in adaptor repeatedlyListView 反复填充适配器中的第一项
【发布时间】:2015-11-15 23:21:59
【问题描述】:

您好,我的 listView 对象只填充了适配器对象中的第一项,这有问题。

下面的代码显示了从文本字段中获取文本的尝试发送(),然后调用 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;
}

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    只需将您设置的代码移出 {}。

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

    【讨论】:

    • 谢谢,我也刚刚找到了解决方案并发表了自己的评论。
    • 也许你应该使用 viewholder 或转向 recyclerview 以重用视图
    • 在github中搜索recyclerview!
    【解决方案2】:

    将下面的注释块移出 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));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2018-02-10
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      相关资源
      最近更新 更多