如何检查RecyclerView中已有的文本

时间:2017-12-01 07:02:23

标签: android android-recyclerview android-cardview

public class ChatsFragment extends Fragment {
    public static final String TITLE = "Chats";

FirebaseDatabase database;
DatabaseReference myRef;
List<Listdata> list;
RecyclerView recyclerview;

public static ChatsFragment newInstance() {

    return new ChatsFragment();
}

public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle b) {
    View view = inflater.inflate(R.layout.fragment_chats, group, false);
    recyclerview = view.findViewById(R.id.rview);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("message");
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            list = new ArrayList<>();
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                UserDetails userdetails = dataSnapshot1.getValue(UserDetails.class);
                Listdata listdata = new Listdata();
                String name = userdetails.getName();
                String message = userdetails.getMessage();
                listdata.setName(name);
                listdata.setMessage(message);
                list.add(listdata);

            }

            RecyclerviewAdapter recycler = new RecyclerviewAdapter(list);
            RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(getContext());
            recyclerview.setLayoutManager(layoutmanager);
            recyclerview.setItemAnimator(new DefaultItemAnimator());
            recyclerview.setAdapter(recycler);

        }

        @Override
        public void onCancelled(DatabaseError error) {

        }
    });

    return view;
}
}

我有一个带有名称列表的回收者CardView。由firebase数据库发送的名称。当我两次发送相同的名称时,它显示计数2.我想检查已经列出的名称。如果名称已在RecyclerView中,我想显示计数2.如何查看名单并显示计数。

2 个答案:

答案 0 :(得分:1)

这是你的答案:

- hosts: localhost
  connection: local
  gather_facts: false
  pre_tasks:
    - assert:
        that: srv_mode in ['start', 'restart']

代替“2”,您可以编码以增加计数。我认为消息是一个计数

答案 1 :(得分:0)

公共类ChatsFragment扩展Fragment {

FirebaseDatabase database;
DatabaseReference myRef;
List<ListData> list;
RecyclerView recyclerview;
HashMap<String,String> hashMap;


public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle b) {
    View view = inflater.inflate(R.layout.fragment_chats, group, false);
    recyclerview = view.findViewById(R.id.rview);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("message");
    hashMap = new HashMap<>();
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            list = new ArrayList<>();
                       for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                UserDetails userdetails = dataSnapshot1.getValue(UserDetails.class);
                String name = userdetails.getName();
                String message = userdetails.getMessage();

                if (hashMap.containsKey(name)) {
                    if (!hashMap.get(name).equalsIgnoreCase(message)) {

                        hashMap.remove(name);
                        hashMap.put(name + "-2", message);
                    }
                } else
                    hashMap.put(name, message);

            }

            list = convertHashToObjectList(hashMap);
            RecyclerViewAdapter recycler = new RecyclerViewAdapter(list);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
            recyclerview.setLayoutManager(layoutManager);
            recyclerview.setItemAnimator(new DefaultItemAnimator());
            recyclerview.setAdapter(recycler);

        }

        @Override
        public void onCancelled(DatabaseError error) {

        }
    });

    return view;
}
private ArrayList<ListData> convertHashToObjectList(HashMap<String, String> chatHashmap){

    ArrayList<ListData> listData = new ArrayList<>();
    for(Map.Entry<String, String> entry: chatHashmap.entrySet()) {
        listData.add(new ListData(entry.getKey(),entry.getValue()));
    }

    return listData;

}

}

这是@ankit给出的答案。谢谢你的解决方案。