You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android点击分类时无法通过JSON获取对应子分类的问题

问题排查与解决方案

1. 主分类点击事件绑定

首先确保QuizAdapter正确实现点击回调,将选中的主分类ID传递给宿主组件:

// 在QuizAdapter中定义回调接口并绑定点击事件
public class QuizAdapter extends RecyclerView.Adapter<QuizAdapter.QuizViewHolder> {
    private List<Category> categoryList;
    private OnCategoryClickListener onCategoryClickListener;

    // 定义回调接口
    public interface OnCategoryClickListener {
        void onCategoryClick(int categoryId);
    }

    // 提供回调设置方法
    public void setOnCategoryClickListener(OnCategoryClickListener listener) {
        this.onCategoryClickListener = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull QuizViewHolder holder, int position) {
        Category category = categoryList.get(position);
        holder.tvCategory.setText(category.getName());
        
        // 绑定点击事件
        holder.itemView.setOnClickListener(v -> {
            if (onCategoryClickListener != null) {
                onCategoryClickListener.onCategoryClick(category.getId());
            }
        });
    }

    // ViewHolder及其他代码省略
}

在Activity/Fragment中设置回调,触发子分类加载:

quizAdapter.setOnCategoryClickListener(categoryId -> {
    new SubCat_List_Loader(categoryId).execute();
});

2. 子分类异步任务(SubCat_List_Loader)修正

确保异步任务正确接收主分类ID、请求并过滤对应子分类:

public class SubCat_List_Loader extends AsyncTask<Void, Void, List<SubCategory>> {
    private int targetCategoryId;

    public SubCat_List_Loader(int categoryId) {
        this.targetCategoryId = categoryId;
    }

    @Override
    protected List<SubCategory> doInBackground(Void... voids) {
        List<SubCategory> subCategoryList = new ArrayList<>();
        try {
            // 替换为你的JSON请求/读取逻辑
            String jsonResponse = loadSubCategoryJson(); 
            JSONArray jsonArray = new JSONArray(jsonResponse);
            
            // 过滤当前主分类对应的子分类
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                int parentId = obj.getInt("parent_id"); // 确保字段名与你的JSON一致
                if (parentId == targetCategoryId) {
                    SubCategory subCat = new SubCategory();
                    subCat.setId(obj.getInt("id"));
                    subCat.setName(obj.getString("name"));
                    subCategoryList.add(subCat);
                }
            }
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
        return subCategoryList;
    }

    @Override
    protected void onPostExecute(List<SubCategory> result) {
        super.onPostExecute(result);
        if (result != null) {
            // 更新子分类适配器数据
            subCategoryAdapter.setSubCategoryList(result);
            subCategoryAdapter.notifyDataSetChanged();
            // 控制子分类RecyclerView显示/隐藏
            subCatRecyclerView.setVisibility(result.isEmpty() ? View.GONE : View.VISIBLE);
        }
    }
}

3. SubCategoryAdapter数据更新逻辑

确保适配器支持数据批量更新,避免直接替换列表引用:

public class SubCategoryAdapter extends RecyclerView.Adapter<SubCategoryAdapter.SubCatViewHolder> {
    private List<SubCategory> subCategoryList = new ArrayList<>();

    // 提供数据更新方法
    public void setSubCategoryList(List<SubCategory> newList) {
        this.subCategoryList.clear();
        this.subCategoryList.addAll(newList);
    }

    // 其他代码省略
}

4. 常见错误排查

  • 核对JSON字段名:确认主分类的id与子分类的父级字段(如parent_id)完全匹配,避免拼写错误
  • 查看Logcat:检查是否存在JSON解析异常、空指针异常,这些是快速定位问题的关键
  • 确认RecyclerView配置:子分类RecyclerView需正确设置布局管理器,且适配器已提前绑定
  • 异步任务线程检查:确保仅在onPostExecute中操作UI,doInBackground中禁止更新视图

内容的提问来源于stack exchange,提问作者user8663248

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:28:25