代码之家  ›  专栏  ›  技术社区  ›  Epic Gamer_1

自定义适配器不根据条件更改布局

  •  0
  • Epic Gamer_1  · 技术社区  · 7 年前

    我正在开发一个聊天应用程序,我对使用ListView和ArrayAdapters以及自定义适配器非常陌生。我遇到了一个问题,导致我列表中的所有项目都是一个布局,即使它们应该根据布尔值进行更改(true将是传出的聊天气泡,false将是传入的聊天气泡)。

    下面是我检索聊天记录的代码(它不是整个文件,只是一个片段):

    变量声明:

    final ArrayList<chatBubble> objects = new ArrayList<>();
    final CustomAdapter customAdapter = new CustomAdapter(this, objects);
    listView.setAdapter(customAdapter);
    

    获取消息并添加到列表的代码:

     mDatabase.child("chat").child("publicDump").child("dumpedMessages").child("message" + i).addListenerForSingleValueEvent(new ValueEventListener() {
    
                 @Override
                 public void onDataChange(DataSnapshot dataSnapshot) {
    
                     final String message = dataSnapshot.getValue(String.class);
                          if (message != null) {
    
                               final String extract = message.substring(message.lastIndexOf("<") + 1, message.indexOf(">"));
                                               mDatabase.child("users").child("c").child("emailToUsername").child(mAuth.getCurrentUser().getEmail().replace(".", ",")).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
                                                            @Override
                                                            public void onDataChange(DataSnapshot dataSnapshot) {
    
                                                                String username = dataSnapshot.getValue(String.class);
                                                                if (username != null) {
    
                                                                    if (username.equals(extract)) {
    
                                                                        Log.i("Extract", extract);
    
                                                                        int semicnt = 0;
                                                                        int num = 0;
                                                                        //Log.i("Loop", "Yes");
    
                                                                        for (int i = 0; i < message.length(); i++) {
    
                                                                            //Log.i("Loop", "y");
    
                                                                            if (String.valueOf(message.charAt(i)).equals(":")) {
    
                                                                                semicnt++;
                                                                                //Log.i("cnt", String.valueOf(semicnt));
    
                                                                                if (semicnt == 3) {
    
                                                                                    num = i;
                                                                                    i = message.length() - 1;
                                                                                    String time = message.substring(0, (Math.min(message.length(), num)));
                                                                                    String finalM = message.replace(time + ": ", "").replace("<" + extract + "> ", "");
                                                                                    chatBubble chat = new chatBubble(finalM, "From: " + extract + " At: " + time, true);
                                                                                    objects.add(chat);
    
    
    
                                                                                }
    
                                                                            }
    
                                                                        }
    
                                                                    } else {
    
    
    
                                                                        int semicnt = 0;
                                                                        int num = 0;
                                                                       // Log.i("Loop", "Yes");
    
                                                                        for (int i = 0; i < message.length(); i++) {
    
                                                                            //Log.i("Loop", "y");
    
                                                                            if (String.valueOf(message.charAt(i)).equals(":")) {
    
                                                                                semicnt++;
                                                                                //Log.i("cnt", String.valueOf(semicnt));
    
                                                                                if (semicnt == 3) {
    
                                                                                    num = i;
                                                                                    i = message.length() - 1;
                                                                                    String time = message.substring(0, (Math.min(message.length(), num)));
                                                                                    String finalM = message.replace(time + ": ", "").replace("<" + extract + "> ", "");
                                                                                    chatBubble chat = new chatBubble(finalM, "From: " + extract + " At: " + time, false);
                                                                                    objects.add(chat);
                                                                                }
    
                                                                            }
    
    
    
                                                                        }
    
                                                                    }
    
                                                                    customAdapter.notifyDataSetChanged();
    
                                                                }
    
                                                            }
    
                                                            @Override
                                                            public void onCancelled(DatabaseError databaseError) {
    
                                                            }
                                                        });
    
    
    
                                                    }
    
                                                }
    
                                                @Override
                                                public void onCancelled(DatabaseError databaseError) {
    
                                                }
                                            });
    

    以下是我的聊天泡泡代码:

    package com.tejasmehta.codeychat;
    
    public class chatBubble {
    
    
            private String msg;
            private String date;
            private boolean myMessage;
    
            public chatBubble(String msg, String date, boolean myMessage) {
                this.msg = msg;
                this.date = date;
                this.myMessage = myMessage;
            }
    
            public String Msg() {
                return msg;
            }
    
            public String Date() {
                return date;
            }
    
            public boolean myMessage() {
                return myMessage;
            }
    
    }
    

    这是我的customAdapter的代码(它显示,如果布尔值myMessage为true,它将加载不同的布局,如果为false,则加载不同的布局):

    public class CustomAdapter extends BaseAdapter {
    
        private LayoutInflater inflater;
        private ArrayList<chatBubble> objects;
    
        private class ViewHolder {
            TextView msg;
            TextView date;
        }
    
        public CustomAdapter(Context context, ArrayList<chatBubble> objects) {
            inflater = LayoutInflater.from(context);
            this.objects = objects;
        }
    
        public int getCount() {
            return objects.size();
        }
    
        public chatBubble getItem(int position) {
            return objects.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
    
            int layoutResource; // determined by view type
            chatBubble ChatBubble = getItem(position);
    
            if (ChatBubble.myMessage()) {
                layoutResource = R.layout.right_bubble;
            } else {
                layoutResource = R.layout.left_bubble;
            }
            if(convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(layoutResource, null);
                holder.msg = convertView.findViewById(R.id.txt_msg);
                holder.date = convertView.findViewById(R.id.date);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.msg.setText(objects.get(position).Msg());
            holder.date.setText(objects.get(position).Date());
            return convertView;
        }
    }
    

    谢谢你的帮助!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Tomer Shemesh    7 年前

    问题是您的getview 视图正在回收,因此convertview已经有了布局。所以它没有被重新充气到正确的布局。

    您需要使用getViewType()和getViewTypeCount()告诉listview您要使用不同的布局 http://android.amberfog.com/?p=296

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
        return getItem(position).myMessage()?0:1;
    }
    
    
    
    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
    
            int layoutResource; // determined by view type
            chatBubble ChatBubble = getItem(position);
    
            if (ChatBubble.myMessage()) {
                layoutResource = R.layout.right_bubble;
            } else {
                layoutResource = R.layout.left_bubble;
            }
            if(convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(layoutResource, null);
                holder.msg = convertView.findViewById(R.id.txt_msg);
                holder.date = convertView.findViewById(R.id.date);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.msg.setText(objects.get(position).Msg());
            holder.date.setText(objects.get(position).Date());
            return convertView;
        }