将数据从云Firestore传递到ListView



我正试图从Cloud Firestore获得我的数据并将其传递到ListView。目前,我的代码需要将总线名称传递给ArrayList,然后传递给ListView。

MainActivity.java

public class busTimetable extends AppCompatActivity {
private static final String TAG = "busTimetable";
FirebaseFirestore fStore;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ongoing_bus);
fStore = FirebaseFirestore.getInstance();
lv = (ListView) findViewById(R.id.listView);
lv.setEmptyView(findViewById(R.id.empty));
//ArrayList<String> arrayList = new ArrayList<String>();

foo(new Callback() {
@Override
public void myResponseCallback(ArrayList<String> result) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
result);
lv.setAdapter(arrayAdapter);
}
});
}
interface Callback {
void myResponseCallback(ArrayList<String> result);//whatever your return type is: string, integer, etc.
}
public void foo(final Callback callback) {
fStore.collection("drivers")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
String busName = document.getString("fName");
boolean ongoing = document.getBoolean("ongoing");
Log.d(TAG, "SUCCESSFULL GET USER DATA");
ArrayList<String> arrayList = new ArrayList<String>();
if (ongoing) {
arrayList.add(busName);
callback.myResponseCallback(arrayList);
}
}
}
}
});

}

}

ongoing_bus.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/no_results" />
</LinearLayout>

Firestore数据库结构

我的数据库结构截图

如果我没有添加代码arrayList.add("busName";

我的ListView将不显示结果,但是当我添加代码时,它将显示代码的结果,但是在它上面有文本busName。我希望显示没有&;busname &;的总线名称。文本。

下面是期望输出的示例

看一下下面的代码。onComplete方法中的代码稍后运行,所以当它运行时,你可以将接收到的数据添加到适配器本身,而不是数组,这会自动通知ListView它需要更新。


public class busTimetable extends AppCompatActivity {
private static final String TAG = "busTimetable";
FirebaseFirestore fStore;
private ListView lv;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ongoing_bus);
fStore = FirebaseFirestore.getInstance();
lv = (ListView) findViewById(R.id.listView);
lv.setEmptyView(findViewById(R.id.empty));

// Create an empty adapter - no items set yet
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1);
lv.setAdapter(adapter);

// call the async method - when it completes (some time in the future)
// it will add stuff to the adapter
fStore.collection("drivers")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {

ArrayList<String> arrayList = new ArrayList<String>();
for (QueryDocumentSnapshot document : task.getResult()) {
String busName = document.getString("fName");
boolean ongoing = document.getBoolean("ongoing");
Log.d(TAG, "SUCCESSFULL GET USER DATA");

if (ongoing) {
arrayList.add(busName);
}
}

adapter.clear();
adapter.addAll(arrayList);
}
}
});
// This will always print "0" - it runs before the code above
// in the onComplete callback
Log.d(TAG, "Adapter size = " + adapter.getCount());
}

代码中没有任何内容表明要添加"busName"在列表的第0个位置。但是,如果像下面这样重构代码,您将得到将列表添加到回调的正确方法:

public void foo(final Callback callback) {
fStore.collection("drivers")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
ArrayList<String> arrayList = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
String busName = document.getString("fName");
boolean ongoing = document.getBoolean("ongoing");
Log.d(TAG, "SUCCESSFULL GET USER DATA");
if (ongoing) {
arrayList.add(busName);
}
}
callback.myResponseCallback(arrayList);
}
}
});

更改的内容:

  • 在循环之外创建了ArrayList。通过这种方式,您将创建一个实例,而不是在每次迭代中创建一个实例。
  • 从完全相同的原因获得了循环外回调的ArrayList的添加。

相关内容

  • 没有找到相关文章

最新更新