聊天在Android应用程序,使发送者和接收者的信息应该是在不同的侧面 [英] chat application in android so that sender and receiver message should be on different side

查看:109
本文介绍了聊天在Android应用程序,使发送者和接收者的信息应该是在不同的侧面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

           }

在此,我想根据发送者和接收者会对右侧消息,左手边。

In this, I would like to have message on right hand side and left hand side based on the sender and receiver.

推荐答案

使用定义适配器与发送者和接收者的邮件单独的布局。这就是所谓的异构的ListView

Use Custom adapter with separate layouts for sender and receiver messages. It is called Heterogeneous ListView.

像这样

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


    }



}

有关自定义适配器的详细信息您可以参考此

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

有关异类的ListView(ListView中不同的行布局)的教程,你可以参考这个

For heterogeneous ListView (Different row layouts in ListView) tutorial you can refer this

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

这篇关于聊天在Android应用程序,使发送者和接收者的信息应该是在不同的侧面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