android - 为每个卡片视图获取有限的 json 数组

Posted

技术标签:

【中文标题】android - 为每个卡片视图获取有限的 json 数组【英文标题】:android - fetching limited json array for each cardview 【发布时间】:2021-07-04 11:29:15 【问题描述】:

我正在使用cardview,每张卡片都有一个弹出菜单,可以打开另一个活动。 “data”数组显示在卡片上,我希望在打开其他活动时显示相应的数组“document_list”。下面是代码,但所有项目都显示出来了。那么如何限制或限制? data(id) == document_list(property_id)。

卡片浏览片段

 StringRequest stringRequest = new StringRequest(Request.Method.POST,"URL",
            new Response.Listener<String>() 
                @Override
                public void onResponse(String response) 
                    try 
                        //converting the string to json array object
                        JSONObject object = new JSONObject(response);
                        if (object.getInt("status") == 200) 
                            JSONArray jsonArray = object.getJSONArray("data");
                           // String userData = jsonArray.getString(0);
                            for (int i = 0; i < jsonArray.length(); i++) 
                                JSONObject pobj = jsonArray.getJSONObject(i);
                                pobj.getString("id");
                                JSONObject pobj1 = new JSONObject(pobj.getString("owner_details"));
                                mylistList.add(new Mylist(
                                      //other elements
                                        pobj.getString("sas_application_no"),
                                        pobj1.getString("name"),
                                        pobj1.getString("mobile")
                                        ));
                            
                        

CARDVIEW 适配器

holder.buttonViewOption.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            PopupMenu popup = new PopupMenu(mCtx, holder.buttonViewOption);
            popup.inflate(R.menu.options_menu);
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 
                @Override
                public boolean onMenuItemClick(MenuItem item) 
                    switch (item.getItemId()) 
                        
                        case R.id.menu_document:
                            //handle menu2 click
                            id = mylist.getId();
                            Bundle bundle = new Bundle();
                            bundle.putString("id",id);
                            DocFragment newfragment1 = new DocFragment();
                            FragmentTransaction fragmentTransaction1 =((AppCompatActivity)mCtx).getSupportFragmentManager().beginTransaction();
                            fragmentTransaction1.replace(R.id.container_view1, newfragment1);
                            fragmentTransaction1.addToBackStack(null);
                            fragmentTransaction1.commit();
                            NavigationActivity.container_view1.setVisibility(View.VISIBLE);
                            break;
                    
                    return false;
                
            );

Doc_fragment 是使用以下代码打开卡片视图的新活动。所以点击 R.id.menu_document: 我需要传递从 CARDVIEW FRAGMENT 中获取的“id”?我该怎么做?

public class DocFragment extends Fragment 
//a list to store all the products
List<DocList> docList;
DocAdapter docAdapter;
//the recyclerview
RecyclerView recyclerView;
private String sid = "";

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) 

    View root = inflater.inflate(R.layout.fragment_doc, container, false);
    if (getArguments() != null) 
        sid = getArguments().getString("id");
        Toast.makeText(getActivity(), sid, Toast.LENGTH_LONG).show();
    
    docList = new ArrayList<>();
    recyclerView = (RecyclerView) root.findViewById(R.id.recyclerView1);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    //docList.add(new DocList("abc","123","asdf"));
    //creating recyclerview adapter
    docAdapter = new DocAdapter(getContext(), docList);
    recyclerView.setAdapter(docAdapter);
    loadMenuData();

    return root;



public void loadMenuData() 
    StringRequest stringRequest = new StringRequest(Request.Method.POST, "url",
            new Response.Listener<String>() 
                @Override
                public void onResponse(String response) 
                    try 
                        //converting the string to json array object
                        //String selectedId = getIntent().getStringExtra("selectedId");
                        JSONObject object = new JSONObject(response);
                        if (object.getInt("status") == 200) 
                            JSONArray jsonArray = object.getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) 
                                JSONObject pobj = jsonArray.getJSONObject(i);
                                if (!sid.equals(pobj.optString("id", "")))
                                    continue;
                                JSONArray jsonArray1 = pobj.getJSONArray("document_list");
                                for (int j = 0; j < jsonArray1.length(); j++) 
                                    JSONObject dobj = jsonArray1.getJSONObject(j);
                                    docList.add(new DocList(
                                            dobj.getString("document_name"),
                                            dobj.getString("description"),
                                            dobj.getString("doc_no")
                                    ));
                                
                            
                        

                        //creating adapter object and setting it to recyclerview
                        docAdapter = new DocAdapter(getContext(), docList);
                        recyclerView.setAdapter(docAdapter);

                     catch (JSONException e) 
                        e.printStackTrace();
                    
                
            ,
            new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 

                
            );

    //adding our stringrequest to queue
    Volley.newRequestQueue(getContext()).add(stringRequest);


