实现具有多行布局的Android ListView

时间:2017-02-15 15:56:41

标签: android listview firebase firebase-realtime-database

我正在尝试制作聊天页面(消息视图)。

有图像和短信。我正在使用Firebase。

所以我尝试了自己的代码。而且它也显示了图像。我的意思是不同的布局工作。但问题是,关闭应用程序后,然后重新打开时。我在字符串而不是图像中看到链接..

这是我的ArrayAdapter。

public class ChatAdapter extends BaseAdapter {
ArrayList<String> message = new ArrayList<> ();
ArrayList<Integer> image = new ArrayList<> ();

Context c;
String username;
String otherusername;

ArrayList<Integer> type = new ArrayList<> ();

public ChatAdapter(ArrayList<String> message, ArrayList<Integer> image, Context c, String username, String otherusername, ArrayList<Integer> type) {
    this.message = message;
    this.image = image;
    this.c = c;
    this.username = username;
    this.otherusername = otherusername;
    this.type = type;
}


@Override
public int getCount() {

    return message.size ();
}

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

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

public static class ViewHolder {
    ImageView imageView;
    TextView messageView;
    CircleImageView profileImage;
}

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

@Override
public int getItemViewType(int position) {
    return type.get (position);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)

@Override

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

    View v = convertView;
    int messageType = getItemViewType (position);

    ViewHolder holder;

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

        if (messageType==1){
            v = View.inflate (c,R.layout.chatroom_image,null);



        }
        else {
            v = View.inflate (c, R.layout.message_item, null);



        }




    }
    if (messageType==1){
        holder = new ViewHolder ();
        holder.imageView = (ImageView) v.findViewById (R.id.imageMessage);
        holder.profileImage = (CircleImageView) v.findViewById (R.id.profile_image);
        Picasso.with(c).load(image.get (position)).into(holder.imageView);


    }
    else {
        holder = new ViewHolder ();
        holder.messageView = (TextView) v.findViewById (R.id.message_view);
        holder.profileImage = (CircleImageView) v.findViewById (R.id.profile_image);

        holder.messageView.setText (message.get (position));
        holder.profileImage.setImageResource (image.get (position));





    }

    return v;
}
}

在Firebase中,数据结构如下所示..

Firebase Datastructure

        messageList.setAdapter (new ChatAdapter (message_list, profile_img_list, getApplicationContext (), Fusername, chat_username,types));

我正在记录数据,它显示了一切。

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我建议您使用[MY FOLDER]/vendor/facebook/php-ads-sdk/src而不是旧RecyclerViewListView真的有点被弃用了。

此处给出了如何在ListView中使用不同ItemView的示例: https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView

答案 1 :(得分:0)

使用当前实现,如果转换视图不为null,则不会用数据填充项目。这意味着列表视图将显示包含旧数据的项目。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        // inflate view

        // populate view
    }
}

尝试移动代码以在null检查之外填充视图持有者。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        // inflate view
    }

    // always populate view
}