Tuesday 14 January 2020

Fragment in ViewPager using FragmentPagerAdapter is blank the first time activity is loaded

I'm creating an app with vertical page adapter using FragmentStatePagerAdapter. The big issue i'm facing is, data is not displayed on the textview on first app launch but is displayed on scrolling the page. I believe the fragment view is delaying to create textview because, on my LoadAlbumDataCompleted() function inside Fragmentone.class, i'm able to print the data returned or also output via toast but is not getting populated to the textview.

Kindly help.

MainActivity.class

public class MainActivity extends FragmentActivity implements LoadAalbumsTotalListener {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
                mPager.setAdapter(mAdapter);

             LoadTotalAlbumsNum.BindAlbumsTotalListener(this);

        }

@Override
    public void OnLoadAlbumsCompleted(String total) {

        if(total.trim().equalsIgnoreCase("")){
            NUMBER_OF_PAGES=0;
        }else{
            NUMBER_OF_PAGES=Integer.parseInt(total.trim());
        }

        mAdapter = new MainActivityVSlideAdapter(this, getSupportFragmentManager(), NUMBER_OF_PAGES);
        mPager.setAdapter(mAdapter);

    }
}

MainActivityVSlideAdapter.class Adapter

public class MainActivityVSlideAdapter extends FragmentStatePagerAdapter {

    static int NUMBER_OF_PAGES;
    private Context con;

    public MainActivityVSlideAdapter(Context con, FragmentManager fm, int NUMBER_OF_PAGES) {
        super(fm);
        this.con=con;
        this.NUMBER_OF_PAGES=NUMBER_OF_PAGES;


    }

    @Override
    public int getCount() {
        return NUMBER_OF_PAGES;
    }

    @Override
    public Fragment getItem(int position) {

        return FragmentOne.newInstance(position);
    }
}

Fragmentone.class

public class FragmentOne extends Fragment implements LoadAlbumDataListener {

    private static final String MY_NUM_KEY = "num";

    private int mNum;
    private TextView SaloonName;
    private TextView location;


    // You can modify the parameters to pass in whatever you want
    public static FragmentOne newInstance(int num) {
        FragmentOne f = new FragmentOne();
        Bundle args = new Bundle();
        args.putInt(MY_NUM_KEY, num);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //get argument from
        mNum = getArguments() != null ? getArguments().getInt(MY_NUM_KEY) : 0;
        session=new Session(getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_one, container, false);

        Methods methods=new Methods(getActivity());

       // v.setBackgroundColor(mColor);

         SaloonName = v.findViewById(R.id.SaloonName);
        location=v.findViewById(R.id.location);


        new LoadAlbumData(getActivity()).execute(getString(R.string.urlAddress)+"load-album-data.php", String.valueOf(mNum));

        LoadAlbumData.BindLoadAlbumDataListener(this);

        return v;
    }


    @Override
    public void LoadAlbumDataCompleted(String s) {


        JSONArray jsonPicsArray = null;
        JSONObject jsonObj;
        String  BusinessLocation=null;

        try {
            jsonPicsArray = new JSONArray(s);

                businessName = jsonObj.getString("businessName");
                BusinessLocation = jsonObj.getString("location");;
            }

          Toast.makeText(getActivity(), businessName, Toast.LENGTH_LONG).show();

           SaloonName.setText(businessName);
            location.setText(BusinessLocation);



        } catch (JSONException e) {
            e.printStackTrace();
        }

    }    
}


from Fragment in ViewPager using FragmentPagerAdapter is blank the first time activity is loaded

No comments:

Post a Comment