Listview聊天外观无法正常工作

时间:2017-09-07 17:19:51

标签: java android android-layout listview

我正在创建一个短信应用程序,我正在使用聊天列表外观这里是我的Chatlist适配器类。

chatlistAdapter.java:

public class chatList_AdapterClass extends BaseAdapter {

private Context mContext;
private ArrayList<chatList_row> mArrayList;
ViewHolder mHolder=null;

public chatList_AdapterClass(Context context, ArrayList<chatList_row> arrayList) {
    mContext = context;
    mArrayList = arrayList;
}

public class ViewHolder{

    TextView mReceivedMessage;

    LinearLayout.LayoutParams mParams;


    ViewHolder(View v){
        mReceivedMessage=(TextView)v.findViewById(R.id.ChatListMessages);


    }
}

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

@Override
public Object getItem(int i) {
    return mArrayList.get(i);
}

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

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

    View row=ConvertView;
    if (row == null){

        LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.chat_list_row,parent,false);


        mHolder=new ViewHolder(row);
        mHolder.mParams=(LinearLayout.LayoutParams)mHolder.mReceivedMessage.getLayoutParams();
        row.setTag(mHolder);
    }else {

        mHolder=(ViewHolder)row.getTag();

    }

    mHolder.mReceivedMessage.setText(mArrayList.get(position).getMessageContent());

    boolean send=mArrayList.get(position).getTypeofMessage().equals("send");
    setChatRowAppearance(send,mHolder);

    return row;
}

public  void setChatRowAppearance(boolean send,ViewHolder mHolder){


    if (send){
        mHolder.mParams.gravity= Gravity.END;
        mHolder.mReceivedMessage.setTextColor(Color.WHITE);
        mHolder.mReceivedMessage.setBackgroundResource(R.drawable.bubble2);

    }else {

        mHolder.mParams.gravity= Gravity.START;
        mHolder.mReceivedMessage.setTextColor(Color.WHITE);
        mHolder.mReceivedMessage.setBackgroundResource(R.drawable.bubble1);


    }

}




}
  1. 当我发送和接收短信时,我将其保存到数据库
  2. 从数据库中检索并传递给arraylist
  3. 从那个arraylist我左右创建聊天内容
  4. 问题是短信发送和接收之后新短信应该出现在上一条消息下面但是这里我的新短信也出现在以前的短信中......就像....
  5.   

    发送短信
              1
              3
        收到短信
              2
              4

0 个答案:

没有答案