简体   繁体   中英

How to use two arrayList in one adapter Android studio Java?

I know this question asked before but I couldn't understand and integrate this into my code. I have 2 arraylist.One for api data and the other for database data.Database's arraylist working successful when it's alone. But when I add the API's arraylist, i get this exception java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

This is Fragment file;

RecyclerView cuzdan_rc;
private ArrayList<CuzdanModel> cuzdanModels;
CuzdanAdapter adapter;

private VeriTabani vt;
CoinMarketApi api;




private List<Datum> cuzdanList =null;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.varliklarim_fragment_layout, container, false);
    vt = new VeriTabani(getActivity());
    cuzdan_rc=rootView.findViewById(R.id.cuzdan_recycler_view);
    api = ApiClient.getClient().create(CoinMarketApi.class);
    CoinList();
    RecyclerBagla(rootView);


    return rootView;
}
private void RecyclerBagla(View rootView) {
    cuzdan_rc=rootView.findViewById(R.id.cuzdan_recycler_view);
    cuzdanList = new ArrayList<>();
    cuzdanModels = new CuzdanDao().Cuzdan(vt);
    adapter = new CuzdanAdapter(getActivity(),cuzdanModels,cuzdanList);

    cuzdan_rc.setAdapter(adapter);
    cuzdan_rc.setLayoutManager(new LinearLayoutManager(getActivity()));


}
public void CoinList() {
    Call<CryptoModel> call = api.coinMarketListCall("500");


    call.enqueue(new Callback<CryptoModel>() {

        @Override
        public void onResponse(Call<CryptoModel> call, Response<CryptoModel> response) {

            CryptoModel list = response.body();

            cuzdanList.clear();
            cuzdanList.addAll(list.getData());
         

            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<CryptoModel> call, Throwable t) {
             Toast.makeText(getActivity(), "onFailure", Toast.LENGTH_SHORT).show();
            Log.d("XXXX", t.getLocalizedMessage());
            call.cancel();
        }
    });

}

And this is Adaptar class;

public class CuzdanAdapter extends RecyclerView.Adapter<CuzdanAdapter.CardTasarimTutucu> {

private Context context;
private List<CuzdanModel> cuzdanList;
private List<Datum> mData;
public CuzdanAdapter(Context context, List<CuzdanModel> cuzdanList,List<Datum> mData) {
    this.context = context;
    this.cuzdanList = cuzdanList;
    this.mData=mData;
}

@NonNull
@Override
public CardTasarimTutucu onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cuzdan_rc_tasarim, parent, false);
    return new CardTasarimTutucu(view);
}

@Override
public void onBindViewHolder(@NonNull CardTasarimTutucu holder, int position) {

    Datum datum = mData.get(position);

    CuzdanModel cuzdan = cuzdanList.get(position);
    holder.textViewCoinDegeriAdapter.setText(String.valueOf(datum.getQuote().getUSD().getPrice()));
    holder.textViewCoinAdi.setText(cuzdan.getCoin_adi());

    holder.textViewAlisFiyatiAdapter.setText(String.format("%.3f",cuzdan.getCoin_alis_fiyati()));
    holder.textViewCoinAdediAdapter.setText(String.format("%.3f",cuzdan.getCoin_alis_adeti()));

}

@Override
public int getItemCount() {
    return cuzdanList.size();
}

public class CardTasarimTutucu extends RecyclerView.ViewHolder {
    private TextView textViewCoinAdi, textViewCoinDegisimAdapter,
            textViewCoinAdediAdapter, textViewCoinDegeriAdapter, textViewAlisFiyatiAdapter;
    private CardView cuzdan_cardView;

    public CardTasarimTutucu(@NonNull View itemView) {
        super(itemView);
        textViewCoinAdi = itemView.findViewById(R.id.textViewCoinAdiAdapter);
        textViewCoinDegisimAdapter = itemView.findViewById(R.id.textViewCoinDegisimAdapter);
        textViewCoinAdediAdapter = itemView.findViewById(R.id.textViewCoinAdediAdapter);
        textViewCoinDegeriAdapter = itemView.findViewById(R.id.textViewCoinDegeriAdapter);
        textViewAlisFiyatiAdapter = itemView.findViewById(R.id.textViewAlisFiyatiAdapter);
        cuzdan_cardView = itemView.findViewById(R.id.cuzdan_cardView);


    }
}


}

Why it is not working? I need your help.

You create and initialize the adapter like this:

cuzdanList = new ArrayList<>();
cuzdanModels = new CuzdanDao().Cuzdan(vt);
adapter = new CuzdanAdapter(getActivity(),cuzdanModels,cuzdanList);

At this point cuzdanList is empty. Obviously you need to have exactly the same number of items in cuzdanList as you have in cuzdanModels and that isn't happening. At the time of the crash, List<cuzdanModels> has more than 0 elements and List<Datum> is still empty.

The fact that you refer to the List<CuzdanModel> as cuzdanList and the List<Datum> as mData in the adapter, but call the adapter constructor using cuzdanList as the List<Datum> might be part of your problem.

java.lang.IndexOutOfBoundsException indicates that you are trying to get an element from a list (or an array or some other collection), but the list does not have it. More specifically, Size: 0 says that your list has 0 elements, so it is empty, and Index: 0 says that you are trying to get first element. But the list is empty, so there is no first element, therefore you get the exception.

You added that the exception happens on line 38 that is 'Datum datum = mData.get(position); . Seems like at that point position is 0, and mData is empty. Looking at your code, I did not find any line in Adapter class where you put something into mData , you only initialize it in CuzdanAdapter constructor. And that is called at line adapter = new CuzdanAdapter(getActivity(),cuzdanModels,cuzdanList); , where cuzdanList is created empty two lines before. The only place in the Fragment file where you put anything in cuzdanList is line cuzdanList.addAll(list.getData()); . But if the response is empty, no elements will be added, and later you get the IndexOutOfBoundsException.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM