【发布时间】:2020-12-30 19:24:55
【问题描述】:
我是 android 开发的新手和幼稚,所以如果这是一个非常基本的问题,我深表歉意。
在我们当前的应用程序中,我有一个 recyclerview,它呈现一个包含类别列表的复选框。在每个类别中,还有其他几个子类别,例如
- 食品(类别之一)
- 蔬菜
- 家禽
- 肉类
- 等等等等,比如说 10 个这样的子类别
我最初关心的是页面加载,子类别有 10 个项目。当用户以一种在 UI 上仅显示 5 个项目的方式滚动,然后选择子类别中的最后一个值时,我看到一个索引超出范围异常。
根本原因是在 onBindView 方法中,子类别 arraylist 被 recyclerview 刷新以仅保存 5 个项目,当我尝试从该 arraylist 获取(第 10 个)位置时,我遇到了超出范围的异常。 我花了 3 天时间在网上搜索此内容,但我不知道如何阻止我的数组列表刷新,以便当用户滚动时某些类别在 UI 上不可见时它不会更改其值。 请求您的帮助,让我为 2020 年带来和平结束。
下面是我的onbindview sn-p
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.setIsRecyclable(false);
subCategoryModel = arrayList_sub_categories.get(position);
holder.catName.setText(subCategoryModel.getsSubCatName());
holder.catName.setChecked(arrayList_sub_categories.get(position).getSelected());
holder.catName.setTag(position);
String strSubCategory = SharedPrefrence_Seller.getFixr_shop_Subcategoryid();
holder.catName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//This is where I try to get the selected tag which gives me the index 10th if I selected the last sub category
Integer pos = (Integer) holder.catName.getTag();
//Below line gives an index out of bounds because my arrayList_sub_categories got refreshed to have only 5 values which are visible on the UI and I am trying to get the 10th value
subCategoryModel = arrayList_sub_categories.get(pos);
req = split;
if(!(subCategoryModel==null)) {
//Do bunch of stuff here
}
我正在添加更多代码 sn-ps 来告诉您有问题的列表 arrayList_sub_categories 是通过下面的 sn-p 在另一个类中通过 API 调出设置的
private void func_fetch_CategoryAPI(final String sCategoryNAme, final String stype) {
requestQueue = VolleySingleton.getInstance(ServiceSelctionActivity.this).getRequestQueue();
//if everything is fine
StringRequest stringRequest = new StringRequest(Request.Method.POST, Urls.URL_SALES_FETCH_CATEGORY_ITEMS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting response to json object and all this is not relevant to this question
JSONObject obj = new JSONObject(response);
String str = obj.getString("data");
arrayListCatItem.clear();
arrayListsubCatItem.clear();
JSONArray jsonArray = new JSONArray(str);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsoObject = jsonArray.getJSONObject(i);
Model_Item_Category model_item_category = new Model_Item_Category();
hashMapListChooseCat = new HashMap<>();
hashMapListChooseCatStatus = new HashMap<>();
model_item_category.setCatId(jsoObject.getString("id"));
model_item_category.setPosition_key(jsoObject.getString("position_key"));
model_item_category.setCatName(jsoObject.getString("cat_name"));
model_item_category.setCatIconName(jsoObject.getString("image_url"));
model_item_category.setSub_cat_status(jsoObject.getString("sub_cat_status"));
//finally
model_item_category.setCheckLevel(strcheck_level);
strSelectedService = SharedPrefrence_Seller.getFixr_shop_categoryid();
arrayListCatItem.add(model_item_category);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
//Here is where adapter_category is set which is a custom class object that includes the list model_item_category in question
rvSelectService.setAdapter(adapter_category);
adapter_category.notifyDataSetChanged();
}
}
adapter_category的定义是
public Adapter_Category(Context activity, List<Model_Item_Category> subCategoryList, HashMap<String, String> hashMapListChooseCat,
HashMap<String, String> hashMapListChooseCatStatus, TextView tvService)
【问题讨论】:
-
尝试将
Integer pos = (Integer) holder.catName.getTag();更改为Integer pos = (Integer) view.getTag();。
标签: android android-recyclerview