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



我实际上卡在这里了。我使用改造,我已经使POJO类CategoryResponse.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类中调用它:

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和/或在alerdialog列表中单击名称后的名称。管理员只需要从服务器编辑列表。

*更新,这里是我的POJO categorydata . class:

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;
}
}

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();
}

试试这个:

您可以仅使用两个参数(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();

如果有效,那么这里我们使用继承使用extends和向下转换的逻辑回复的列表将给出您想要的正确列表。

最新更新