"status": 200,
"data": [
    
        "id": "5",
        "property_type": "AGRICULTURAL",
        "name": "A D  ",
        "site_no": "SY NO 21",
        "p_id_no": "21",
        "sas_application_no": "13",
        "latitude": "12",
        "longitude": "33",
        "status": "OWNED",
        "purchase_year": "2007",
        "owner_details": 
            "name": "A D",
            "mobile": "776006"
        ,
        "size": "41724",
        "unit": "SQF",
        "document_list": [
            
                "property_id": "5",
                "document_type_id": "4",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "Encumbrance Certificate",
                "description": null,
                "doc_no": null
            ,
            
                "property_id": "5",
                "document_type_id": "3",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "Mother Deed",
                "description": null,
                "doc_no": null
            ,
            
                "property_id": "5",
                "document_type_id": "84",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "MUTATION COPY ",
                "description": null,
                "doc_no": null
            ,
            
                "property_id": "5",
                "document_type_id": "87",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "OLD E C",
                "description": null,
                "doc_no": null
            ,
            
                "property_id": "5",
                "document_type_id": "83",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "OTHER DOCUMENTS ",
                "description": null,
                "doc_no": null
            ,
                    ]
    ,
    
        "id": "9",
        "property_type": "AGRICULTURAL",
        "name": "A D ",
        "site_no": "SY NO 19/1 ",
        "p_id_no": "19/1",
        "sas_application_no": "12",
        "latitude": "12",
        "longitude": "45",
        "status": "OWNED",
        "purchase_year": "2007",
        "owner_details": 
            "name": "A D ",
            "mobile": "7760906"
        ,
        "size": "2",
        "unit": "ACR",
        "document_list": [
            
                "property_id": "9",
                "document_type_id": "4",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "Encumbrance Certificate",
                "description": null,
                "doc_no": null
            ,
            
                "property_id": "9",
                "document_type_id": "43",
                "file_path": "https://demoapi.dyuppar.in/uploads",
                "document_name": "GPA",
                "description": null,
                "doc_no": null
            ,
            
        ]
    ,

【问题讨论】:

您通过什么意图调用该活动?你在其他活动中的收获如何?你能请一些代码和例子吗? @Mayur Gajra - 我没有通过意图传递任何东西。在这两个活动中,我都传递了相同的 URL。(cardview 工作正常) 【参考方案1】:

你可以这样做:

在您的 CardViewFragment 中设置选择卡片时的参数,如下所示:

switch (item.getItemId()) 

            case R.id.menu_document:
                //handle menu2 click
                Bundle bundle = new Bundle();
                bundle.putString("selectedId",selectedId);//get this id from your object/model .e.g list.get(postion).getId() , do it as per your strcuture setup. 
                DocFragment newfragment1 = new DocFragment();
                newfragment1.setArguments(bundle);
                FragmentTransaction fragmentTransaction1 =((AppCompatActivity)mCtx).getSupportFragmentManager().beginTransaction();
                fragmentTransaction1.replace(R.id.container_view1, newfragment1);
                fragmentTransaction1.addToBackStack(null);
                fragmentTransaction1.commit();
                NavigationActivity.container_view1.setVisibility(View.VISIBLE);
                break;
        

在您的DocFragment 中创建一个全局变量并将其与来自您的源的数据相匹配。

private String selectedId = "";

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        if (getArguments() != null) 
            selectedId = getArguments().getString("selectedId");
        
    

然后在获取DocFragment时匹配该id

        try 
            //converting the string to json array object
            JSONObject object = new JSONObject(response);
            if (object.getInt("status") == 200) 
                JSONArray jsonArray = object.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++) 
                    JSONObject pobj = jsonArray.getJSONObject(i);
                    // compare id passed from main activity, which was selected by user
                    if (!selectedId.equals(pobj.optString("id", "")))
                        continue;
                    JSONArray jsonArray1 = pobj.getJSONArray("document_list");
                    for (int j = 0; j < jsonArray1.length(); j++) 
                        JSONObject dobj = jsonArray1.getJSONObject(j);
                        docList.add(new DocList(
                                dobj.getString("document_name"),
                                dobj.getString("description"),
                                dobj.getString("doc_no")
                        ));
                    
                
            
         catch (Exception e) 

        

【讨论】:

好的,但是我有一个 cardview 片段和 cardview 适配器(在这个适配器中我有一个弹出菜单)所以通过弹出菜单我调用新片段(所以 id shd 在这里传递但意图不起作用) @AparnaMutnalkar 您能否在您如何调用片段的问题中更新您的代码?以及您在其他片段中收到的内容? @AparnaMutnalkar 查看更新的答案,如果有帮助,请告诉我。 我更新了谢谢你但是 if (getArguments() != null) selectedId = getArguments().getString("selectedId"); 为空白。我尝试在 toast 中显示 你检查过你传入的bundle.putString("selectedId",selectedId); 不为空吗?如果没有,请使用您尝试过的内容更新您的最新代码。

以上是关于android - 为每个卡片视图获取有限的 json 数组的主要内容,如果未能解决你的问题,请参考以下文章

Android - 如何像每 1 秒一样自动刷新卡片视图

在Android中隐藏另一个视图后面的视图

为每个卡片视图打开 1 个不同的活动

如何为我在回收站视图中的卡片视图中的每个项目创建一个点击监听器

CardElevation的Android问题

Android10.4 卡片视图