gpt4 book ai didi

java - 如何使用 Firebase 数据填充 Spinner?

转载 作者:行者123 更新时间:2023-12-02 11:34:30 25 4
gpt4 key购买 nike

我已经浏览了有关 SO 主题的在线文章,试图找出如何做到这一点,但还没有更接近。

此线程 ( How to get a String List from Firebase to fill a Spinner ) 看起来可能很有用,但我不确定如何针对我自己的场景实现它。

我希望属性地址出现在微调器中。

如果问题看起来含糊不清,很难解释,我深表歉意。

维护 Activity

public class MaintenanceActivity extends AppCompatActivity {

EditText editTextTitle;
EditText editTextDesc;
Spinner spinnerPrimary;
Spinner spinnerSecondary;
Spinner spinnerProperty;
Button buttonSubmit;

DatabaseReference databaseMaintenance;

ListView listViewIssues;

List<Maintenance> maintenanceList;

fDatabaseRoot.child("propertyAddress").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Is better to use a List, because you don't know the size
// of the iterator returned by dataSnapshot.getChildren() to
// initialize the array
final List<String> propertyAddress = new ArrayList<String>();

for (DataSnapshot addressSnapshot: dataSnapshot.getChildren()) {
String propertyAddress = addressSnapshot.child("propertyAddress").getValue(String.class);
propertyAddress.add(propertyAddress);
}

Spinner spinnerProperty = (Spinner) findViewById(R.id.spinnerProperty);
ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(MaintenanceActivity.this, android.R.layout.simple_spinner_item, areas);
addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProperty.setAdapter(addressAdapter);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

Activity 维护

<Spinner
android:id="@+id/spinnerProperty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/fuel" />

Firebase 数据库

Firebase

错误

这个答案有几个错误。我以前遇到过这些错误,不知道如何解决。

Errors

LogCat 错误

    03-01 17:19:53.402 28517-28517/com.example.***.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.***.myapplication, PID: 28517
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:493)
at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:994)
at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:990)
at android.widget.Spinner.measureContentWidth(Spinner.java:856)
at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1225)
at android.widget.Spinner$DropdownPopup.show(Spinner.java:1251)
at android.widget.Spinner.performClick(Spinner.java:780)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:442)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
03-01 17:19:53.403 28517-28517/com.example.***.myapplication E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:493)
at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:994)
at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:990)
at android.widget.Spinner.measureContentWidth(Spinner.java:856)
at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1225)
at android.widget.Spinner$DropdownPopup.show(Spinner.java:1251)
at android.widget.Spinner.performClick(Spinner.java:780)
at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:442)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

最佳答案

我可以为您提供很多帮助,但您需要配置数据库和 Firebase,请参阅 docs .

之后在您的 Activity 中:

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference fDatabaseRoot = database.getReference();

fDatabaseRoot.child("properties").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Is better to use a List, because you don't know the size
// of the iterator returned by dataSnapshot.getChildren() to
// initialize the array
final List<String> propertyAddressList = new ArrayList<String>();

for (DataSnapshot addressSnapshot: dataSnapshot.getChildren()) {
String propertyAddress = addressSnapshot.child("propertyAddress").getValue(String.class);
if (propertyAddress!=null){
propertyAddressList.add(propertyAddress);
}
}

Spinner spinnerProperty = (Spinner) findViewById(R.id.maintenanceProperty);
ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(MaintenanceActivity.this, android.R.layout.simple_spinner_item, propertyAddressList);
addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProperty.setAdapter(addressAdapter);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

您已经为 propertyAddress 添加了一个监听器,但是当数据快照返回时,您再次探测 propertyAddress,这不起作用,因为监听器已经返回属性地址。我上面写的方式将返回数据库中的所有键,在每个键中都有一个可以检索的 propertyAddress。尝试一下。

关于java - 如何使用 Firebase 数据填充 Spinner?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49053155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com