Android Retrofit:如何只从List中获取少量数据并显示在alertdialog列表中?

Android Retrofit: How to only take few data from List and show it in alertdialog list?

我真的卡在这里了。我使用改造,我已经制作了 POJO class CategoryResponse.class & CategoryData.class 这是我在 JSON:

中的数据
{
    "success": true,
    "message": "Category Report Found",
    "data": [
        {
            "id_category": 1,
            "category_name": "Mother Nature POWER",
            "created_at": "2021-09-29T21:25:07.000000Z",
            "updated_at": "2021-09-29T21:25:07.000000Z"
        },
        {
            "id_category": 2,
            "category_name": "Terrorisme",
            "created_at": "2021-09-29T21:25:20.000000Z",
            "updated_at": "2021-09-29T21:25:20.000000Z"
        }
    ]
}

这是我的“api.interface”,用于获取带有令牌承载的数据:

 @GET("mobile/category")
    Call<CategoryResponse> getCategoryList(@Header("Authorization") String authHeader);

这是我在 Activity class 中的称呼:

Call<CategoryResponse> call = RetrofitClient
                .getInstance()
                .getApi()
                .getCategoryList(rtoken);

        call.enqueue(new Callback<CategoryResponse>() {
            @Override
            public void onResponse(Call<CategoryResponse> call, Response<CategoryResponse> response) {

                CategoryResponse categoryResponse = response.body();
                if (response != null && response.isSuccessful()){
                    List<CategoryData> kategoriList = categoryResponse.getData();
                    
                     

                     -------- HERE IS WHERE I STUCK---------



                } else {
                    Toast.makeText(AddReportActivity.this, "Something is wrong i can feel it..", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<CategoryResponse> call, Throwable t) {
            }
        });

我的问题是如何只获取 id_category + category_name,并将其放入 alertdialog 列表中,以便在 onResponse 中创建一个简单的动态列表?因为我只想在单击警报对话框列表上的名称后保存 ID and/or 名称。管理员只需从服务器编辑列表即可。

*更新,这是我的 POJO CategoryData.clas:

import com.google.gson.annotations.SerializedName;

public class CategoryData {

    @SerializedName("category_name")
    private String categoryName;

    @SerializedName("updated_at")
    private String updatedAt;

    @SerializedName("id_category")
    private int idCategory;

    @SerializedName("created_at")
    private String createdAt;

    public void setCategoryName (String categoryName){
        this.categoryName = categoryName;
    }

    public String getCategoryName(){
        return categoryName;
    }

    public void setUpdatedAt(String updatedAt){
        this.updatedAt = updatedAt;
    }

    public String getUpdatedAt(){
        return updatedAt;
    }

    public void setIdCategory(int idCategory){
        this.idCategory = idCategory;
    }

    public int getIdCategory(){
        return idCategory;
    }

    public void setCreatedAt(String createdAt){
        this.createdAt = createdAt;
    }

    public String getCreatedAt(){
        return createdAt;
    }
}

试试这个

 if (response != null && response.isSuccessful()){
                List<CategoryData> kategoriList = categoryResponse.getData();
                 for(int i=0;i<kategoriList.size();i++)
               {
               // assuming that you have defined categoryID and categoryname in your pojo class. Change it accordingly 
                 String categotyID = kategoriList.get(i).getCategoryID();
                 String categoryName = kategoriList.get(i).getCategoryName();

             }
                 

                



            } else {
                Toast.makeText(AddReportActivity.this, "Something is wrong i can feel it..", Toast.LENGTH_SHORT).show();
            }

试试这个:

您可以只用两个参数(Id 和名称)创建 class BaseCategoryData,示例如下:

class BaseCategoryData{

   // here two fields category id and name;

}

class CategoryData extends BaseCategoryData{

   // here remaining fields

}

并且在获取的时候:

 List<BaseCategoryData> kategoriList = (List<BaseCategoryData>) categoryResponse.getData();

如果可行,那么这里我们使用 继承 逻辑,使用扩展和 向下转换 来自响应的列表将为我们提供正确的列表想要。