无法尝试将查询中的随机结果带到Firestore

时间:2018-05-13 05:39:34

标签: java android firebase listview android-arrayadapter

我在Firestore上有一个数据库,有一些数据。有一个ListView,我在其中放置3个随机记录,我尝试将所有记录放入数据库中,我在查询中请求。但是在启动时,listview是空的。有什么问题?

我的主要课程:

public class MainActivity extends AppCompatActivity {

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference placeRef = rootRef.collection("Peoples");

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

    placeRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                List<Peoples> peoplesList = new ArrayList<>();
                for (DocumentSnapshot document : task.getResult()) {
                    Peoples peoples = document.toObject(Peoples.class);
                    peoplesList.add(peoples);
                }
                if (peoplesList.size() > 0) {
                    int placeCount = peoplesList.size();
                    Random randomGenerator = new Random();
                    List<Peoples> randomPlaceList = new ArrayList<>();
                    for (int i = 0; i < 3; i++) {
                        int randomIndex = randomGenerator.nextInt(randomPlaceList.size());
                        Peoples item = randomPlaceList.get(randomIndex);
                        randomPlaceList.add(item);
                    }
                    ListView mListView = findViewById(R.id.place_list);
                    PeoplesAdapter peoplesAdapter = new PeoplesAdapter(getBaseContext(), randomPlaceList);
                    mListView.setAdapter(peoplesAdapter);
                }
            }
        }
    });
}

}

适配器类:

public class PeoplesAdapter extends ArrayAdapter<Peoples> {
    public PeoplesAdapter(Context context, List<Peoples> list) {
        super(context, 0, list);
    }

    @NonNull
    @Override
    public View getView(int position, View listItemView, @NonNull ViewGroup parent) {
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        }
        Peoples peoples = getItem(position);
        Log.d("TAG", peoples.getName());
        String name = peoples.getName();
        ((TextView) listItemView).setText(name);

        return listItemView;
    }
}

和Model类:

class Peoples {
private String name, age;

public Peoples() {}

public Peoples(String name, String age) {
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

public String getAge() {
    return age;
}

}

我的Firestore数据库示例:

enter image description here

有一段时间它的工作,但只有一个......

1 个答案:

答案 0 :(得分:1)

问题在于:

int randomIndex = randomGenerator.nextInt(randomPlaceList.size());

Random::nextInt(0)会抛出异常。

当您将randomPlaceList初始化为

List<Peoples> randomPlaceList = new ArrayList<>(); // size = 0

所以我认为正确的随机函数应该是:

int randomIndex = randomGenerator.nextInt(placeCount); // placeCount = peopleList.size()
Peoples item = peoplesList.get(randomIndex);