S
S
sergei10942016-10-05 09:52:51
Android
sergei1094, 2016-10-05 09:52:51

How to make the ListView markup correct, like a chat?

Hello. Tell me please. I have messages loaded from the server into `ListView`. What I want to do is....in the ListView, I want my(usera) message to be on the left, and the other person's message to be on the right. How to correctly implement the substitution of items for user.xml or sobesednik.xml depending on the logic by type: If the user ID in the item == my ID, then show user.xml, otherwise .... I think it's understandable) Tell me if it's not difficult , here is my adapter, or rather what I did with it :)

class ChatAdapter extends SimpleAdapter {

        public ChatAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
            super(context, data, resource, from, to);
        }

        public View getView(int position, View convertView, ViewGroup parent){
            View v = super.getView(position, convertView, parent);

            LinearLayout layout = (LinearLayout) v.findViewById(R.id.layoutUsersMsg);
            LinearLayout layoutBg = (LinearLayout) v.findViewById(R.id.layoutUsersMsgBg);
            layout.setBackground(getResources().getDrawable(R.drawable.lv_msg_styles));

            TextView textView = (TextView) v.findViewById(R.id.id_msg_user);
            TextView userText = (TextView) v.findViewById(R.id.msg);

            String myIDfromList = textView.getText().toString();

            if(!myIDfromList.equals(LV_USID)) { 
                layout.setGravity(Gravity.RIGHT);
                layoutBg.setBackground(getResources().getDrawable(R.drawable.user_style_msg));
            } else {
                layout.setGravity(Gravity.LEFT);
                layoutBg.setBackground(getResources().getDrawable(R.drawable.my_style_msg));
            }
            return v;
        }
    }

ListView and adapter building
String msg = msgList.getString("msg");
                        String msg_id = msgList.getString("msg_id");
                        final String msg_id_us = msgList.getString("msg_id_user");

                        map = new HashMap<String, Object>();
                        map.put(LV_MSGID, msg_id);
                        map.put(LV_MSG, msg);
                        map.put(LV_USID_MSG, msg_id_us);
                        data.add(map);
                    }

                    String[] from = { LV_MSGID, LV_MSG, LV_USID_MSG };
                    int   [] to   = { R.id.id_msg, R.id.msg, R.id.id_msg_user };

                    sAdapter = new ChatAdapter(FriendMsgActivity.this, data, R.layout.activity_friend_msg_adapter, from, to);
                    sList = (ListView) findViewById(R.id.lvFriendMsg);
                    sList.setAdapter(sAdapter);

In essence, this code seems to work, but it seems not :)
When the ListView is loaded, my messages, and the messages of the interlocutor, took the right positions until I started scrolling down. As soon as I scrolled back to the top, the first two objects lost their wrap_content and gravity[]. Simply put .... I'll show you screenshots. Figure 1 shows a list that has just loaded, Figure 2 shows the problem itself.
-
fig1.
57fb902bdfd548b5bf60e05d9eedc642.png
fig.2
c67694d2b40643f4a5b321923953d0c8.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ershov, 2016-10-07
@alaershov

Don't use ListView, use RecyclerView.
It has an excellent method in the adapter:
It determines what kind of element this element belongs to by the element number and returns its numerical code. You need to do the code and implementation of this method yourself.
Then this code is fed (by the system) to
In this method, you can create the View you need from the desired layout. Don't need this:

if(!myIDfromList.equals(LV_USID)) { 
    layout.setGravity(Gravity.RIGHT);
    layoutBg.setBackground(getResources().getDrawable(R.drawable.user_style_msg));
} else {
    layout.setGravity(Gravity.LEFT);
    layoutBg.setBackground(getResources().getDrawable(R.drawable.my_style_msg));
}

Just make two ViewHolder classes and different layouts for them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question