java – 在初始应用程序加载时未加载ViewPager数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在初始应用程序加载时未加载ViewPager数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6697字,纯文字阅读大概需要10分钟。
内容图文
我正在使用android studio Tabbed Activity创建的应用程序我选择此活动,以便当用户滑动它从json url加载一些数据时,我创建了另一个在onCreateView上获取JSON数据的类(LayoutInflater inflater,ViewGroup容器方法和这一切都很好,除了当应用程序启动时和创建方法上的主要活动,当我调用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;
}
}
内容总结
以上是互联网集市为您收集整理的java – 在初始应用程序加载时未加载ViewPager数据全部内容,希望文章能够帮你解决java – 在初始应用程序加载时未加载ViewPager数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。