2015-11-15 62 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)); 
    } 
相關問題