skip to Main Content

i actually stuck here. I use retrofit, i already made the POJO class CategoryResponse.class & CategoryData.class here is my data in 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"
        }
    ]
}

Here is my "api.interface" to get the data with token bearer:

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

And here is how i call it in 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) {
            }
        });

My problem is how to get only id_category + category_name, and put it inside alertdialog list, to make a simple dynamic list inside onResponse? bcuz i just want to save the id and/or the name after clicked the name on the alerdialog list. The admin just need to edit the list from the server.

*Update, here is my 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;
    }
}

2

Answers


  1. try this

     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();
                }
    
    Login or Signup to reply.
  2. Try this one:

    You can make class BaseCategoryData with only two parameters (Id and name) here is the example:

    class BaseCategoryData{
    
       // here two fields category id and name;
    
    }
    
    class CategoryData extends BaseCategoryData{
    
       // here remaining fields
    
    }
    

    And at the time of fetching:

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

    If it works, then here we uses the inheritance logic using extends and downcasting the list from response will give us correct list you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search