Mohammad Mohammad - 5 months ago 237
JSON Question

How to load json data into fragment in android

I want show 3

fragments
in my
Activity
and load data from
json
in any
fragments
! I want show each json data into one fragment, but in my application load 3
fragments
json
datas in first enter to application.

For Example : when first enter to application load all of
fragments
datas, but i want load data when show this fragment (swipe on that
fragment
).

Fragment 1 codes:

public class free_fragment extends Fragment {

private RecyclerView mRecyclerView;
private free_recycler_adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<DataModel> dataModels = new ArrayList<DataModel>();

private Context context;

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_free_layout, container, false);

context = getContext();

LoadData();

///----- RecyclerView -----
mRecyclerView = (RecyclerView) view.findViewById(R.id.pdf_RecyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new free_recycler_adapter(context, dataModels);
mRecyclerView.setAdapter(mAdapter);


return view;
}

@Subscribe
public void onEvent(MyEvent event) {
List<DataModel> dataModels = event.getInfoModels();

if (event.fragmentTag.equals("forfragment1")) {
mAdapter.add(dataModels);
mAdapter.notifyDataSetChanged();
}
}

private void LoadData() {
freeDataInfo dataInfo = new freeDataInfo();
// here getMainDataInfo() should return the server response
dataInfo.getFreeDataInfo(context);
}

@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}

@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}
}


Fragment 1 AsyncTask codes:

public class freeDataInfo {
private Context mContext;
private String ServerAddress = freeServer_IP.getFreeIP();

public void getFreeDataInfo(Context context) {
mContext = context;
new getInfo().execute(ServerAddress + "limit=10");
}

private class getInfo extends AsyncTask<String, Void, String> {
EventBus bus = EventBus.getDefault();
private String ou_response;
private List<DataModel> infoModels = new ArrayList<>();
private ProgressDialog dialog;

@Override
protected void onPreExecute() {
//CustomProcessDialog.createAndShow(mContext);

dialog = new ProgressDialog(mContext);
this.dialog.setMessage("شکیبا باشید...");
this.dialog.show();
}

@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();

//String url = (String) params[0];
Request request = new Request.Builder()
.url(ServerAddress + "limit=10")
.cacheControl(CacheControl.FORCE_NETWORK)
.build();

Response response;
try {
response = client.newCall(request).execute();
ou_response = response.body().string();
response.body().close();
if (ou_response != null) {
try {
JSONObject postObj = new JSONObject(ou_response);
JSONArray postsArray = postObj.optJSONArray("result");

for (int i = 0; i <= postsArray.length(); i++) {
JSONObject postObject = (JSONObject) postsArray.get(i);

int id = postObject.getInt("id");
Log.d("id", String.valueOf(id));
String title = postObject.getString("title");
String description = postObject.getString("description");
String image = postObject.getString("image");
String category = postObject.getString("categoryName");
String date = postObject.getString("publishDate");

Log.d("Data", "Post ID: " + id);
Log.d("Data", "Post title: " + title);
Log.d("Data", "Post image: " + image);
Log.d("Data", "---------------------------------");

//Use the title and id as per your requirement
infoModels.add(new DataModel(id, title, description, category, date, image));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("error", String.valueOf(e));
}
}
} catch (IOException e) {
e.printStackTrace();
Log.e("error2", String.valueOf(e));
}
return ou_response;
}

@Override
protected void onPostExecute(String result) {
//CustomProcessDialog.dissmis();

//Stop Progress
if (dialog.isShowing()) {
dialog.dismiss();
}

if (result != null) {
bus.post(new MyEvent("forfragment1", infoModels));
} else {
Toast.makeText(mContext, "Empty", Toast.LENGTH_SHORT).show();
}
}
}
}


Fragment 2 codes:

public class paid_fragment extends Fragment {

private RecyclerView mRecyclerView;
private paid_recycler_adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<DataModel> dataModels = new ArrayList<DataModel>();

private Context context;

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_paid_layout, container, false);

context = getContext();

/* if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}*/

LoadData();

///----- RecyclerView -----
mRecyclerView = (RecyclerView) view.findViewById(R.id.voice_RecyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new paid_recycler_adapter(context, dataModels);
mRecyclerView.setAdapter(mAdapter);

return view;
}

@Subscribe
public void onEvent(MyEvent event) {
List<DataModel> dataModels = event.getInfoModels();
/* if (dataModels.size() > 0) {
dataModels.remove(dataModels.size() - 1);
mAdapter.notifyItemRemoved(dataModels.size());
//mAdapter.setLoaded();
}*/
if (event.fragmentTag.equals("forfragment2")) {
mAdapter.add(dataModels);
mAdapter.notifyDataSetChanged();
}
}

private void LoadData() {
paidDataInfo dataInfo = new paidDataInfo();
// here getMainDataInfo() should return the server response
dataInfo.getPaidDataInfo(context);
}

