Java – chat application in Android so that sender and receiver messages should be on different sides

 protected void onPostExecute( ArrayList<HashMap<String,String>> myArrayList)//    for arraylist(ArrayList<String> result)
   {                            

       for (HashMap<String, String> data : myArrayList)
      {
           String sender_no = data.get(TAG_SENDER_NO);
           String msg1=data.get(TAG_SEN_MSG);
           String receiver_no=data.get(TAG_RECEIVER_NO);

           if(sender_no.equals(senderno))
           {

           listadapter adapter = new SimpleAdapter(SinglechatActivity.this, myArrayList,R.layout.list_row_layout_even, 
           new String[] { TAG_SEN_MSG },new int[] { R.id.message_me });

        //  CustomList adapter= new CustomList(SinglechatActivity.this,myArrayList);//sender_no,  msg1,   receiver_no);

         ListView  lv = (ListView) findViewById(R.id.listview);
           lv.setAdapter( adapter);

          }

           else

           {
               listadapter adapter = new SimpleAdapter(SinglechatActivity.this, myArrayList,R.layout.list_row_layout_odd, 
                       new String[] { TAG_SEN_MSG },new int[] { R.id.message_frnd });

                //  CustomList adapter= new CustomList(SinglechatActivity.this,  sender_no,  msg1,   receiver_no);

                 ListView  lv = (ListView) findViewById(R.id.listview);
                   lv.setAdapter( adapter);

           }

Here, I want to leave messages on the right and left according to the sender and receiver

resolvent:

Use a custom adapter with separate layout for sender and recipient messages. It is called heterogeneous listview

such as it is

public class MyAdapter extends BaseAdapter {

    ArrayList<HashMap<String,String>> messages;
    int SENDER_MESSAGE = 0;
    int RECEIVER_MESSAGE = 1;
    Context context;

    @Override
    public int getCount() {
        return messages.size();
    }

    @Override
    public Object getItem(int position) {
        return messages.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {

        //This is dummy logic
        //Write your own logic to differentiate between sender and receiver message
        if (position % 2 == 0) {
            return SENDER_MESSAGE;
        }

        else {
            return RECEIVER_MESSAGE;
        }
    }

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (getItemViewType(position) == SENDER_MESSAGE) {
                convertView = inflater.inflate(R.layout.sender_message_layout, null);
            } 

            else {
                //Received message
                convertView = inflater.inflate(R.layout.received_message_layout, null);
            }
        }

            //...set text to message layout here


    }



}

For more information about custom adapters, you can refer to

http://www.vogella.com/articles/AndroidListView/article.html#adapterown_ example

For the heterogeneous listview (different row layouts in listview) tutorial, you can refer to this content

http://chrislee.kr/wp/tag/getitemviewtype-tutorial/

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>