ViewPager data not loading on initial app load

后端 未结 2 1417
梦毁少年i
梦毁少年i 2020-12-22 12:54

i am working on a app which i created using android studio Tabbed Activity i choose this activity so that when user swipe it load some data from a json url

相关标签:
2条回答
  • 2020-12-22 13:30

    Update your both classes like that may be this help you!

    fetchDataclass:

    public class fetchData extends AsyncTask<Void,Void,Void> {
        String data ="";
        String factTitle = "";
        String factDesc = "";
        String factImg ="";
        String singleParsed ="";
        String DoubleParsed ="";
        String tripleParsed ="";
        Activity mContext;
        TextView mfactTitle,mfactDesc;
        ImageView mfactImg;
        ProgressBar mProgressbar;
    
       public fetchData(Activity context,TextView mfactTitle,TextView 
       mfactDesc,ImageView mfactImg,ProgressBar mProgressbar){
       this.context=context;
       this.mfactTitle=mfactTitle; 
       this.mfactDesc=mfactDesc;
       this.mfactImg=mfactImg;
       this.mProgressbar=mProgressbar;
       }
        @Override
        protected Void doInBackground(Void... voids) {
            try {
                URL url = new URL("http://xxxxxx.com/api");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                while(line != null){
                    line = bufferedReader.readLine();
                    data = data + line;
                }
                JSONArray JA = new JSONArray(data);
                for(int i =0 ;i <JA.length(); i++){
                    JSONObject JO = (JSONObject) JA.get(i);
                    singleParsed =  ""+JO.get("fact");
                    DoubleParsed =  ""+JO.get("factdesc");
                    tripleParsed =  ""+JO.get("img");
                    factTitle = factTitle + singleParsed;
                    factDesc  = factDesc + DoubleParsed;
                    factImg  = factImg + tripleParsed;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            mfactImg.setVisibility(View.VISIBLE);
            mfactTitle.setText(this.factTitle);
            mfactDesc.setText(this.factDesc);
         Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(mfactImg);
           mProgressBar.setVisibility(View.INVISIBLE);
        }
    }
    

    PlaceHolder Fragment:

    public static PlaceholderFragment newInstance(int sectionNumber) {
                PlaceholderFragment fragment = new PlaceholderFragment();
                Bundle args = new Bundle();
                args.putInt(ARG_SECTION_NUMBER, sectionNumber);
                fragment.setArguments(args);
                return fragment;
            }
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                factTitle = (TextView) rootView.findViewById(R.id.fact_label);
                factDesc = (TextView) rootView.findViewById(R.id.fact_description);
                factImg = (ImageView) rootView.findViewById(R.id.imageView);
                loader = (ProgressBar) rootView.findViewById(R.id.progressBar);
                fetchData process = new fetchData(getActivity(),factTitle,factDesc,factImg,loader);
                process.execute();
                return rootView;
            }
        }
    
    0 讨论(0)
  • 2020-12-22 13:38

    You have to make network call first and once you receive response refresh the Viewpager or set Adapter.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Add below two lines in on create
            fetchData process = new fetchData();
            process.execute();
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    
    }
    

    And on Post execute of async task, set adapter or refresh page

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    
        MainActivity.mViewPager.setAdapter(mSectionsPagerAdapter);
    
        MainActivity.factImg.setVisibility(View.VISIBLE);
        MainActivity.factTitle.setText(this.factTitle);
        MainActivity.factDesc.setText(this.factDesc);
        Picasso.get().load(factImg).placeholder(R.drawable.defaultthumb).into(MainActivity.factImg);
        MainA
    
    0 讨论(0)
提交回复
热议问题