android - 为每个卡片视图获取有限的 json 数组
android - fetching limited json array for each cardview
我正在使用 cardview,每张卡片都有一个弹出菜单,可以打开另一个 activity。 “数据”数组显示在卡上,我希望在打开其他 activity 时显示相应的数组“document_list”。下面是代码,但所有项目都得到显示。那么我该如何限制或约束呢?数据(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 是新的 activity,它使用以下代码打开卡片视图。所以在点击 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
},
]
},
你可以这样做:
在您的 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,每张卡片都有一个弹出菜单,可以打开另一个 activity。 “数据”数组显示在卡上,我希望在打开其他 activity 时显示相应的数组“document_list”。下面是代码,但所有项目都得到显示。那么我该如何限制或约束呢?数据(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 是新的 activity,它使用以下代码打开卡片视图。所以在点击 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
},
]
},
你可以这样做:
在您的 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
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) {
}