@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}

@Override
public void onPause() {
EventBus.getDefault().unregister(this);
super.onPause();
}
}


Fragment 2 AsyncTask codes:

public class paidDataInfo {
private Context mContext;
private String ServerAddress = paidServer_IP.getPaidIP();

public void getPaidDataInfo(Context context) {
mContext = context;
new getInfo().execute(ServerAddress + "limit=10");
}

private class getInfo extends AsyncTask<String, Void, String> {
EventBus bus = EventBus.getDefault();
private String ou_response;
private List<DataModel> infoModels = new ArrayList<>();
private ProgressDialog dialog;

@Override
protected void onPreExecute() {
//CustomProcessDialog.createAndShow(mContext);
//infoModels = new ArrayList<>();

// Initiate Progress
dialog = new ProgressDialog(mContext);
this.dialog.setMessage("شکیبا باشید...");
this.dialog.show();

infoModels.clear();
}

@Override
protected String doInBackground(String... params) {
OkHttpClient client = new OkHttpClient();

//String url = (String) params[0];
Request request = new Request.Builder()
.url(ServerAddress + "limit=10")
.cacheControl(CacheControl.FORCE_NETWORK)
.build();

Response response;
try {
response = client.newCall(request).execute();
ou_response = response.body().string();
response.body().close();
if (ou_response != null) {
try {
JSONObject postObj = new JSONObject(ou_response);
JSONArray postsArray = postObj.optJSONArray("result");
infoModels = new ArrayList<>();

for (int i = 0; i <= postsArray.length(); i++) {
JSONObject postObject = (JSONObject) postsArray.get(i);

int id = postObject.getInt("id");
Log.d("id", String.valueOf(id));
String title = postObject.getString("title");
String description = postObject.getString("full_description");
String image = postObject.getString("image");
String category = postObject.getString("categoryName");
String date = postObject.getString("publishDate");

Log.d("Data", "Post ID: " + id);
Log.d("Data", "Post title: " + title);
Log.d("Data", "Post Desc: " + description);
Log.d("Data", "Post image: " + image);
Log.d("Data", "---------------------------------");

//Use the title and id as per your requirement
infoModels.add(new DataModel(id, title, description, category, date, image));
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("error", String.valueOf(e));
}
}
} catch (IOException e) {
e.printStackTrace();
Log.e("error2", String.valueOf(e));
}
return ou_response;
}

@Override
protected void onPostExecute(String result) {
//Stop Progress
if (dialog.isShowing()) {
dialog.dismiss();
}
//CustomProcessDialog.dissmis();
if (result != null) {
bus.post(new MyEvent("forfragment2", infoModels));
} else {
Toast.makeText(mContext, "Empty", Toast.LENGTH_SHORT).show();
}
}
}
}


MainActivity codes :

public class Main_Page extends AppCompatActivity {

private CollapsingToolbarLayout mCollapsingToolbarLayout;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main__page);

mCollapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
//mCollapsingToolbarLayout.setTitle(getResources().getString(R.string.app_name));

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}

/**
* Adding custom view to tab
*/
private void setupTabIcons() {

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText(R.string.free_fragment_title);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_download_image, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);

TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText(R.string.paid_fragment_title);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_paid_download_image, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);

TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText(R.string.pdf_fragment_title);
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_pdf_icon, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
}

/**
* Adding fragments to ViewPager
* @param viewPager
*/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new free_fragment(), "رایگان ها");
adapter.addFrag(new paid_fragment(), "پرداختی ها");
adapter.addFrag(new pdf_fragment(), "مقالات");
viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}

@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}


How can i fix this and load fragment data just show that fragment ? thanks all <3

Answer

There is a callback method which gets invoked when the fragment is visible to the user. You can trigger your loadData() in this method. Have a boolean which tells you if data is already fetched. Refer the below lines of code :

In your fragment code add the following method.

@Override 
 public void setUserVisibleHint(boolean isVisibleToUser) { 
    mIsVisibleToUser = isVisibleToUser; 
    if (isVisibleToUser && !isDataFetched && getContext() != null) { 
    context = getContext(); 
    LoadData(); //Remove this call from onCreateView 
    }  
}

In onEvent method make the following change.

@Subscribe
public void onEvent(MyEvent event) {
    List<DataModel> dataModels = event.getInfoModels();

    if (event.fragmentTag.equals("forfragment1")) {
        mAdapter.add(dataModels);
        isDataFetched = true;
        mAdapter.notifyDataSetChanged();
    }
}

Add instance variable in your fragment code.

private boolean isDataFetched;
private boolean mIsVisibleToUser;

Finally modify OnCreateView()

   if(mIsVisibleToUser)
       LoadData();

Make sure you do these changes for both the fragments.