2013-12-23 115 views
0

我的列表視圖在滾動時不斷變化。我試圖在「消息」變量中顯示消息。當我第一次滾動時,一切都很好。但是當我再次滾動時,textviews重疊。我不確定問題是什麼。Android - 滾動時自定義listview更改?

public class DisplayMessageAdapter extends ArrayAdapter<Message> { 

    Context context; 
    int resource; 
    ArrayList<Message> messages = null; 

    public DisplayMessageAdapter(Context context, int resource, ArrayList<Message> messages) { 
     super(context, resource, messages); 
     this.context = context; 
     this.resource = resource; 
     this.messages = messages; //list of messages to display 
    } 

    @SuppressLint("NewApi") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View row = convertView; 
     final MessagesHolder holder; 


     if(row == null) 
     { 
      //Log.i("row-null","row-null"); 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(resource, parent, false); 

      holder = new MessagesHolder(); 
      holder.sent = (TextView) row.findViewById(R.id.sent_message); 
      holder.received = (TextView) row.findViewById(R.id.received_message); 
      row.setTag(holder); 
     } 

     else 
     { 
      //Log.i("holder-not-null","holder-not-null"); 
      holder = (MessagesHolder) row.getTag(); 
     } 

     Message message = messages.get(position); 


     if(message.sent != null) 
     { 
      holder.sent.setText(message.sent); 
      holder.received.setBackground(null); 
     } 
     else 
     { 
      holder.received.setText(message.received); 
      holder.sent.setBackground(null); 
     } 

     return row; 
    } 

    static class MessagesHolder 
    { 
     TextView sent; 
     TextView received; 

    } 

} 

佈局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
     <TextView android:id="@+id/sent_message" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:textColor="#000000" 
     android:background="@drawable/sent" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp" 

     android:textSize="15sp" 
     /> 

     <TextView 
      android:id="@+id/received_message" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/rcvd" 
      android:layout_marginRight="20dp" 
      android:textColor="#000000" 
      android:layout_marginTop="4dp" 
      android:layout_marginBottom="4dp" 
      android:textSize="15sp" 
      /> 

</RelativeLayout> 
+0

你有這個'holder.received.setText(message.received);'和這個'holder.sent.set.Text(message.sent);'兩次刪除其中的一個 – Raghunandan

+0

@GrIsHu不需要那個coz op擴展ArrayAdapter並具有'super(上下文,資源,消息);' – Raghunandan

回答

0

試圖改變如下條件的在你的TextView設定值。 在你的下面的代碼中,如果條件相同,這沒有任何區別。

if(message.sent != null && message.received != null) 
    { 
     Log.i("adapter","both"); 
     holder.sent.setText(message.sent); 
     holder.received.setText(message.received); 
    } 
    else if(message.sent != null) 
    { 
     holder.sent.setText(message.sent); 
     //holder.received.setBackground(null); 
    } 
    else if(message.received != null) 
    { 
     holder.received.setText(message.received); 
     //holder.sent.setBackground(null); 
    } 

其更改如下:

if(message.sent != null && message.received != null) 
    { 
     holder.sent.setText(message.sent); 
     holder.received.setText(message.received); 
    } 
    else 
    { 
     holder.sent.setText("NA"); 
     holder.received.setText("NA"); 
    } 
+0

實際上,這是一個死代碼(看到這是否正在執行)。我已將其更改爲我最初使用的那個。消息將發送或接收數據。不是都。 – brainfreak

+0

@ user3128485我認爲你需要的是不同的行類型。其中一個接收消息。 http://stackoverflow.com/questions/18868194/android-xml-layout-for-a-listview-with-different-items – Raghunandan

+0

你的意思是發送行和接收行? – brainfreak

0

Temproary解決方案:

的問題似乎是在支架和它的使用。它現在的作品,如果我不使用持有人類的性能提升。雖然我不確定如何正確使用持有人。

0

我有一個類似的問題。我的行中有一個進度條。我的問題是我每次調用視圖時都沒有重置進度欄。您必須重置以往的資產。

相關問題