简体   繁体   English

如何从Firebase数据库读取并将信息存储在列表视图中? Android的

[英]How can I read from a firebase database and store the information within a list view? Android

I have a view all recipes activity that is trying to read from a firebase database. 我有一个试图从Firebase数据库读取的所有食谱活动的视图。

However, 然而,

I keep getting null exception errors (see error codes below). 我不断收到null异常错误(请参见下面的错误代码)。

What I want to try is to read data and store it within a listview. 我想尝试的是读取数据并将其存储在listview中。 This data is in the form of four strings: name, recipe description, ingredient list and method. 该数据采用四个字符串的形式:名称,配方描述,成分列表和方法。

Would anyone be able to assist? 有人可以协助吗?

JAVA JAVA

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.lang.reflect.Method;
import java.util.ArrayList;

import static com.example.korey.R.id.ingredients;

public class viewAllRecipes extends AppCompatActivity {




    FirebaseDatabase mFirebaseDatabase;
    DatabaseReference mMessagesDatabaseReference;
    ArrayList<String> recipes = new ArrayList<>();
    ListView recipeListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_recipes);
        recipeListView = (ListView) findViewById(R.id.recipeListView);

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mMessagesDatabaseReference = mFirebaseDatabase.getReference("recipeDetails");

showData();
    }
    private void showData() {
        mMessagesDatabaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            getUpdates(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });
    }

    public void getUpdates(DataSnapshot ds) {
        recipes.clear();

        for (DataSnapshot data : ds.getChildren()) {
            recipe r = new recipe();
            r.setName(data.getValue(recipe.class).getName());
            recipes.add(r.getName());


        }
        if (recipes.size()>0) {
            ArrayAdapter<String> adapter = new ArrayAdapter<>(viewAllRecipes.this,android.R.layout.simple_list_item_1,recipes);
            recipeListView.setAdapter(adapter);
        }
        else {
            Toast.makeText(viewAllRecipes.this, "No data", Toast.LENGTH_SHORT).show();
        }
    }
}

error code: 错误代码:

 FATAL EXCEPTION: main
                                                                                      Process: com.example.korey.puregymapplication, PID: 29093
                                                                                      com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.korey.puregymapplication.recipe
                                                                                          at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                                                                                          at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                          at com.example.korey.puregymapplication.viewAllRecipes.getUpdates(viewAllRecipes.java:78)
                                                                                          at com.example.korey.puregymapplication.viewAllRecipes$1.onChildAdded(viewAllRecipes.java:47)
                                                                                          at com.google.android.gms.internal.zzblz.zza(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                                          at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Can anybody help? 有人可以帮忙吗?

Cheers. 干杯。

convert object of type java.lang.String to type com.example.korey.puregymapplication.recipe

this erro is when json data dont fit your model 这个错误是当json数据不适合您的模型时

check this code 检查此代码

r.setName(data.getValue(recipe.class).getName());

here is your problem , see your returned Json data 这是您的问题,请参阅返回的Json数据

Here's a cleaner solution: 这是一个更清洁的解决方案:

    adapter = new FirebaseListAdapter<recipe>(viewAllRecipes.this, recipe.class,
            android.R.layout.simple_list_item_1,FirebaseDatabase.getInstance()
            .getReference().child("recipeDetails")) {
        @Override
        protected void populateView(View v, recipe model, int position) {
           TextView recipe =(TextView)v.findViewById(android.R.id.text1);
           tutorUser.setText(model.getName());
        }
    };

    recipeListView.setAdapter(adapter);

Simple fix that probably took longer than it should. 简单的修复可能要花更长的时间。

mMessagesDatabaseReference = mFirebaseDatabase.getInstance().getReference(); mMessagesDatabaseReference = mFirebaseDatabase.getInstance()。getReference();

^I required to add an instance of this database and a reference. ^我需要添加此数据库的实例和引用。

Fixed. 固定。 No more crashing. 不再崩溃。 ALL information within the firebase database is now displaying nicely. firebase数据库中的所有信息现在都可以很好地显示。

I hope this can help other users who are trying to display information in a separate activity using a list view! 我希望这可以帮助其他尝试使用列表视图在单独活动中显示信息的用户!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase数据库读取用户信息并在Android App中显示为列表 - How to a read user Information from Firebase database and display as a List in Android App 如何在React Native中从Firebase中的数据库访问信息并显示在 <Text> ? - How can I access information from a database in Firebase in React Native and display it within a <Text>? 我无法从android中的片段读取活动中的数据库信息 - I can't read database information in activity from fragment in android 如何查看我的Android Apps数据库信息? - How can I view my Android Apps database information? 如何在firebase数据库android中存储用户配置文件信息 - How to store user profile information in firebase database android 如何在Android上从Firebase列出用户信息? - How to list users information from firebase on android? 如何从Firebase实时数据库获取下一个孩子的数据并将其显示在列表视图中 - How I can get data of next child and show it in list view from firebase realtime database 如何从Firebase数据库读取数据到列表 - How to read data from Firebase database to a List 如何在 Android 的 firebase 数据库中存储 FireBase 云消息传递令牌? - How can I store FireBase Cloud Messaging tokens in firebase database in Android? 我如何从 EditText 中获取日期,然后将其存储在 Android 的数据库中 - How can i get the date from the EditText and then store it in database in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM