为什么我的回收商视图没有显示正确的数据?

时间:2018-04-27 03:55:37

标签: java android

我遇到的问题是我的回收站视图仅显示虚拟数据(请参阅参考资料)

fUser.add(new firebaseUser("Nate", "f"));
fUser.add(new firebaseUser("Nate", "f"));
fUser.add(new firebaseUser("Nate", "f"));
fUser.add(new firebaseUser("Nate", "f"));

而不是任何真实数据。你可以看到我是如何查询firebase的,我已经检查过以确保它正在获取数据。我似乎无法弄清楚为什么回收者视图只显示虚拟数据而不是组合数据?

public class admin_viewuser extends AppCompatActivity {

    //ArrayList<Contact> contacts;
    ArrayList<firebaseUser> fUser;
    private FirebaseFirestore FireStore;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_viewuser);

        fUser = new ArrayList<>();
        FireStore = FirebaseFirestore.getInstance();

        query();
        fUser.add(new firebaseUser("Nate", "f"));
        fUser.add(new firebaseUser("Nate", "f"));
        fUser.add(new firebaseUser("Nate", "f"));
        fUser.add(new firebaseUser("Nate", "f"));





    }


    public void query() {
        FireStore
                .collection("groupss")
                .document("Lpux0m1ZmAHgiulBbvOZ") /* for testing would be users uid in real life */
                .collection("users")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        List<DocumentSnapshot> data = queryDocumentSnapshots.getDocuments();
                        for (DocumentSnapshot item : data) {
                            System.out.println(item.getData());
                            fUser.add(new firebaseUser(item));
                        }

                    }
                });
        System.out.println("FINISHED");
        addAdapter();


    }

    public void addAdapter() {
        System.out.println("CALLED NOW");
        RecyclerView rvContacts = findViewById(R.id.rvContacts);


        //contacts = Contact.createContactsList(20);
        ContactsAdapter adapter = new ContactsAdapter(fUser);

        rvContacts.setAdapter(adapter);

        rvContacts.setLayoutManager(new LinearLayoutManager(this));
    }
}

class firebaseUser {
    private String uName;
    private String deleteID; //pos

    public firebaseUser(DocumentSnapshot data) {
        uName = data.get("user_name").toString();
        deleteID = data.get("deleteID").toString();
    }

    public firebaseUser(String name, String id) {
        uName = name;
        deleteID = id;
    }

    public String getName() {
        return uName;
    }

    public String getDeleteID() {
        return deleteID;
    }
}


class ContactsAdapter extends
        RecyclerView.Adapter<ContactsAdapter.ViewHolder> {


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView nameTextView;
        public Button messageButton;


        public ViewHolder(View itemView) {

            super(itemView);

            nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
            messageButton = (Button) itemView.findViewById(R.id.message_button);
        }
    }

    private List<firebaseUser> mContacts;

    public ContactsAdapter(List<firebaseUser> contacts) {
        mContacts = contacts;
    }

    @Override
    public ContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View contactView = inflater.inflate(R.layout.item_contact, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(contactView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder, int position) {
        firebaseUser contact = mContacts.get(position);
        System.out.println(contact);
        System.out.println("CALLED1");
        TextView textView = viewHolder.nameTextView;
        textView.setText(contact.getName());
        Button button = viewHolder.messageButton;
        button.setText("Kick User");


    }

    // Returns the total count of items in the list
    @Override
    public int getItemCount() {
        return mContacts.size();
    }
}

1 个答案:

答案 0 :(得分:0)

您所做的是,您尝试在提取完成之前加载数据。这是异步的。所以如果你像这样放置addAdapter(),它就会起作用。

public void query() {
    FireStore
            .collection("groupss")
            .document("Lpux0m1ZmAHgiulBbvOZ") /* for testing would be users uid in real life */
            .collection("users")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                    List<DocumentSnapshot> data = queryDocumentSnapshots.getDocuments();
                    for (DocumentSnapshot item : data) {
                        System.out.println(item.getData());
                        fUser.add(new firebaseUser(item));
                        addAdapter();
                    }

                }
            });
    System.out.println("FINISHED");



}