如何在一个适配器 Android studio Java 中使用两个 arrayList?

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

我知道之前有人问过这个问题,但我无法理解并将其集成到我的代码中。我有 2 个 arraylist.One 用于 api 数据,另一个用于数据库 data.Database 的数组列表,当它单独工作时成功。但是当我添加 API 的数组列表时,我得到了这个异常 java.lang.IndexOutOfBoundsException:索引:0,大小:0

这是片段文件;

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

}

这是 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);


    }
}


}

为什么不起作用?我需要你的帮助。

您可以像这样创建和初始化适配器:

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

此时cuzdanList为空。显然,您需要在 cuzdanList 中拥有与在 cuzdanModels 中完全相同数量的项目,而这并没有发生。崩溃时,List<cuzdanModels> 有超过 0 个元素,List<Datum> 仍然是空的。

您在适配器中将 List<CuzdanModel> 称为 cuzdanList 并将 List<Datum> 称为 mData,但使用 [=11= 调用适配器构造函数] 因为 List<Datum> 可能是你的问题的一部分。

java.lang.IndexOutOfBoundsException 表示您正在尝试从列表(或数组或其他集合)中获取元素,但列表中没有它。更具体地说,Size: 0 表示您的列表有 0 个元素,因此它是空的,而 Index: 0 表示您正在尝试获取第一个元素。但是列表是空的,所以没有第一个元素,因此你会得到异常。

您补充说异常发生在第 38 行,即 'Datum datum = mData.get(position);。似乎在那一点 position 是 0,而 mData 是空的。查看您的代码,我没有在 Adapter class 中找到您将内容放入 mData 的任何行,您只在 CuzdanAdapter 构造函数中对其进行了初始化。这在第 adapter = new CuzdanAdapter(getActivity(),cuzdanModels,cuzdanList); 行调用,其中 cuzdanList 在两行之前创建为空。在片段文件中,您在 cuzdanList 中放置任何内容的唯一位置是行 cuzdanList.addAll(list.getData());。但是如果响应为空,则不会添加任何元素,稍后会得到 IndexOutOfBoundsException。