firebase数据回溯运行时错误

时间:2018-03-23 10:53:58

标签: android listview firebase runtime

我想从firebase检索数据,并希望在我的项目的listview上显示它 以下是代码:
Overview.java

package com.example.kishan.expense_fragment;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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 com.google.firebase.database.ValueEventListener;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
*/
public class Overview extends Fragment {

private ListView listView;
List<Overview_ListView> listOverview;
Overview_List_Adapter adapterOverview;
TextView total;
FirebaseDatabase database;
DatabaseReference myRef ;
//FloatingActionButton fab;

public Overview() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_overview, container, false);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("Expense");

   // fab=(FloatingActionButton)view.findViewById(R.id.menu_item);
    listView = (ListView) view.findViewById(R.id.list_viewAll);
    total = (TextView)view.findViewById(R.id.txtAmountOverview);
    Overview_ListView overList = null;
    final List<Overview_ListView> listOverview = new ArrayList<>();

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren())
            {
                Overview_ListView value = dataSnapshot1.getValue(Overview_ListView.class);
                String category = value.getCategory();
                int amount = value.getAmount();
                String  date = value.getDate();
                String  note=value.getNote();
                String key=value.getKey();
                Toast.makeText(getContext(),category+amount+date+note,Toast.LENGTH_LONG).show();
                Overview_ListView ol=new Overview_ListView(key,category,amount,date,note);
                listOverview.add(ol);

            }
    adapterOverview = new Overview_List_Adapter(getContext(), listOverview);
    listView.setAdapter(adapterOverview);

        }

        @Override
        public void onCancelled(DatabaseError error)
        {
            Log.w("Hello", "Failed to read value.", error.toException());
        }
    });
    return view;
}

Overview_List_Adapter.java

package com.example.kishan.expense_fragment;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.List;


public class Overview_List_Adapter extends BaseAdapter {
Context context;
List<Overview_ListView> listOverview;

public Overview_List_Adapter(Context context, List<Overview_ListView> listOverview)
{
    this.context = context;
    this.listOverview = listOverview;
}
@Override
public int getCount() {
    return listOverview.size();
}

@Override
public Object getItem(int position) {
    return listOverview.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = View.inflate(context, R.layout.overview_list, null);
    TextView txtName = (TextView) view.findViewById(R.id.txtOverview_name);
    TextView txtAmount = (TextView) view.findViewById(R.id.txtOverview_amount);
    ProgressBar progressBar = (ProgressBar)view.findViewById(R.id.progressBar);

    txtName.setText(listOverview.get(position).getCategory());
    DecimalFormat precision = new DecimalFormat("0.00");
    txtAmount.setText(precision.format(listOverview.get(position).getAmount()) + " Rs");
    progressBar.setProgress(txtAmount.getAutoLinkMask());
    progressBar.setProgress(0);
    progressBar.setMax(100);

    return view;
}
}

Overview_Listview.java

package com.example.kishan.expense_fragment;


public class Overview_ListView {

String category;
int amount;
String date;
String note;
String key;


public Overview_ListView (String key,String category, int amount, String date, String note)
{
    this.key=key;
    this.category = category;
    this.amount = amount;
    this.date = date;
    this.note = note;

}

public String getKey()
{
    return key;
}

public void setKey(String key)
{
    this.key=key;
}


public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public int getAmount() {
    return amount;
}

public void setAmount(int amount) {
    this.amount = amount;
}

public String getDate() { return date; }

public void setDate(String date) { this.date = date; }

public String getNote() { return note;}

public void setNote(String note) { this.note = note; }
}

火力地堡: [![在此处输入图像说明] [1]] [1]

应用程序在运行时继续崩溃,当我绕过firebase的检索代码时,应用程序正常运行而不会崩溃。

使用firebase正确配置Gradle

logcat的:

 03-23 16:52:04.729 18308-18308/com.example.kishan.expense_fragment E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.kishan.expense_fragment, PID: 18308
                                                                                 com.google.firebase.database.DatabaseException: Class com.example.kishan.expense_fragment.Overview_ListView is missing a constructor with no arguments
                                                                                     at com.google.android.gms.internal.zzanp$zza.zze(Unknown Source)
                                                                                     at com.google.android.gms.internal.zzanp$zza.zzcg(Unknown Source)
                                                                                     at com.google.android.gms.internal.zzanp.zze(Unknown Source)
                                                                                     at com.google.android.gms.internal.zzanp.zzb(Unknown Source)
                                                                                     at com.google.android.gms.internal.zzanp.zza(Unknown Source)
                                                                                     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                                                     at com.example.kishan.expense_fragment.Overview$1.onDataChange(Overview.java:66)
                                                                                     at com.google.android.gms.internal.zzakg.zza(Unknown Source)
                                                                                     at com.google.android.gms.internal.zzalg.zzcxk(Unknown Source)
                                                                                     at com.google.android.gms.internal.zzalj$1.run(Unknown Source)
                                                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:154)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 个答案:

答案 0 :(得分:1)

要在string中显示Firebase数据库中的数据,您需要使用适配器。所以请使用以下代码:

Span<char>

另外,不要忘记在模型类中添加ListView构造函数,如下所示:

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<Overview_ListView> listOverview = new ArrayList<>();
        for(DataSnapshot ds :dataSnapshot.getChildren()) {
            Overview_ListView value = ds.getValue(Overview_ListView.class);
            listOverview.add(ol);
        }
        ArrayAdapter<Overview_ListView> arrayAdapter = new ArrayAdapter<Overview_ListView>(this, android.R.layout.simple_list_item_1, listOverview);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError error) {
        Log.w("Hello", "Failed to read value.", error.toException());
    }
});