当其他用户发送消息时更改Drawable

时间:2015-04-27 17:43:38

标签: android

我正在尝试使用Bubbles语音创建聊天,我从Json文件中获取消息。

现在我试图在特定用户谈话时改变泡泡的颜色。

我使用Java从我的Json文件中获取发送消息的用户的消息和Id。

我创建了一个" Session"从登录用户获取Id,我将此Id与来自邮件发件人的Id进行比较,以检查我将使用的希望泡泡。

为此,我创建了一个名为position的String。如果登录用户的ID等于打开的会话中的ID,则气泡会更改颜色,然后向右移动。

我创建了一个接收消息并在那里插入气泡的适配器。我正在检查哪个用户发送了消息,调用了位置变量。

我的问题是所有气泡都在改变,但只有一种颜色。我想改变特定消息的泡沫。当登录用户说话时,它必须显示不同的气泡。

我该怎么做?

变量

private static final String TAG_MESSAGE = "reply";
private static final String TAG_ID = "id";

获取数据

 //Calling session from logged in user

            NewSession app = (NewSession) getApplication();
            String logid = app.getLogid();

...

JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String message = c.getString(TAG_MESSAGE);
                        String position = "";

                        if(username == logid){
                        position = "right";
                        }else{
                        position = "left";
                        }

                       HashMap<String, String> contact = new HashMap<String, String>();

                      // adding each child node to HashMap key => value
                      contact.put(TAG_ID, id);
                      contact.put(TAG_MESSAGE, message);

适配器

public View getView(int position, View convertView, ViewGroup parent){

      View v = super.getView(position, convertView, parent);


      TextView message = (TextView) v.getTag();

      if(message == null){
         message = (TextView) v.findViewById(R.id.msg);
          v.setTag(message);
      }

     String url = (String) ((Map)getItem(position)).get(TAG_POSITION);

     message.setBackgroundResource(url == "left" ? R.drawable.bubble_green : R.drawable.bubble_yellow);

      return v;
   }

感谢。

2 个答案:

答案 0 :(得分:0)

不要将Strings与之比较   s1 == s2 你应该使用   s1.equals(S2)

答案 1 :(得分:0)

您在适配器中使用==(参考)比较代替String#equals()比较。

试试这个:

     message.setBackgroundResource("left".equalsIgnoreCase(url) ? R.drawable.bubble_green : R.drawable.bubble_yellow);
相关问题