ViewPager 数据未在初始应用加载时加载
我正在开发一个使用 android studio 创建的应用程序 Tabbed Activity 我选择了这个活动,以便当用户滑动它时,它会从 json url 加载一些数据,然后我创建了另一个获取 JSON 数据的类在 onCreateView(LayoutInflater inflater, ViewGroup container
方法上,这一切正常,除了当应用程序启动时和从创建方法上的主要活动时调用 mViewPager.setAdapter(mSectionsPagerAdapter)
没有数据填充布局我想要的是当应用程序 MainActivity 加载时我想显示只有当我从右向左滑动时才显示的数据
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 and i created another class which fetch JSON data on onCreateView(LayoutInflater inflater, ViewGroup container
method and this all works fine except when app starts and from main activity on create method when i call mViewPager.setAdapter(mSectionsPagerAdapter)
no data is filled in layout what i want is when app MainActivity loaded i want to show data which only now show when i swipe from right to left
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);
}
}
推荐答案
像这样更新你的两个类可能对你有帮助!
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 片段:
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;
}
}
相关文章