2012-03-23 37 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