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

Android Retrofit如何从返回List中提取部分数据展示到AlertDialog列表

你可以在卡壳位置直接填入以下代码实现需求:

// 提取所有分类名称生成弹窗展示用的列表
List<String> categoryNames = new ArrayList<>();
for (CategoryData item : kategoriList) {
    categoryNames.add(item.getCategoryName());
}
String[] nameArray = categoryNames.toArray(new String[0]);

// 构建分类选择弹窗
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(AddReportActivity.this);
dialogBuilder.setTitle("请选择分类");
// 绑定列表和点击事件
dialogBuilder.setItems(nameArray, (dialog, which) -> {
    // 点击后直接通过下标获取对应分类的id和名称
    int selectCategoryId = kategoriList.get(which).getIdCategory();
    String selectCategoryName = kategoriList.get(which).getCategoryName();
    
    // 此处写入你的保存逻辑,可自行存储到全局变量、本地缓存或者直接用于后续业务逻辑
    Toast.makeText(AddReportActivity.this, "已选择:" + selectCategoryName, Toast.LENGTH_SHORT).show();
    
    dialog.dismiss();
});
// 显示弹窗
dialogBuilder.show();

注意事项

  • 若使用AndroidX兼容包,记得导入androidx.appcompat.app.AlertDialog避免兼容问题
  • 建议补充onFailure方法的错误提示,避免请求失败时用户无感知:
@Override
public void onFailure(Call<CategoryResponse> call, Throwable t) {
    Toast.makeText(AddReportActivity.this, "分类加载失败:" + t.getMessage(), Toast.LENGTH_SHORT).show();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 14:06:05