2017-03-22 44 views
0

我有一個查看嘗試從Firebase數據庫讀取的所有食譜活動。如何從Firebase數據庫讀取信息並將信息存儲在列表視圖中? Android

但是,

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

我想嘗試的是讀取數據並將其存儲在列表視圖中。該數據採用四種字符串形式:名稱,配方說明,成分列表和方法。

任何人都可以協助嗎?

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) 

有人能幫忙嗎?

乾杯。

回答

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

這埃羅是當JSON數據不適合模型

檢查這個代碼

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

這裏是你的問題,請與返回的JSON數據

0

這裏有一個清潔的解決方案:

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

我不確定這是什麼意思。 – Korey

+0

FirebaseListAdapter帶有Firebase庫,可讓您輕鬆填充ListView:http://stackoverflow.com/questions/37580991/how-to-use-firebase-list-adapter –

0

簡單的修復可能需要比它應該更長的時間。

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

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

固定。沒有更多的崩潰。 Firebase數據庫中的所有信息現在都可以很好地顯示。

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

相關問題