阵列适配器崩溃

时间:2018-12-16 19:17:45

标签: java arrays firebase android-studio firebase-realtime-database

我正在使用抽签卡创建一个应用程序。它应该从Bookmakers Firebase表中读取数据,但是当我启动它时,它会使应用程序崩溃。

下面引用的3个Java的代码都在下面,但是没有错误,我无法确定它崩溃的原因。

BETSWIPE.java

toString()

ArryayApadpter2.JAVA

package com.example.swoddz.swoddz;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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 com.lorentzos.flingswipe.SwipeFlingAdapterView;

import java.util.ArrayList;
import java.util.List;

public class BetSwipe extends AppCompatActivity {

    private ArrayList<String> al;
    private ArrayAdapter<String> arrayAdapter;
    private int i;

    private FirebaseAuth mAuth;


    List<cards2> rowItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //add the view via xml or programmatically

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bet_swipe);


        al = new ArrayList<>();
        al.add("php");


        arrayAdapter = new ArrayAdapter<>(this, R.layout.item2, R.id.helloText, al );


        rowItems = new ArrayList<cards2>();

        SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);


        flingContainer.setAdapter(arrayAdapter);
        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                // this is the simplest way to delete an object from the Adapter (/AdapterView)
                Log.d("LIST", "removed object!");
                rowItems.remove(0);
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
                //Do something on the left!
                //You also have access to the original object.
                //If you want to use it just cast it (String) dataObject
                Toast.makeText(BetSwipe.this, "Left", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                Toast.makeText(BetSwipe.this, "Right", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                al.add("XML ".concat(String.valueOf(i)));
                arrayAdapter.notifyDataSetChanged();
                Log.d("LIST", "notified");
                i++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
            }
        });


        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {
                Toast.makeText(BetSwipe.this, "clicked!", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void checkBookmakers() {
        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        DatabaseReference BookmakerDB = FirebaseDatabase.getInstance().getReference().child("Football-Bets");
        BookmakerDB.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                if (dataSnapshot.exists()){
                    al.add(dataSnapshot.child("Bookmaker").getValue().toString());
                }
            }

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

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

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

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

    }

}

CARDS2.Java

package com.example.swoddz.swoddz;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by manel on 9/5/2017.
 */

public class arrayAdapter2 extends ArrayAdapter<cards>{

    Context context;

    public arrayAdapter2(Context context, int resourceId, List<cards> items){
        super(context, resourceId, items);
    }
    public View getView(int position, View convertView, ViewGroup parent){
        cards card_item = getItem(position);

        if (convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
        }

        TextView Bookmaker = (TextView) convertView.findViewById(R.id.name);
        ImageView image = (ImageView) convertView.findViewById(R.id.image);

        Bookmaker.setText(card_item.getName());
     //   image.setImageResource(R.mipmap.ic_launcher);

        return convertView;

    }
}

堆栈跟踪

package com.example.swoddz.swoddz;

public class cards2 {
    private String userId;
    private String Bookmaker;
    public cards2(String userId, String name){
        this.userId = userId;
        this.Bookmaker = Bookmaker;

    }

    public String getUserId(){
        return userId;
    }
    public void setUserID(String userID){
        this.userId = userId;
    }

    public String getBookmaker(){
        return Bookmaker;
    }
    public void setBookmaker(String Bookmaker){
        this.Bookmaker = Bookmaker;
    }


}

数据库结构

enter image description here

0 个答案:

没有答案