列表视图不会使用Firebase填充

时间:2018-04-26 04:32:07

标签: java android

填充列表视图时遇到问题。我想我知道是什么问题,因为我多次测试它,似乎检索我的数据的路径实际上是用户存储而不是赌注,但我似乎没有得到路径来自哪里。我回头看看这些方法,看起来路径就是DatabaseReference,尽管我在类中的引用只是标准的

mDatabase = FirebaseDatabase.getInstance().getReference(); 

这是我的适配器

public class CustomAdapter extends BaseAdapter{
    Context c;
    ArrayList<Bets> bets;
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); //Getting currently logged user
    DatabaseReference mDatabase;
    String hours;
    String samount;


    public CustomAdapter(Context c, ArrayList<Bets> bets) {
        this.c = c;
        this.bets = bets;
    }

    @Override
    public int getCount() {
        return bets.size();
    }

    @Override
    public Object getItem(int pos) {
        return bets.get(pos);
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if(view == null)
        {
            view = LayoutInflater.from(c).inflate(R.layout.item_layout,viewGroup,false);
        }

        TextView condition = (TextView) view.findViewById(R.id.conditionList);
        TextView place = (TextView) view.findViewById(R.id.placeList);
        TextView amount = (TextView) view.findViewById(R.id.amountList);

        final Bets bet = (Bets) this.getItem(position);

        condition.setText(bet.getCondition());
        place.setText(bet.getPlace());
        String setamount = String.valueOf(bet.getAmount());
        amount.setText(setamount);
        hours = String.valueOf(bet.getHours());
        samount = String.valueOf(bet.getAmount());


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                openDetailList(bet.getCreator(),bet.getCondition(),bet.getPlace(),hours,samount,bet.getJoined(),bet.getCreatorUid(),bet.getJoinedUid());


            }
        });
        return view;
    }



    public void openDetailList(String...details)
    {

        Intent i = new Intent(c, ListDetail.class);
        i.putExtra("CREATOR_KEY",details[0]);
        i.putExtra("CONDITION_KEY",details[1]);
        i.putExtra("PLACE_KEY",details[2]);
        i.putExtra("HOURS_KEY",details[3]);
        i.putExtra("AMOUNT_KEY",details[4]);
        i.putExtra("JOINED_KEY",details[5]);
        i.putExtra("CREATORUID_KEY",details[6]);
        i.putExtra("JOINEDUID_KEY",details[7]);

        c.startActivity(i);

    }
}

这是我的FirebaseHelper

public class FirebaseHelper {

    DatabaseReference db;
    Boolean saved = null;
    ArrayList<Bets> bets = new ArrayList<>();


    public FirebaseHelper(DatabaseReference db) {
        this.db = db;
    }


    public Boolean save(Bets bets)
    {
        if(bets == null)
        {

            saved = false;
        }
        else {

            try {
                db.child("bets").push().setValue(bets);
                saved = true;
            } catch (DatabaseException e) {

                e.printStackTrace();
                saved = false;
            }


        }


        return saved;
    }



    private void fetchData(DataSnapshot dataSnapshot)
    {

        bets.clear();

        for(DataSnapshot snapshot: dataSnapshot.getChildren())
        {
            Bets bet = snapshot.getValue(Bets.class);
            bets.add(bet);

        }


    }


    public ArrayList<Bets> retrieve()
    {
        db.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                 fetchData(dataSnapshot);
            }

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

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, "onComplete: Failed=" + databaseError);
            }
        });



        return bets;





    }


}

保存/插入数据的效果非常好,因为我添加了一个赌注后,我甚至会有一个示例数据显示,但它没有填充数据。

{
  "bets" :
 {
    "-LAzYM9SA-8Sg0YgGqnv" : 
      {
      "amount" : 10,

             "condition" : "It will be sunny",

              "creator" : "Ginart",

             "creatorUid" : "MxQPvCjUIkahVSZk1y2stdCxeY32",

              "hours" : 2,

              "joined" : "Free",

              "joinedUid" : "",

               "place" : "New York"
    }

出于某种原因,我认为它会读取此处的用户数据

"users" : {
    "EbYtfLUPs7Vu2rvnExaOaqJ4J883" : {
      "balance" : 5,
      "bets" : 1
    },

每次添加新用户时,列表视图都会在我的列表中添加一个项目,但我的模板根本没有数据,Android Studio会向我显示

W/ClassMapper: No setter/field for balance found on class Models.Bets
W/ClassMapper: No setter/field for bets found on class Models.Bets

所以我认为这些路径一定是错的。我的投注模型没有平衡和投注变量。

这是我的OnCreate方法,我在firebase中使用retrieve()方法

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logged_in);



Money = (EditText) findViewById(R.id.Balance);
User = (EditText) findViewById(R.id.Player);
betList = (ListView) findViewById(R.id.BetList);
addbet = (FloatingActionButton) findViewById(R.id.AddBet);
addcreds = (TextView) findViewById(R.id.addCredits);
Money.setInputType(0);
User.setInputType(0);

betting = FirebaseDatabase.getInstance().getReference();
mDatabase = FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(betting);

mDatabase.child("users").child(current.getUid().toString()).child("balance").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this location is updated.
        balance = dataSnapshot.getValue(Long.class);
        String b = String.valueOf(balance);
        Money.setText(b);

    }

    @Override
    public void onCancelled(DatabaseError error) {
        Money.setText("Error");

    }
});

mDatabase.child("users").child(current.getUid().toString()).child("bets").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this location is updated.
        bets = dataSnapshot.getValue(Long.class);

    }

    @Override
    public void onCancelled(DatabaseError error) {


    }
});

User.setText(current.getDisplayName().toString());


//ADAPTER
adapter = new CustomAdapter(this,helper.retrieve());
betList.setAdapter(adapter);

addbet.setOnClickListener(this);
addcreds.setOnClickListener(this);




}

此外,每当我试图改变我对

的引用时
betting = FirebaseDatabase.getInstance().getReference().child("bets");

我收到此错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: app.betme.betme, PID: 22814
                  com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type Models.Bets
                      at com.google.android.gms.internal.zg.zzb(Unknown Source)
                      at com.google.android.gms.internal.zg.zza(Unknown Source)
                      at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                      at Utils.FirebaseHelper.fetchData(FirebaseHelper.java:67)
                      at Utils.FirebaseHelper.access$000(FirebaseHelper.java:21)
                      at Utils.FirebaseHelper$1.onChildAdded(FirebaseHelper.java:81)
                      at com.google.android.gms.internal.px.zza(Unknown Source)
                      at com.google.android.gms.internal.vj.zzHX(Unknown Source)
                      at com.google.android.gms.internal.vp.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 :(得分:0)

这不是错误,只是来自Firebase,你应该有方法为balancebets设置数据