【发布时间】:2013-05-19 17:06:07
【问题描述】:
我正在尝试设置一个 if-else 语句来制作一个带有 JSON 的聊天应用程序,我想让 if-else 语句读取 JSON 并将其放在我希望它们所在的标签中。
想象一下,我是“A”,我和“B”聊天。
如果消息来自“B”(不是我),它会将消息放在列表视图中与右侧对齐的标签上。 ELSE - 当我发送一条消息时,它会放在列表视图内的左侧标签中。
我在语句中使用的字符串是“TAG_FACEBOOK”。
我的列表适配器:
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item, new String[] { TAG_CONTEXT, TAG_FACEBOOK }, new int[] {
R.id.context, R.id.facebook });
setListAdapter(adapter);
我尝试使用这样的 if-else 语句来解决它:
if (TAG_FACEBOOK == "B"){
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item, new String[] { TAG_CONTEXT, TAG_FACEBOOK }, new int[] {
R.id.context, R.id.facebook });
setListAdapter(adapter);
}
else {
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item, new String[] { TAG_CONTEXT, TAG_FACEBOOK }, new int[] {
R.id.contextL, R.id.facebookL });
setListAdapter(adapter);
}
但 Eclipse 告诉我这是“死代码”。 我该怎么办?
【问题讨论】:
-
您的 if 语句似乎有误,请尝试使用 if (TAG_FACEBOOK.equals("B")),因为这是比较字符串的标准方法。
标签: android json if-statement android-listview