什么错过了,连接 Firestore 和 ListView 以获得样本中的随机结果? [英] What miss, with connect Firestore and ListView for random results in sample?
问题描述
我尝试将我的 Firestore
连接到 ListView
.我的 Query
有 20 条记录和 3 条随机记录,放入 ArrayList
(randomPlaceList
),然后我合乎逻辑地尝试包含这个 ArrayList
到我的自定义 adapter
.但我想念一些东西..
I try to connect my Firestore
to ListView
. With my Query
there are put 20 records and 3 random of it, put in ArrayList
(randomPlaceList
), then I logical try to include this ArrayList
to my custom adapter
. But I miss something..
编辑了一些变化变量;现在我有这个错误:
EDITED with some change variables; Now I have this error:
进程:com.example.arara.myapplication,PID:10283java.lang.IndexOutOfBoundsException:索引:4,大小:0在 java.util.ArrayList.get(ArrayList.java:437)在 com.example.arara.myapplication.MainActivity$1.onComplete(MainActivity.java:46)
Process: com.example.arara.myapplication, PID: 10283 java.lang.IndexOutOfBoundsException: Index: 4, Size: 0 at java.util.ArrayList.get(ArrayList.java:437) at com.example.arara.myapplication.MainActivity$1.onComplete(MainActivity.java:46)
在这一行:
Peoples item = randomPlaceList.get(randomIndex);
ListViewPlaces 类:
ListViewPlaces class:
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);
System.out.println(peoples);
peoplesList.add(peoples);
}
if (peoplesList.size() > 0) {
System.out.println(peoplesList);
int placeCount = peoplesList.size();
Random randomGenerator = new Random();
List<Peoples> randomPlaceList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
int randomIndex = randomGenerator.nextInt(placeCount);;
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;
}
}
和模型类:
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 数据库:
Firestore database:
推荐答案
这是将 Cloud Firestore 中的数据显示到ListView"中的方法使用自定义的ArrayAdapter"在 Android 上.
This is how you can display data from a Cloud Firestore into a "ListView" using a custom "ArrayAdapter" on Android.
首先,要向数据库中添加数据,您应该使用以下几行代码:
First of all, to add data to the database, you should use the following lines of code:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference placesRef = rootRef.collection("places");
Places places = new Places("PlaceOne", "ImageOne");
placesRef.document().set(places);
通过这种方式,您可以添加任意数量的地点.因此,您将拥有一个如下所示的数据库结构:
In this way, you can add as many places as you want. So, you'll have a database structure that will look like this:
Firestore-root
|
--- places (collection)
|
--- placeIdOne (document)
| |
| --- image: "ImageOne"
| |
| --- name: "NameOne"
|
--- placeIdTwo (document)
| |
| --- image: "ImageTwo"
| |
| --- name: "NameTwo"
|
--- //And so on
为了更清楚,请看下图:
To be more clear, please see the image below:
如果要查询数据,需要使用Query
对象.
If you want to query the data, you need to use a Query
object.
Query query = placesRef.whereEqualTo("name", "NameOne");
但是因为我在数据库中只有几条记录,所以我将只使用placesRef
CollectionReference.
But because I only have a few records in the database, I will use only the placesRef
CollectionReference.
假设您已经有一个ListView"在您的 .XML 文件中,如下所示:
Assuming you already have a "ListView" in your .XML file that looks like this:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/place_list"/>
要显示数据,首先需要创建一个适配器类.这个类应该是这样的:
To display the data, first you need to create an adapter class. This class should look like this:
public class PlacesAdapter extends ArrayAdapter<Places> {
public PlacesAdapter(Context context, List<Places> 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);
}
Places places = getItem(position);
String name = places.getName();
((TextView) listItemView).setText(name);
return listItemView;
}
}
然后在您的 onCreate()
方法中使用以下代码:
Then in your onCreate()
method use the following code:
ListView mListView = (ListView) findViewById(R.id.place_list);
PlacesAdapter placesAdapter = new PlacesAdapter(getApplicationContext(), randomPlaceList);
mListView.setAdapter(placesAdapter);
然后立即获取数据并将更改通知适配器:
And right after that, get the data and notify the adapter about the changes:
placesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<Places> placesList = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
Places places = document.toObject(Places.class);
placesList.add(places);
}
int placeCount = placesList.size();
Random randomGenerator = new Random();
List<Places> randomPlaceList = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
randomPlaceList.add(placesList.get(randomGenerator.nextInt(placeCount)));
}
placesAdapter.notifyDataSetChanged();
}
}
});
每次启动应用时,ListView 中的结果将是 3 个随机位置.
The result in your ListView will be, 3 random places each time you start your app.
这篇关于什么错过了,连接 Firestore 和 ListView 以获得样本中的随机结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!