Сбой адаптера массива - PullRequest
       2

Сбой адаптера массива

0 голосов
/ 16 декабря 2018

Я создаю приложение, используя swipecards.Предполагается, что он извлекает данные из таблицы Bookmakers Firebase, однако, когда я запускаю ее, приложение вылетает.

Код для Java-файла с 3 ссылками приведен ниже, но ошибок нет, и я не могу определитьпричина сбоя.

BETSWIPE.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) {
            }
        });

    }

}

ArryayApadpter2.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;

    }
}

CARDS2.Java

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


}

Трассировка стека

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.swoddz.swoddz, PID: 371
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.remove(ArrayList.java:477)
        at com.example.swoddz.swoddz.BetSwipe$1.removeFirstObjectInAdapter(BetSwipe.java:63)
        at com.lorentzos.flingswipe.SwipeFlingAdapterView$1.onCardExited(SwipeFlingAdapterView.java:223)
        at com.lorentzos.flingswipe.FlingCardListener$1.onAnimationEnd(FlingCardListener.java:256)
        at android.view.ViewPropertyAnimator$AnimatorEventListener.onAnimationEnd(ViewPropertyAnimator.java:1121)
        at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1149)
        at android.animation.ValueAnimator.doAnimationFrame(ValueAnimator.java:1309)
        at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:146)
        at android.animation.AnimationHandler.-wrap2(AnimationHandler.java)
        at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
        at android.view.Choreographer.doCallbacks(Choreographer.java:702)
        at android.view.Choreographer.doFrame(Choreographer.java:635)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
        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:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

Структура базы данных

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...