简体   繁体   中英

Message is not being displayed in the called layout in android

I'm doing a chatbot that reads message values from 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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