所有标签显示相同的列表内容。需要帮助Menufragment.class根据标签位置
访问相应的类别项目预期结果:我需要在第一个标签中显示4个羊肉项目,在第二个标签中显示8个海鲜,在第三个标签中显示5个面条,在第四个标签中显示7个蛋品,在第五个标签中显示13个蔬菜菜肴。
实际结果:所有标签显示13种素食菜肴清单。
主要活动
JSONObject hotels = obj.getJSONObject("Menu");
JSONArray items = hotels.getJSONArray("Items");
for (int j = 0; j < items.length(); j++) {
JSONObject hotel = items.getJSONObject(j);
categoryname = hotel.getString("name");
System.out.println(categoryname);
JSONArray hotelmenu = hotel.getJSONArray(categoryname);
//categoryitems = new ArrayList<List<Menuclass>>(hotelmenu.length());
listitems = new ArrayList<Menuclass>(hotelmenu.length());
List<Menuclass> list = new ArrayList<Menuclass>(hotelmenu.length());
// categoryitems.add(list);
for (int i = 0; i < hotelmenu.length(); i++) {
System.out.println(hotelmenu.length());
JSONObject menuitems = hotelmenu.getJSONObject(i);
itemname = menuitems.getString("name");
itemsprice = menuitems.getString("price");
itemtype = menuitems.getString("veg");
int itemtypeint = Integer.parseInt(itemtype);
// System.out.println(menuitems[i]);
if (itemtypeint == 0) {
list.add(new Menuclass(itemname, itemsprice, R.drawable.nonveg));
// categoryitems.add(Arrays.asList(new Menuclass(itemname,itemtype,R.drawable.nonveg)));
}
else {
list.add(new Menuclass(itemname, itemsprice, R.drawable.vegicon));
// categoryitems.add(Arrays.asList(new Menuclass(itemname, itemtype, R.drawable.vegicon)));
}
listitems=list;
}
// System.out.println(categoryitems);
System.out.println(listitems);
tabLayout.addTab(tabLayout.newTab().setText((categoryname)));
ViewPagerAdapter1 adapter = new ViewPagerAdapter1(getSupportFragmentManager(), items);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
adapter.notifyDataSetChanged();
}
}
Menufragment.class
public class Menufragment extends android.support.v4.app.Fragment {
Context context;
public Menufragment() {
// Required empty public constructor
}
public static Fragment getInstance(int position) {
Menufragment fragment = new Menufragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_menu1, container, false);
int position = getArguments().getInt("position", 0);
// set your list base on position
// list 01x
ListView listview = (ListView) view.findViewById(R.id.Listview1);
System.out.println(MenumainActivity.listitems);
***MenuViewAdapter listViewAdapter = new MenuViewAdapter(getActivity(), MenumainActivity.listitems);***
listview.setAdapter(listViewAdapter);
return view;
}
}
class ViewPagerAdapter1 extends FragmentPagerAdapter {
private JSONArray jsonArray;
public ViewPagerAdapter1(FragmentManager manager, JSONArray jsonArray) {
super(manager);
this.jsonArray = jsonArray;
}
@Override
public Fragment getItem(int position) {
return Menufragment.getInstance(position);
}
@Override
public int getCount() {
return jsonArray.length();
}
}
JSON响应
{ “菜单”:{ “文件”:[{ “ID”:1, “代码”: “hot1_sub1_mnu”, “名称”: “羊肉”, “状态”: “1”, “羊肉”:[ {“id”:1,“code”:“hot1_sub1_mut”,“name”:“Mutton Peper Fry”,“price”:“100”,“veg”:“0”},{“id”:2,“代码“:”hot1_sub2_mut“,”name“:”Mutton Curry“,”price“:”100“,”veg“:”0“},{”id“:3,”code“:”hot1_sub3_mut“,”name“ “:”Mutton Rogan Josh“,”price“:”100“,”veg“:”0“},{”id“:4,”code“:”hot1_sub4_mut“,”name“:”Mutton Mohalai“,” price“:”100“,”veg“:”0“}]},{”id“:2,”code“:”hot1_sub2_mnu“,”name“:”海鲜“,”状态“:”1“, “Sea Food”:[{“id”:1,“code”:“hot1_sub1_sf”,“name”:“Fish Fry”,“price”:“150”,“veg”:“0”},{“id “:2,”code“:”hot1_sub2_sf“,”name“:”Chilly Fish“,”price“:”250“,”veg“:”0“},{”id“:3,”code“:” hot1_sub3_sf“,”name“:”Finger Fish“,”price“:”350“,”veg“:”0“},{”id“:4,”code“:”hot1_sub4_sf“,”name“:”Prawn Pepper Fry“,”price“:”180“,”veg“:”0“},{”id“:5,”code“:”hot1_sub5_sf“,”name“:”Chilly Prawn“,”price“:” 150“,”veg“:”0“},{”id“:6,”code“:”hot1_sub6_sf“,”name“:”Golden Fried Prawn“,”price“:”210“,”veg“:” 0“},{”id“:7,”code“:”hot1_sub7_sf“,”name“:”Fish M alabar Curry“,”price“:”300“,”veg“:”0“},{”id“:8,”code“:”hot1_sub8_sf“,”name“:”Prawn Curry“,”price“:” 130" , “蔬菜”: “0”}]},{ “ID”:3 “代码”: “hot1_sub3_mnu”, “名称”: “Noodels”, “状态”: “1”, “Noodels”:[ {“id”:1,“code”:“hot1_sub1_nd”,“name”:“Chicken Noodels”,“price”:“70”,“veg”:“0”},{“id”:2,“code” “:”hot1_sub2_nd“,”name“:”Egg Noodles“,”price“:”60“,”veg“:”0“},{”id“:3,”code“:”hot1_sub3_nd“,”name“ :“Veg Noodles”,“price”:“55”,“veg”:“1”},{“id”:4,“code”:“hot1_sub4_nd”,“name”:“Mix Noodles”,“price” :“80”,“veg”:“1”},{“id”:5,“code”:“hot1_sub1_nd”,“name”:“Scezwan Chicken Noodles”,“price”:“60”,“veg” : “0”}]},{ “ID”:4 “代码”: “hot1_sub4_mnu”, “姓名”: “蛋”, “状态”: “1”, “蛋”:[{ “ID”:1 ,“code”:“hot1_sub1_egg”,“name”:“Scrambled Egg”,“price”:“20”,“veg”:“0”},{“id”:2,“code”:“hot1_sub2_egg”, “name”:“Omelet”,“price”:“20”,“veg”:“0”},{“id”:3,“code”:“hot3_sub3_egg”,“name”:“Masala Omlete”,“价格“:”25“,”蔬菜“:”0“},{”id“:4,”代码“:”hot3_sub4_egg“,”名称“:”煎蛋(2个)“,”价格“:”20 “ ”蔬菜“: ”0“},{ ”ID“:5 ”代码“: ”hot1_sub5_egg“, ”姓名“:” 水煮Egg(2 nos)“,”price“:”20“,”veg“:”0“},{”id“:6,”code“:”hot1_sub6_egg“,”name“:”Egg Podimas“,”price“ “:”25“,”veg“:”0“},{”id“:7,”code“:”hot1_sub7_egg“,”name“:”Egg Curry / Masala“,”price“:”50“,” veg“:”0“}]},{”id“:5,”code“:”hot1_sub5_mnu“,”name“:”素食菜肴“,”状态“:”1“,”素食菜肴“:[{” id“:1,”code“:”hot1_sub1_veg“,”name“:”Veg。/ Gobi Man.Dry“,”price“:”60“,”veg“:”1“},{”id“:2 ,“code”:“hot1_sub2_veg”,“name”:“Panner Manchoorian Dry”,“price”:“60”,“veg”:“1”},{“id”:3,“code”:“hot1_sub3_veg” ,“name”:“Dhal Fry”,“price”:“50”,“veg”:“1”},{“id”:4,“code”:“hot1_sub4_veg”,“name”:“Mix Veg Curry “,”“price”:“50”,“veg”:“1”},{“id”:5,“code”:“hot1_sub5_veg”,“name”:“Aloo Gobi Masala”,“price”:“50 “,”veg“:”1“},{”id“:6,”code“:”hot1_sub6_veg“,”name“:”Green Peas masala“,”price“:”50“,”veg“:”1 “},{”id“:7,”code“:”hot1_sub7_veg“,”name“:”Chenna Masala“,”price“:”50“,”veg“:”1“},{”id“:8 ,“code”:“hot1_sub8_veg”,“name”:“Kadai vegetable”,“price”:“60”,“veg”:“1”},{“id”:9,“code”:“hot1_sub9_veg”, “名称”:“Veg Kolapuri”,“价格”:“60”,“ve g“:”1“},{”id“:10,”code“:”hot1_sub10_veg“,”name“:”Malai Kuftha Curry“,”price“:”60“,”veg“:”1“}, {“id”:11,“code”:“hot1_sub11_veg”,“name”:“Panner Butter Masala”,“price”:“60”,“veg”:“1”},{“id”:12,“代码“:”hot1_sub11_veg“,”name“:”Mushroom Dry / Gravy“,”price“:”70“,”veg“:”1“},{”id“:13,”code“:”hot1_sub12_veg“, “名称”:“煮熟的蔬菜”,“价格”:“50”,“蔬菜”:“1”}}}}},“状态”:“真实”}
答案 0 :(得分:0)
在for循环中初始化你的列表:
listitems = new ArrayList<Menuclass>(hotelmenu.length());
List<Menuclass> list = new ArrayList<Menuclass>(hotelmenu.length());
此行放入for循环
答案 1 :(得分:0)
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject jsonObject_Menu = jsonObject.getJSONObject("Menu");
JSONArray jsonArray_Items = jsonObject_Menu.getJSONArray("Items");
for(int i=0; i<jsonArray_Items.length(); i++){
JSONArray jsonArray_Mutton = jsonArray_Items.optJSONObject(i).getJSONArray("Mutton");
for(int j=0; j<jsonArray_Mutton.length(); j++){
HashMap<String, String> map = new HashMap<>();
JSONObject jsonObject1 = jsonArray_Mutton.optJSONObject(j);
map.put("name", "name");
map.put("price", "price");
map.put("veg", "veg");
arrayList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
使用像这样的解析,然后在listview中传递arraylist设置值
tvName.setText(arrayList.get(position).get("name"));
答案 2 :(得分:0)
尝试这个解决方案并知道它是否有效。 通过你的清单
SELECT YOUR_SEQ.NEXTVAL FROM DUAL
并相应更改ViewPagerAdapter1 adapter = new ViewPagerAdapter1(getSupportFragmentManager(), items);
。
之后在ViewPagerAdapter1
中传递相同的列表,您可以将其序列化为包含在bundle中。在您的片段中,而不是执行此public static Fragment getInstance(int position)
获取列表,从bundle获取列表并将其传递给您的适配器。
希望它能解决您的问题。