【问题标题】:How can I read from a firebase database and store the information within a list view? Android如何从 firebase 数据库中读取信息并将信息存储在列表视图中?安卓
【发布时间】:2017-03-22 22:25:37
【问题描述】:

我查看了尝试从 firebase 数据库读取的所有食谱活动。

然而,

我不断收到空异常错误(请参阅下面的错误代码)。

我想尝试的是读取数据并将其存储在列表视图中。该数据采用四个字符串的形式:名称、配方描述、配料表和方法。

有人可以帮忙吗?

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)

有人可以帮忙吗?

干杯。

【问题讨论】:

    标签: android


    【解决方案1】:
    convert object of type java.lang.String to type com.example.korey.puregymapplication.recipe
    

    这个错误是 json 数据不适合你的模型

    检查此代码

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

    这是您的问题,请查看您返回的 Json 数据

    【讨论】:

      【解决方案2】:

      这是一个更清洁的解决方案:

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

      【讨论】:

      【解决方案3】:

      简单的修复可能花费的时间比预期的要长。

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

      ^我需要添加这个数据库的一个实例和一个引用。

      已修复。不再崩溃。 firebase 数据库中的所有信息现在都可以很好地显示。

      我希望这可以帮助其他尝试使用列表视图在单独的活动中显示信息的用户!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-06
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多