传入和传出消息布局

snz8szmq  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(195)

我正在创建一个应用程序,两个人可以互相留言。我已经做了两个xml布局,一个用于传入消息

<de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:id="@+id/image"
        android:src="@color/black"
        android:layout_margin="20sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_layout"
        android:orientation="vertical"
        android:layout_marginTop="20sp"
        android:layout_marginRight="20sp"
        android:layout_marginBottom="20sp">

        <TextView
            android:id="@+id/tv_clan_chat_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="2dp"
            android:layout_marginEnd="2dp"

            android:padding="1dp"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:background="@drawable/testrect"/>
        <ImageView
            android:layout_width="8dp"
            android:layout_height="16dp"
            android:layout_gravity="start"
            android:background="@drawable/testcorner" />

一个用来传递信息

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:id="@+id/image"
        android:src="@color/black"
        android:layout_margin="20sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_layout"
        android:orientation="vertical"
        android:layout_marginTop="20sp"
        android:layout_marginRight="20sp"
        android:layout_marginBottom="20sp">

        <TextView
            android:id="@+id/tv_clan_chat_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="2dp"
            android:layout_marginEnd="2dp"

            android:padding="1dp"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:background="@drawable/outgoing"/>
        <ImageView
            android:layout_width="8dp"
            android:layout_height="16dp"
            android:layout_gravity="start"
            android:background="@drawable/outgoing_triangle" />

我有一个数据库设置使用消防基地和它收集的数据

Username
userid
message
date
time

并使用

private void DisplayAllMessages(DataSnapshot dataSnapshot) {
    Iterator iterator = dataSnapshot.getChildren().iterator();

    while (iterator.hasNext()) {
        String Id = (String)((DataSnapshot) iterator.next()).getValue();
        String chatUser = (String) ((DataSnapshot) iterator.next()).getValue();
        String chatDate = (String) ((DataSnapshot) iterator.next()).getValue();
        String chatMessage = (String) ((DataSnapshot) iterator.next()).getValue();
        String chattime = (String) ((DataSnapshot) iterator.next()).getValue();

如何区分每个用户并相应地更改聊天气泡
我试着做一个简单的基础适配器来让它工作,但我相信我需要做一些不同的convertview

public DatalistAdapter(Context c, String[] message, String userid, String[]  nonuserid) {
    this.mContext = c; 
    Message = message;
    userId = userid;
    NonuserId = nonuserid;

}

public int getCount() {
    // TODO Auto-generated method stub
    return Message.length;
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    Log.i("UserID",userId);

    final View row;
    final View row1;

    if (convertView == null) {

我知道问题出在这里。。显然,它不能读取id之前,它被初始化,我想我需要做一些事情与convertview?

if (!userId.equals(id))

            row = LayoutInflater.from(parent.getContext()).inflate(R.layout.incoming_mesaage, parent, false);
        else

它总是显示这个视图,除非我反转if语句使其不=to id

row = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.outgoing_mesaage, parent, false);

    }else{
        row = convertView;
    }

    message = (TextView) row.findViewById(R.id.tv_clan_chat_display);

    ImageView img = (ImageView) row.findViewById(R.id.image);
     id = String.valueOf(NonuserId[position]);
    Log.i("Non id",id);
    message.setText(Message[position]);

    return (row);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题