我以JSON的形式从服务器中获取了一系列数据,如下所示,然后将它们存储在列表中,然后它们与适配器一起显示在Recycler视图中。
[
{
"cat_id": 593,
"title": "آلرژی و ایمونولوژی",
"image_add": "http://pezeshk-yab.com/upload/job_add",
"image": "http://pezeshk-yab.com/upload/job_cat/Allergy.svg",
"sub_cat": [
{
"cat_id": 594,
"cat_title": "متخصص",
"cat_parent_fk": 593
},
{
"cat_id": 595,
"cat_title": "فوق تخصص",
"cat_parent_fk": 593
}
]
},
{
"cat_id": 15,
"title": "ارتوپد",
"image_add": "http://pezeshk-yab.com/upload/job_add",
"image": "http://pezeshk-yab.com/upload/job_cat/Orthopedic.svg",
"sub_cat": [
{
"cat_id": 506,
"cat_title": "متخصص",
"cat_parent_fk": 15
},
{
"cat_id": 507,
"cat_title": "فوق تخصص",
"cat_parent_fk": 15
}
]
}]
一切正常。现在,我想要显示一个对话框,方法是单击“回收站”中的任何项以仅显示这两个JSON部分:
"sub_cat": [
{
"cat_id": 594,
"cat_title": "متخصص",
"cat_parent_fk": 593
},
{
"cat_id": 595,
"cat_title": "فوق تخصص",
"cat_parent_fk": 593
}
]
为此,我在适配器中定义了对话框,问题是现在对话框{all sub_cat }
中的所有部分都已显示。
我的适配器的代码如下:
[![public class CatsAdapter extends RecyclerView.Adapter<CatsAdapter.ViewHolder> {
private Context context;
private List<Cats> list;
CatsAdapter(Context context, List<Cats> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.cat_index, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Cats cats = list.get(position);
holder.txt_title.setText(cats.getTitle());
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Dialog dialog = new Dialog((Activity) context);
String id = cats.getCat_id();
Log.i("l", "onClick: " +id );
List<Cats> catsList = new ArrayList<>();
dialog.setContentView(R.layout.custompopup);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
RecyclerView recyclerView = (RecyclerView) dialog.findViewById(R.id.rec_dialog);
DialogAdapter dialogAdapter = new DialogAdapter(context, list);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(dialogAdapter);
dialog.show();
}
});
ImageRequest.create(holder.image).setTargetUrl(cats.getImage()).execute();
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
TextView txt_title;
CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.img_cat);
txt_title = (TextView) itemView.findViewById(R.id.title_cat);
cardView = (CardView) itemView.findViewById(R.id.card_view);
txt_title.setSelected(true);
}
}
}]
解析JSON代码:
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
Cats cats = new Cats();
JSONObject jsonObject = (JSONObject) response.get(i);
String cat_id = jsonObject.getString("cat_id");
String title = jsonObject.getString("title");
String image_add = jsonObject.getString("image_add");
String image = jsonObject.getString("image");
JSONArray jsonArray = jsonObject.getJSONArray("sub_cat");
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject object = jsonArray.getJSONObject(j);
String sub_cat_id = object.getString("cat_id");
String sub_cat_title = object.getString("cat_title");
String sub_parent_fk = object.getString("cat_parent_fk");
cats.setSub_cat_id(sub_cat_id);
cats.setCat_title(sub_cat_title);
cats.setCat_parent_fk(sub_parent_fk);
}
cats.setCat_id(cat_id);
cats.setTitle(title);
cats.setImage(image);
cats.setImage_add(image_add);
list.add(cats);
catsAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
为什么要将整个列表传递给DialogAdapter?
DialogAdapter dialogAdapter = new DialogAdapter(context, list);
如果要在对话框中显示该类别的子sub_cat,则应从该类别中获取sub_cats并使用这些sub_cats创建一个适配器
这应该是您的类别类:
public class Category {
private Integer catId;
private String title;
private String imageAdd;
private String image;
private List<SubCat> subCat = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getCatId() {
return catId;
}
public void setCatId(Integer catId) {
this.catId = catId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImageAdd() {
return imageAdd;
}
public void setImageAdd(String imageAdd) {
this.imageAdd = imageAdd;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<SubCat> getSubCat() {
return subCat;
}
public void setSubCat(List<SubCat> subCat) {
this.subCat = subCat;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
和subCategory类:
public class SubCat {
private Integer catId;
private String catTitle;
private Integer catParentFk;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public Integer getCatId() {
return catId;
}
public void setCatId(Integer catId) {
this.catId = catId;
}
public String getCatTitle() {
return catTitle;
}
public void setCatTitle(String catTitle) {
this.catTitle = catTitle;
}
public Integer getCatParentFk() {
return catParentFk;
}
public void setCatParentFk(Integer catParentFk) {
this.catParentFk = catParentFk;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
,并在您的代码中执行此操作:
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
Category category = new Category();
JSONObject jsonObject = (JSONObject) response.get(i);
category.setCatId(jsonObject.getString("cat_id"));
category.setTitle(jsonObject.getString("title"));
category.setImageAdd(jsonObject.getString("image_add"));
category.setImage(jsonObject.getString("image"));
JSONArray jsonArray = jsonObject.getJSONArray("sub_cat");
category.setSubCat(new ArrayList<>());
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject object = jsonArray.getJSONObject(j);
SubCat subCat = new SubCat();
subCat.setCatId(object.getString("cat_id"));
subCat.setCatTitle(object.getString("cat_title"));
subCat.setCatParentFk(object.getString("cat_parent_fk"));
category.getSubCat().add(subCat);
}
list.add(category);
} catch (JSONException e) {
e.printStackTrace();
}
}
catsAdapter.notifyDataSetChanged();
}
然后,当您需要该类别的子类别时,您可以执行以下操作:
Category cat=list.get(position);
ArrayList<SubCat> subCategories=cat.getSubCat();