当前位置: 首页 > 面试题库 >

初始应用加载时未加载ViewPager数据

东方森
2023-03-14
问题内容

我正在使用我使用android studio Tabbed Activity 创建的应用程序上工作,我选择了此活动,以便在用户滑动时从json
url加载一些数据,并且我创建了另一个类,该类可以在onCreateView(LayoutInflater inflater, ViewGroup container方法上获取JSON数据,并且所有这些都可以正常工作,除非在应用程序时从调用方法的主要活动开始,并且当我调用mViewPager.setAdapter(mSectionsPagerAdapter)布局时未填充任何数据时
,我想要的是加载应用程序MainActivity时要显示的数据,仅当我从右向左滑动时才显示

主要活动

public class MainActivity extends AppCompatActivity {
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    static TextView factTitle;
    static TextView factDesc;
    static ImageView factImg;
    static ProgressBar loader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";
        public PlaceholderFragment() {
        }
        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();
            process.execute();
            return rootView;
        }
    }
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
          Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }
        @Override
        public int getCount() {
            return 999;
        }
    }
}

fetchData类别

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data ="";
    String factTitle = "";
    String factDesc = "";
    String factImg ="";
    String singleParsed ="";
    String DoubleParsed ="";
    String tripleParsed ="";
    @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);
        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);
        MainActivity.loader.setVisibility(View.INVISIBLE);
    }
}

问题答案:

像这样更新两个类,可能对您有帮助!

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片段:

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;
        }
    }


 类似资料:
  • 问题内容: 我想知道在应用程序启动之前加载初始数据库数据的最佳方法是什么?我正在寻找的东西将用数据填充我的H2数据库。 例如,我有一个域模型“ User”,我可以通过转到/ users来访问用户,但是最初数据库中没有任何用户,所以我必须创建它们。无论如何,是否有自动用数据填充数据库的信息? 目前,我有一个由容器实例化并为我创建用户的Bean。 例: 但是我非常怀疑这是最好的方法。还是? 问题答案:

  • 我想知道在应用程序启动之前加载初始数据库数据的最佳方法是什么?我正在寻找的是一些东西,将填补我的H2数据库与数据。 例如,我有一个域模型“user”我可以通过转到/users来访问用户,但最初数据库中没有任何用户,所以我必须创建它们。是否存在自动填充数据库的数据? 但我很怀疑这是最好的方法。还是真的?

  • 我有一个关于初始化的有趣问题。我有以下代码: 现在当我调用ErrorLookupProvider时。getInstance(),我碰到了一个NPE。中的映射未使用新的初始化。 如果我将的声明更改为final,那么我看到它已初始化。或者,即使我删除static并将其作为私有类变量

  • 我在开发环境中使用嵌入式MongoDB数据库。它在应用程序启动时设置为空。我想在设置Spring上下文时加载应用程序所需的初始数据。 Spring Data MongoDB中是否有方法指向将加载到数据库中的JSON文件(类似于hibernate中的import.sql(hibernate.hbm2ddl.import_文件)或NoSql单元中的@UsingDataSet)?

  • 问题内容: 我目前正在使用WordPress REST API和vue-router在小型单页网站上的页面之间进行转换。但是,当我使用REST API对服务器进行AJAX调用时,数据将加载,但仅在页面已呈现之后才加载。 该VUE路由器文档提供了深入了解在关于如何前和导航到各航线后加载数据,但我想知道如何加载在初始页面加载的所有路线和页面数据,绕过需要每个负载数据激活路线的时间。 请注意,我正在将数