我正在尝试创建一个动态的tablayout。我从json获取值,我能够制作选项卡,但我想在这些选项卡中添加值(List)。值(List)也是json格式。我没有得到如何实现它。
这是我的json格式
{ "Categories": [ { "cat_id": "17", "cat_name": "Bridal Costume ", "sub_categories": [] }, { "cat_id": "18", "cat_name": "Jewellery", "sub_categories": [] }, { "cat_id": "19", "cat_name": "Office\/Godown", "sub_categories": [] }, { "cat_id": "20", "cat_name": "House\/Flat", "sub_categories": [] }, { "cat_id": "21", "cat_name": "Car\/Bike", "sub_categories": [] }, { "cat_id": "22", "cat_name": "Events", "sub_categories": [ { "cat_id": "22", "sub_cat_name": "Tent House" } ] }, { "cat_id": "23", "cat_name": "Fancy Dress", "sub_categories": [] }, { "cat_id": "24", "cat_name": "Other", "sub_categories": [ { "cat_id": "24", "sub_cat_name": "Experiences" }, { "cat_id": "24", "sub_cat_name": "Laptop" } ] } ] }
在这里我得到cat_name
作为选项卡名称,sub_categories
作为选项卡内的值(列表)。
这里我从JSONFormat接收值,在接收到值后,我将这些值发送到Fragment parentclass。这是在下面声明
public void detail(){
String category = null,cat_img,cat_id = null,sub_cat = null,s_name = null;
JSONObject c = null;
try {
JSONObject object = new JSONObject(myJSON);
JSONArray Jarray = object.getJSONArray("Categories");
for(int i=0;i<Jarray.length();i++){
c = Jarray.getJSONObject(i);
JSONArray moreDetails = c.getJSONArray("sub_categories");
category = c.getString("cat_name");
cat_id = c.getString("cat_id");
sub_cat = c.getString("sub_categories");
fragmentParent.addPage(category,cat_id);
for (int i2 = 0; i2 < moreDetails.length(); i2++) {
JSONObject detail = moreDetails.getJSONObject(i2);
s_name = detail.getString("sub_cat_name");
String s_id = detail.getString("cat_id");
fragmentParent.addlist(s_name,s_id);
Toast.makeText(this, s_name, Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
FragmentParent.class,接收到值后,我将它发送给FragmentChild Class。
public class FragmentParent extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPagerAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_parent, container, false);
getIDs(view);
setEvents();
return view;
}
private void getIDs(View view) {
viewPager = (ViewPager) view.findViewById(R.id.my_viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.my_tab_layout);
adapter = new ViewPagerAdapter(getFragmentManager(), getActivity(), viewPager, tabLayout);
viewPager.setAdapter(adapter);
}
int selectedTabPosition;
private void setEvents() {
tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
@Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
viewPager.setCurrentItem(tab.getPosition());
selectedTabPosition = viewPager.getCurrentItem();
Log.d("Selected", "Selected " + tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
super.onTabUnselected(tab);
Log.d("Unselected", "Unselected " + tab.getPosition());
}
});
}
public void addPage(String pagename,String id) {
Bundle bundle = new Bundle();
bundle.putString("data", pagename);
bundle.putString("c_id", id);
FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setArguments(bundle);
adapter.addFrag(fragmentChild, pagename);
adapter.notifyDataSetChanged();
if (adapter.getCount() > 0) tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(adapter.getCount() - 1);
setupTabLayout();
}
public void addlist(String listname,String id) {
Bundle bundle = new Bundle();
bundle.putString("list_name", listname);
bundle.putString("s_id", id);
}
public void setupTabLayout() {
selectedTabPosition = viewPager.getCurrentItem();
for (int i = 0; i < tabLayout.getTabCount(); i++) {
tabLayout.getTabAt(i).setCustomView(adapter.getTabView(i));
}
}
}
这就是FragmentChild类,我将在其中显示标签
中的值FragmentChild.class
public class FragmentChild extends Fragment {
String childname,img_name,cid,s_list,s_id;
TextView textViewChildName;
EditText editText;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_child, container, false);
Bundle bundle = getArguments();
childname = bundle.getString("data");
img_name = bundle.getString("gmi");
cid = bundle.getString("c_id");
s_list = bundle.getString("list_name");
s_id = bundle.getString("s_id");
getIDs(view);
return view;
}
private void getIDs(View view) {
textViewChildName = (TextView) view.findViewById(R.id.textViewChild);
textViewChildName.setText(childname+"-"+cid);
}
}
这是我的输出
答案 0 :(得分:0)
您的addlist
子类别方法会将值放在新的Bundle
中,而这些值在任何地方都没有使用。猜测最好使用不必要的fragmentParent.addPage
参数创建Bundle
函数,该参数应添加到fragmentChild.setArguments(bundle)
调用中。