Good all the time of day. I have not a big problem with the ListView markup programmatically. The fact is that I load messages from the server, and when they load, I try to change the gravity of the objects, depending on the difference in the ID. In this case, LinearLayout. In general, what I want .... the code builds a list in the ListView, I want my message to be on the left, and the user's message was lined up along gravity, on the right. Now I will show in the screenshots as I have already done. Maybe this is not right? In the first picture, it shows how Messages were loaded and the list was built. But if you scroll the ListView down and climb to the top, in the second one you can see how (the first two) objects lost gravity. Tell me the direction. Thank you in advance :)

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 Build

  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); 

Figure number 1

Figure number 1

Figure number 2

Figure number 2


Here is the currently obtained adapter:

 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 class ViewHolder { private LinearLayout layout; private LinearLayout layoutBg; private TextView textView; private TextView userText; } @Override public View getView(int position, View convertView, ViewGroup parent){ //View v = super.getView(position, convertView, parent); ViewHolder viewHolder; if(convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.activity_friend_msg_adapter, parent, false); viewHolder = new ViewHolder(); viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layoutUsersMsg); viewHolder.layoutBg = (LinearLayout) convertView.findViewById(R.id.layoutUsersMsgBg); viewHolder.textView = (TextView) convertView.findViewById(R.id.id_msg_user); viewHolder.userText = (TextView) convertView.findViewById(R.id.msg); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } String myIDfromList = viewHolder.textView.getText().toString(); viewHolder.layout.setBackground(getResources().getDrawable(R.drawable.lv_msg_styles)); if(!myIDfromList.equals(LV_USID)) { viewHolder.layout.setGravity(Gravity.RIGHT); viewHolder.layoutBg.setBackground(getResources().getDrawable(R.drawable.user_style_msg)); } else { viewHolder.layout.setGravity(Gravity.LEFT); viewHolder.layoutBg.setBackground(getResources().getDrawable(R.drawable.my_style_msg)); } return convertView; } } 
  • Look at any answer here, on the site about ListView and ViewHolder - YuriySPb
  • I did not understand your direction. - sergei1094
  • Most likely the case in this line is View v = super.getView(position, convertView, parent); - You would need to implement the ViewHolder pattern. The markup of the elements is reused and in your case a wide markup was loaded and it tried to transform to the old look. But unsuccessfully. ViewHolder may solve the problem - Juriy Spb
  • Redid it on ViewHolder, now I have the template convertView = inflater.inflate(R.layout.activity_friend_msg_adapter, parent, false); from 7 lines, instead of my messages. Ie in fact, the list was loaded, but instead of messages, a template name. Why is that? - sergei1094
  • Show the resulting code in question - YuriySPb

0