简体   繁体   English

消息未在android的调用布局中显示

[英]Message is not being displayed in the called layout in android

I'm doing a chatbot that reads message values from JSON. 我正在做一个从JSON读取消息值的聊天机器人。 In a chat generally, messages sent are on the left of the screen and received messages are on the right. 通常,在聊天中,发送的消息在屏幕的左侧,而收到的消息在屏幕的右侧。 However, after running my app, all messages are included on the right. 但是,运行我的应用程序后,所有消息都将包含在右侧。

The sent message's layout is: my_message.xml
The received message's layout is: their_message.xml

The problem is that all message is being displayed using my_message although the if condition used with the trace is showing that one of the messages should be displayed on the left. 问题是尽管跟踪使用的if条件显示应在左侧显示消息之一,但所有消息都使用my_message显示。

Below you can find my code. 在下面可以找到我的代码。

protected Void doInBackground(Void... arg0) {
    String jsonStr = null;
    jsonStr = jsonManagement.loadJSONFromAsset("contacts.json", mContext);

    Log.e(TAG, "Response from url: " + jsonStr);
    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            JSONArray contacts = jsonObj.getJSONArray("contacts");

            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);
                final String id = c.getString("id");
                String name = c.getString("name");
                String email = c.getString("email");
                String address = c.getString("address");
                String gender = c.getString("gender");

                JSONObject phone = c.getJSONObject("phone");
                String mobile = phone.getString("mobile");
                String home = phone.getString("home");
                String office = phone.getString("office");

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

                contact.put("id", id);
                contact.put("name", name);
                contact.put("email", email);
                contact.put("mobile", mobile);

                contactList.add(contact);
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0 ; j < contactList.size() ; j++){
                        Log.e(TAG, "contactList " + contactList.get(j).get("id"));
                        if (contactList.get(j).get("id").equals("c200") ) {
                            Log.e(TAG, "their message " );
                            ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
                                    R.layout.their_message, new String[]{  "email","mobile"},
                                    new int[]{ R.id.name, R.id.message_body});
                            lv.setAdapter(adapter);
                        } else {
                            Log.e(TAG, "my message ");
                            ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
                                    R.layout.my_message, new String[]{ "email","mobile"},
                                    new int[]{R.id.message_body, R.id.message_body});
                            lv.setAdapter(adapter);
                        }
                    }
                }
            });

        } catch (final JSONException e) {
            Log.e(TAG, "Json parsing error: " + e.getMessage());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Json parsing error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            });
        }
    } else {
        Log.e(TAG, "Couldn't get json from server.");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),
                        "Couldn't get json from assets.
                        Toast.LENGTH_LONG).show();
            }
        });
    }

    return null;
}

And below you will find the stack trace: 在下面,您将找到堆栈跟踪:

2019-08-22 19:47:42.824 24525-24525/com.abc.jsonTest E/MainActivity: contactList c200
2019-08-22 19:47:42.825 24525-24525/com.abc.jsonTest E/MainActivity: their message 
2019-08-22 19:47:42.830 24525-24525/com.abc.jsonTest E/MainActivity: contactList c201
2019-08-22 19:47:42.830 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.835 24525-24525/com.abc.jsonTest E/MainActivity: contactList c202
2019-08-22 19:47:42.835 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.836 24525-24525/com.abc.jsonTest E/MainActivity: contactList c203
2019-08-22 19:47:42.836 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.838 24525-24525/com.abc.jsonTest E/MainActivity: contactList c204
2019-08-22 19:47:42.838 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.839 24525-24525/com.abc.jsonTest E/MainActivity: contactList c205
2019-08-22 19:47:42.840 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.841 24525-24525/com.abc.jsonTest E/MainActivity: contactList c206
2019-08-22 19:47:42.842 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.843 24525-24525/com.abc.jsonTest E/MainActivity: contactList c207
2019-08-22 19:47:42.843 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.844 24525-24525/com.abc.jsonTest E/MainActivity: contactList c208
2019-08-22 19:47:42.844 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.845 24525-24525/com.abc.jsonTest E/MainActivity: contactList c209
2019-08-22 19:47:42.846 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.846 24525-24525/com.abc.jsonTest E/MainActivity: contactList c2010
2019-08-22 19:47:42.847 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.847 24525-24525/com.abc.jsonTest E/MainActivity: contactList c2011
2019-08-22 19:47:42.848 24525-24525/com.abc.jsonTest E/MainActivity: my message 
2019-08-22 19:47:42.848 24525-24525/com.abc.jsonTest E/MainActivity: contactList c2012
2019-08-22 19:47:42.849 24525-24525/com.abc.jsonTest E/MainActivity: my message 

How can I make the message with their message show on the left? 如何使消息及其消息显示在左侧? It is currently showing on the right. 当前显示在右侧。

Thanks in advance. 提前致谢。

Your implementation has some problems. 您的实现存在一些问题。 You do not have to create an adapter for each item on your list. 您不必为列表中的每个项目创建适配器。 Instead, you might consider writing a custom adapter that takes the list and while binding the views for each item, set the layout of the item that will be used. 相反,您可以考虑编写一个接受列表的自定义适配器,并在绑定每个项目的视图时,设置将要使用的项目的布局。

You might consider looking into this answer for an idea of how the implementation should work in your case. 您可能考虑研究此答案 ,以了解实现在您的情况下应如何工作。 I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM