簡體   English   中英

如何從Firebase數據庫讀取並將信息存儲在列表視圖中? Android的

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

我有一個試圖從Firebase數據庫讀取的所有食譜活動的視圖。

然而,

我不斷收到null異常錯誤(請參見下面的錯誤代碼)。

我想嘗試的是讀取數據並將其存儲在listview中。 該數據采用四個字符串的形式:名稱,配方描述,成分列表和方法。

有人可以協助嗎?

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();
        }
    }
}

錯誤代碼:

 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)

有人可以幫忙嗎?

干杯。

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

這個錯誤是當json數據不適合您的模型時

檢查此代碼

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

這是您的問題,請參閱返回的Json數據

這是一個更清潔的解決方案:

    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);

簡單的修復可能要花更長的時間。

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

^我需要添加此數據庫的實例和引用。

固定。 不再崩潰。 firebase數據庫中的所有信息現在都可以很好地顯示。

我希望這可以幫助其他嘗試使用列表視圖在單獨活動中顯示信息的用戶!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM