2012-03-23 47 views
1

我正在研究android聊天應用程序。我在使用xmpp發送和接收函數時遇到問題。我能夠從模擬器發送消息到xmpp並從xmpp接收消息。但我在列表視圖中顯示傳入和傳出消息時遇到問題。我很困惑如何給條件設置視圖佈局。從xmpp動態數據的列表視圖

if(message from xmpp) { 
    TextView textLabel = (TextView) row.findViewById(R.id.textb); // if message received dislay in left side textview 
    textLabel.setText(receiveddata); //receiveddata contains arraylist of incoming message 
} else (message from me) { 
    TextView textLabel = (TextView) row.findViewById(R.id.texts); // if message sent by me dislay in right side textview 
    textLabel.setText(sentdata); //sentdata contains arraylist of outgoing message 
} 

請告訴我,我怎麼能做到這一點。

謝謝

回答

1

您可以創建一個具有兩個具有相同字段的佈局的適配器類。如果條件爲傳入和傳出的消息使用。並相應地膨脹。

public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     entry = list.get(position); 

     if (convertView == null) { 
      if (getItemViewType(position) == 0) { 
       convertView = inflator.inflate(
         R.layout.messages_even_list_layout, null); 
      } else { 
       convertView = inflator.inflate(
         R.layout.messages_odd_list_layout, null); 
      } 
+0

getItemViewType在android docs.Check中檢查它的類型。如果可以在getView中將其轉換爲另一個視圖,則兩個視圖應共享相同的類型。注:整數必須在0到getViewTypeCount - 1之間。IGNORE_ITEM_VIEW_TYPE也可以返回。 – 2012-03-28 11:22:58