0

I am making a flash card app with SwipeCard

This is my FlashCardActivity:

import android.os.Bundle;
import android.text.style.TtsSpan;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;

import org.koreanlab.fabloading.R;
import org.koreanlab.fabloading.adapter.CardListAdapter;
import org.koreanlab.fabloading.basickit.BasicCompatActivity;
import org.koreanlab.fabloading.basickit.remote.RemoteService;
import org.koreanlab.fabloading.basickit.remote.ServiceGenerator;
import org.koreanlab.fabloading.item.CardItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FlashCardActivity extends BasicCompatActivity {
    private String TAG = getClass().getSimpleName();
    private ArrayList<CardItem> cardList;
    private ArrayAdapter<String> cardAdapter;
    private CardListAdapter cardListAdapter;

    private int i;
    CardItem newCard;

    @BindView(R.id.frame)
    SwipeFlingAdapterView flingContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flashcard);
        ButterKnife.bind(this);

        cardList = new ArrayList<>();
        Log.d(TAG, "cardList created");

        // get Two Words;
        cardList.add(getCardItem());
        cardList.add(getCardItem());

        Log.d(TAG, "added Card");
        cardListAdapter = new CardListAdapter(this, R.layout.card_item, cardList);
        Log.d(TAG, "adapter created: " + (cardListAdapter == null ? "cardAdapter is NULL" : "cardAdapter is not NULL"));
        flingContainer.setAdapter(cardListAdapter);
        Log.d(TAG, "adapter set: " + (flingContainer == null ? "flingContainer is NULL" : "flingContainer is not NULL"));
        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!");
                cardList.remove(0);
                cardListAdapter.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
                makeToast(FlashCardActivity.this, "Left!");
            }

            @Override
            public void onRightCardExit(Object dataObject) {
                makeToast(FlashCardActivity.this, "Right!");
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                // Ask for more data here
                Log.d(TAG, "onAdapterAboutToEmpty: "+itemsInAdapter);
                cardList.add(getCardItem());
                cardListAdapter.notifyDataSetChanged();
                Log.d("LIST", "notified");
                i++;
            }

            @Override
            public void onScroll(float scrollProgressPercent) {
                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
            }
        });

        // Optionally add an OnItemClickListener
        flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {
                makeToast(FlashCardActivity.this, "Clicked!");
            }
        });
    }

    @OnClick(R.id.right)
    public void right() {
        /**
         * Trigger the right event manually.
         */
        flingContainer.getTopCardListener().selectRight();
    }

    @OnClick(R.id.left)
    public void left() {
        flingContainer.getTopCardListener().selectLeft();
    }

    public CardItem getCardItem() {
        // No problem here.
        return newCard;
    }    
}

Notice that, getCardItem() just get one card. First of all, it calls two cards and the cardList has two cards when activity has created. And after that I'd like to get just one card after swipe. getCardItem() has no problem. I can see that I receives the data from my Server.

This is my custom CardListAdapter:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.koreanlab.fabloading.R;
import org.koreanlab.fabloading.item.CardItem;

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

public class CardListAdapter extends ArrayAdapter {
    private final String TAG = this.getClass().getSimpleName();
    private Context context;
    private int cardResId;
    private int frontResId;
    private int backResId;
    private List<CardItem> cardList;
    private LayoutInflater mInflater;

    //this, R.layout.card_item, R.id.card_front, R.id.card_back, cardList
    public CardListAdapter(Context context, int cardResId, ArrayList<CardItem> cardList) {
        super(context, cardResId);
        this.context = context;
        this.cardResId = cardResId;
        this.cardList = cardList;

        mInflater = LayoutInflater.from(context);
    }

    public void setItem(CardItem newItem) {
        Log.d(TAG, "setItem");

        for (int i = 0; i < cardList.size(); i++) {
            CardItem item = cardList.get(i);

            if (item.seq == newItem.seq) {
                cardList.set(i, newItem);
                break;
            }
        }
    }

    @Override
    public int getCount() {
        Log.d(TAG, "getVIew");
        return 0;
    }

    public int getPosition(CardItem item) {
        Log.d(TAG, "getPosition = "+item);
        return cardList.indexOf(item);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = mInflater.inflate(cardResId, parent,false);
            holder = new ViewHolder();
            Log.d(TAG, "getView");

            holder.frontTV = convertView.findViewById(R.id.card_front);
            holder.backTV = convertView.findViewById(R.id.card_back);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.frontTV.setText((String)getItem(position));
        holder.backTV.setText((String)getItem(position));
        return convertView;
    }
    static class ViewHolder
    {
        TextView frontTV, backTV;
    }
}

I checked many blogs and other Q&A, but I haven't figured it out how to solve this problem. That activity keeps going back after receiving 'one word' data from the Server and It doesn't show any error messages.

c-an
  • 3,543
  • 5
  • 35
  • 82
  • if you want to use 3rd party library this is good implementation https://github.com/yuyakaido/CardStackView – karan Nov 21 '18 at 06:06
  • @Karan Mer Oh, It seems nice!!. Could you please answer this too? https://stackoverflow.com/questions/53359024/how-to-draw-and-drop-to-put-an-item-into-a-view – c-an Nov 21 '18 at 06:09
  • no need to answer this as comment is sufficient. good luck – karan Nov 21 '18 at 06:10

0 Answers0