正在获取表格布局片段中的 JSON 数据 returns null
Fetching JSON data returns null in fragment of a tablayout
我的DynamicFragment.java
如下
public class DynamicFragment extends Fragment {
public static DynamicFragment newInstance() {
return new DynamicFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// adding the layout with inflater
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dynamic, container, false);
initViews(view);
return view;
}
// initialise the categories
private void initViews(View view) {
TextView textView = view.findViewById(R.id.commonTextView);
textView.setText(String.valueOf("Category : " + getArguments().getInt("position")));
TextView userName = view.findViewById(R.id.userName);
userName.setText("Name :" + getArguments().getString("name"));
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
// pause function call
@Override
public void onPause() {
super.onPause();
}
// resume function call
@Override
public void onResume() {
super.onResume();
}
// stop when we close
@Override
public void onStop() {
super.onStop();
}
// destroy the view
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
DynamicFragmentAdapter.java
如下:
public class DynamicFragmentAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
DynamicFragmentAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
// get the current item with position number
@Override
public Fragment getItem(int position) {
Bundle b = new Bundle();
b.putInt("position", position);
Fragment frag = DynamicFragment.newInstance();
frag.setArguments(b);
return frag;
}
// get total number of tabs
@Override
public int getCount() {
return mNumOfTabs;
}
}
DynamicActivity.java
如下:
public class DynamicActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
initViews();
}
private void initViews() {
// initialise the layout
viewPager = findViewById(R.id.viewpager);
mTabLayout = findViewById(R.id.tabs);
// setOffscreenPageLimit means number
// of tabs to be shown in one page
viewPager.setOffscreenPageLimit(5);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// setCurrentItem as the tab position
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
setDynamicFragmentToTabLayout();
}
// show all the tab using DynamicFragmnetAdapter
private void setDynamicFragmentToTabLayout() {
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(loadJSONFromAsset());
// fetch JSONArray named contacts
JSONArray userArray = obj.getJSONArray("contacts");
int arrayLength = userArray.length();
// implement for loop for getting users list data
for (int i = 0; i < arrayLength; i++) {
// create a JSONObject for fetching single user data
JSONObject userDetail = userArray.getJSONObject(i);
// fetch name and store it in arraylist
MainActivity.personNames.add(userDetail.getString("name"));
// set the tab name as "Page: " + i
mTabLayout.addTab(mTabLayout.newTab().setText("Page:" + i));
}
} catch (JSONException e) {
e.printStackTrace();
}
DynamicFragmentAdapter mDynamicFragmentAdapter = new DynamicFragmentAdapter(getSupportFragmentManager(), mTabLayout.getTabCount());
// set the adapter
viewPager.setAdapter(mDynamicFragmentAdapter);
// set the current item as 0 (when app opens for first time)
viewPager.setCurrentItem(0);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("contact_data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
虽然我已经定义了 TextView
userName,但 name
不会从 JSON 加载并在片段中显示为“空” fragment_dynamic.xml
如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:id="@+id/commonTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="zxjhcvzxvchjzxbhj"
android:textColor="@color/purple_500"
android:textStyle="bold" />
<TextView
android:id="@+id/userName"
android:layout_below="@id/commonTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="zxjhcvzxvchjzxbhj"
android:textColor="@color/purple_500"
android:textStyle="bold" />
</RelativeLayout>
我截取的contacts.json如下:
{
"contacts": [
{
"id": "1",
"name": "Amitabh"
"age": "25"
},
{
"id": "2",
"name": "Amit"
"age": "30"
}
]
}
当我从 FOR 循环中轻扫片段时,选项卡的位置编号会正确更新,但名称在所有片段中都显示为“null”。我不熟悉 JSON
概念及其在 Fragments
和 Tablayouts
中的使用。在这方面的任何帮助都会有很大的帮助。
我认为我的 JSON 数据中的变量名称与 java class 中的变量没有链接,不知道该怎么做。
提前致谢!
尝试将 personNames
存储在局部变量中并将其传递给适配器:
ArrayList<String> personNames = new ArrayList<String>()
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(loadJSONFromAsset());
// fetch JSONArray named contacts
JSONArray userArray = obj.getJSONArray("contacts");
int arrayLength = userArray.length();
// implement for loop for getting users list data
for (int i = 0; i < arrayLength; i++) {
// create a JSONObject for fetching single user data
JSONObject userDetail = userArray.getJSONObject(i);
// fetch name and store it in arraylist
personNames.add(userDetail.getString("name"));
// set the tab name as "Page: " + i
mTabLayout.addTab(mTabLayout.newTab().setText("Page:" + i));
}
} catch (JSONException e) {
e.printStackTrace();
}
DynamicFragmentAdapter mDynamicFragmentAdapter = new DynamicFragmentAdapter(getSupportFragmentManager(), mTabLayout.getTabCount(), personNames);
// set the adapter
viewPager.setAdapter(mDynamicFragmentAdapter);
然后,从适配器将 name
参数向下传递给 DynamicFragment
:
public class DynamicFragmentAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
private ArrayList<String> personNames;
DynamicFragmentAdapter(FragmentManager fm, int NumOfTabs, ArrayList<String> personNames) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.personNames = personNames;
}
// get the current item with position number
@Override
public Fragment getItem(int position) {
Bundle b = new Bundle();
b.putInt("position", position);
b.putString("name", personNames[position])
Fragment frag = DynamicFragment.newInstance();
frag.setArguments(b);
return frag;
}
最后,您应该能够访问 Fragment
中的名称(就像您在做的那样):
getArguments().getString("name")
我的DynamicFragment.java
如下
public class DynamicFragment extends Fragment {
public static DynamicFragment newInstance() {
return new DynamicFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// adding the layout with inflater
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dynamic, container, false);
initViews(view);
return view;
}
// initialise the categories
private void initViews(View view) {
TextView textView = view.findViewById(R.id.commonTextView);
textView.setText(String.valueOf("Category : " + getArguments().getInt("position")));
TextView userName = view.findViewById(R.id.userName);
userName.setText("Name :" + getArguments().getString("name"));
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
// pause function call
@Override
public void onPause() {
super.onPause();
}
// resume function call
@Override
public void onResume() {
super.onResume();
}
// stop when we close
@Override
public void onStop() {
super.onStop();
}
// destroy the view
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
DynamicFragmentAdapter.java
如下:
public class DynamicFragmentAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
DynamicFragmentAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
// get the current item with position number
@Override
public Fragment getItem(int position) {
Bundle b = new Bundle();
b.putInt("position", position);
Fragment frag = DynamicFragment.newInstance();
frag.setArguments(b);
return frag;
}
// get total number of tabs
@Override
public int getCount() {
return mNumOfTabs;
}
}
DynamicActivity.java
如下:
public class DynamicActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout mTabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic);
initViews();
}
private void initViews() {
// initialise the layout
viewPager = findViewById(R.id.viewpager);
mTabLayout = findViewById(R.id.tabs);
// setOffscreenPageLimit means number
// of tabs to be shown in one page
viewPager.setOffscreenPageLimit(5);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// setCurrentItem as the tab position
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
setDynamicFragmentToTabLayout();
}
// show all the tab using DynamicFragmnetAdapter
private void setDynamicFragmentToTabLayout() {
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(loadJSONFromAsset());
// fetch JSONArray named contacts
JSONArray userArray = obj.getJSONArray("contacts");
int arrayLength = userArray.length();
// implement for loop for getting users list data
for (int i = 0; i < arrayLength; i++) {
// create a JSONObject for fetching single user data
JSONObject userDetail = userArray.getJSONObject(i);
// fetch name and store it in arraylist
MainActivity.personNames.add(userDetail.getString("name"));
// set the tab name as "Page: " + i
mTabLayout.addTab(mTabLayout.newTab().setText("Page:" + i));
}
} catch (JSONException e) {
e.printStackTrace();
}
DynamicFragmentAdapter mDynamicFragmentAdapter = new DynamicFragmentAdapter(getSupportFragmentManager(), mTabLayout.getTabCount());
// set the adapter
viewPager.setAdapter(mDynamicFragmentAdapter);
// set the current item as 0 (when app opens for first time)
viewPager.setCurrentItem(0);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("contact_data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
虽然我已经定义了 TextView
userName,但 name
不会从 JSON 加载并在片段中显示为“空” fragment_dynamic.xml
如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:id="@+id/commonTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="zxjhcvzxvchjzxbhj"
android:textColor="@color/purple_500"
android:textStyle="bold" />
<TextView
android:id="@+id/userName"
android:layout_below="@id/commonTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="zxjhcvzxvchjzxbhj"
android:textColor="@color/purple_500"
android:textStyle="bold" />
</RelativeLayout>
我截取的contacts.json如下:
{
"contacts": [
{
"id": "1",
"name": "Amitabh"
"age": "25"
},
{
"id": "2",
"name": "Amit"
"age": "30"
}
]
}
当我从 FOR 循环中轻扫片段时,选项卡的位置编号会正确更新,但名称在所有片段中都显示为“null”。我不熟悉 JSON
概念及其在 Fragments
和 Tablayouts
中的使用。在这方面的任何帮助都会有很大的帮助。
我认为我的 JSON 数据中的变量名称与 java class 中的变量没有链接,不知道该怎么做。
提前致谢!
尝试将 personNames
存储在局部变量中并将其传递给适配器:
ArrayList<String> personNames = new ArrayList<String>()
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(loadJSONFromAsset());
// fetch JSONArray named contacts
JSONArray userArray = obj.getJSONArray("contacts");
int arrayLength = userArray.length();
// implement for loop for getting users list data
for (int i = 0; i < arrayLength; i++) {
// create a JSONObject for fetching single user data
JSONObject userDetail = userArray.getJSONObject(i);
// fetch name and store it in arraylist
personNames.add(userDetail.getString("name"));
// set the tab name as "Page: " + i
mTabLayout.addTab(mTabLayout.newTab().setText("Page:" + i));
}
} catch (JSONException e) {
e.printStackTrace();
}
DynamicFragmentAdapter mDynamicFragmentAdapter = new DynamicFragmentAdapter(getSupportFragmentManager(), mTabLayout.getTabCount(), personNames);
// set the adapter
viewPager.setAdapter(mDynamicFragmentAdapter);
然后,从适配器将 name
参数向下传递给 DynamicFragment
:
public class DynamicFragmentAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
private ArrayList<String> personNames;
DynamicFragmentAdapter(FragmentManager fm, int NumOfTabs, ArrayList<String> personNames) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.personNames = personNames;
}
// get the current item with position number
@Override
public Fragment getItem(int position) {
Bundle b = new Bundle();
b.putInt("position", position);
b.putString("name", personNames[position])
Fragment frag = DynamicFragment.newInstance();
frag.setArguments(b);
return frag;
}
最后,您应该能够访问 Fragment
中的名称(就像您在做的那样):
getArguments().getString("name")