问题描述
我正在使用 cardview,每张卡片都有一个弹出菜单,可以打开另一个活动。 “数据”数组显示在卡片上,我希望在其他活动打开时显示相应的数组“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")
));
}
}
卡片视图适配器
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","document_name": "Mother Deed","document_type_id": "84","document_name": "MUTATION copY ","document_type_id": "87","document_name": "OLD E C","document_type_id": "83","document_name": "OTHER DOCUMENTS ",]
},{
"id": "9","name": "A D ","site_no": "SY NO 19/1 ","p_id_no": "19/1","sas_application_no": "12","longitude": "45","owner_details": {
"name": "A D ","mobile": "7760906"
},"size": "2","unit": "ACR","document_list": [
{
"property_id": "9",{
"property_id": "9","document_type_id": "43","document_name": "GPA",
解决方法
你可以这样做:
在您的 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) {
}