我正在使用ViewPager,它有多个幻灯片(Fragment)每个片段类我想从JSON URL获取值并在Fragment页面中设置..任何正文请帮助我..
这是我的主要课程
public class OnLive extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewpager;
FragmentPageAdapter ft;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
try
{
viewpager = (ViewPager) findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionBar = getActionBar();
viewpager.setAdapter(ft);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Tab1").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab2").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab3").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab3").setTabListener(this));
}
catch(Exception e)
{
e.printStackTrace();
}
viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int arg0)
{
actionBar.setSelectedNavigationItem(arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
FragmentTransaction fragMentTra = null;
@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
viewpager.setCurrentItem(arg0.getPosition());
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_action_bar_main, menu);
return true;
}}
这是我的页面适配器类
public class FragmentPageAdapter extends FragmentPagerAdapter{
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Orders();
case 1:
return new Closings();
case 2:
return new Quotes();
case 3:
return new Encompass();
default:
break;
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}}
这是我的第一个Fragment类
public class Orders extends Fragment {
private static ProgressDialog pDialog;
private static String url = "http://api.androidhive.info/contacts/";
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
public ArrayList<HashMap<String, String>> contactList ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
new GetContacts().execute();
contactList = new ArrayList<HashMap<String, String>>();
ListView listView = new ListView(getActivity());
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1);
for (String str : contactList)
array.add(str);
listView.setAdapter(array);
return listView;
}
class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
contact.put(TAG_PHONE_MOBILE, mobile);
contactList.add(contact);
}
if (pDialog.isShowing()){
pDialog.dismiss();
}
// if(contactList != null){
// for(HashMap<String, String> map: contactList)
// for(Entry<String, String> mapEntry: map.entrySet()) {
// String key = mapEntry.getKey();
// String value = mapEntry.getValue();
// System.out.println(" key is >>>>> "+key);
// System.out.println("value is <<<<< "+value);
// }
//
// }
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
}}
在Fragment类的GetContect类调用但是没有给出任何值..请给我解决这个问题..
答案 0 :(得分:0)
首先想到的是你正在做的事情:
new GetContacts().execute();
contactList = new ArrayList<HashMap<String, String>>();
在调用GetContacts()。execute()之后,将contactList初始化为空列表。在定义变量的同一点将contactList初始化为空列表会更合理,以确保在已经解析之后不会清空它。
同样,当您使用异步任务时,您也不知道它何时完成。
http://developer.android.com/reference/android/os/AsyncTask.html
您当前没有实现onPostExecute()方法,该方法应在作业最终完成时触发。您正在处理代码中的异步调用,就好像它是同步的一样。您应该将以下代码移动到onPostExecute方法,以确保仅在调用结束并且有数据后才继续。