2015-11-15 43 views
1

你好我有一个问题,我的listView对象只填充了我的适配器对象的第一个项目。ListView填充第一个项目在适配器重复

下面的代码显示了从文本字段中调用addMessage()的tryptySend()获取文本。 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; 
} 

回答

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

谢谢,我也刚刚找到了解决办法和评价自己。 – user2202098

+0

也许你应该使用viewholder或转到recyclerview重用视图 –

+0

请你链接一个好的例子,遵循? – user2202098

0

移动下面的注释块了,如果(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)); 
    } 
相关问题