我叫json。
我成功创建了Tabhost(viewpager),我在viewpager中有3个片段,每个片段我解析json并将其显示到listview(图像和文本)。
我编写的代码可以解析每个片段的json(不同的只是URL)。它工作但我有一个问题:每个片段我使用Asynctask从服务器解析json。我的问题是所有三个asynctask类同时运行。
当程序启动时,这三个Asynctask同时运行,这是我的代码
public class MainActivity extends FragmentActivity implements
TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
private TabHost mTabHost;
private ViewPager mViewPager;
private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MainActivity.TabInfo>();
private PagerAdapter mPagerAdapter;
private HorizontalScrollView horizontalScrollView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
this.initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
this.intialiseViewPager();
}
protected void onSaveInstanceState(Bundle outState) {
outState.putString("tab", mTabHost.getCurrentTabTag());
super.onSaveInstanceState(outState);
}
private void intialiseViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, SendItemsFragment.class.getName()));
fragments.add(Fragment.instantiate(this, RecivedItemsFragment.class.getName()));
fragments.add(Fragment.instantiate(this, FavoriteItemsFragment.class.getName()));
this.mPagerAdapter = new PagerAdapter(
super.getSupportFragmentManager(), fragments);
//
this.mViewPager = (ViewPager) super.findViewById(R.id.viewpager);
this.horizontalScrollView = (HorizontalScrollView) super
.findViewById(R.id.horizontalScrollView);
this.mViewPager.setAdapter(this.mPagerAdapter);
this.mViewPager.setOnPageChangeListener(this);
}
public void centerTabItem(int position) {
mTabHost.setCurrentTab(position);
final TabWidget tabWidget = mTabHost.getTabWidget();
final int screenWidth = getWindowManager().getDefaultDisplay()
.getWidth();
final int leftX = tabWidget.getChildAt(position).getLeft();
int newX = 0;
newX = leftX + (tabWidget.getChildAt(position).getWidth() / 2)
- (screenWidth / 2);
if (newX < 0) {
newX = 0;
}
horizontalScrollView.scrollTo(newX, 0);
}
private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabInfo tabInfo = null;
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Send").setIndicator(
"Send"), (tabInfo = new TabInfo("Send",
SendItemsFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(
this,
this.mTabHost,
this.mTabHost.newTabSpec("Recived").setIndicator(
"Recived"), (tabInfo = new TabInfo("Recived",
RecivedItemsFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
MainActivity.AddTab(this, this.mTabHost,
this.mTabHost.newTabSpec("Favourite").setIndicator("Favourite"),
(tabInfo = new TabInfo("Favourite", FavoriteItemsFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);
mTabHost.setOnTabChangedListener(this);
}
private static void AddTab(MainActivity activity, TabHost tabHost,
TabHost.TabSpec tabSpec, TabInfo tabInfo) {
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}
public void onTabChanged(String tag) {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
//tv.setTextColor(getResources().getColor(R.color.sa_green));
}
int pos = this.mTabHost.getCurrentTab();
this.mViewPager.setCurrentItem(pos);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
centerTabItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
private class TabInfo {
private String tag;
private Class<?> clss;
private Bundle args;
private Fragment fragment;
TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clss = clazz;
this.args = args;
}
}
class TabFactory implements TabContentFactory {
private final Context mContext;
public TabFactory(Context context) {
mContext = context;
}
public View createTabContent(String tag) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
} 这是一个片段代码(第一个)
public class SendItemsFragment extends Fragment {
public ListView list;
private String URL = "**************";
public static String KEY_name = "name";
public static String KEY_id = "id";
public static String KEY_uid = "uid";
public static String KEY_type = "type";
public static String KEY_title = "title";
public static String KEY_lat = "lat";
public static String KEY_long = "long";
public static String KEY_image = "image";
public static String KEY_time = "time";
public TextView name, id1, type, title, lat, longitute, time, image;
private int screenSize;
private Adapter adapter;
public JSONParser jsonparser;
public JSONArray jsonarray;
public LoadDataAllChanelsToServer loader;
ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();
public EditText search;
public TransparentProgressDialog pd;
public TextView Txtlongitude, Txtlatitude, Txtname, Txtid, Txttype,
Txtimage, Txttime;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.send_items, container, false);
list = (ListView) rootView.findViewById(R.id.sendlistView1);
pd = new TransparentProgressDialog(getActivity(), R.drawable.loader);
screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
loader = new LoadDataAllChanelsToServer();
loader.execute();
list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
Txtlongitude = (TextView) view
.findViewById(R.id.viewpager_long);
Txtlatitude = (TextView) view.findViewById(R.id.viewpager_lat);
String Stringlongitude = Txtlongitude.getText().toString();
String Stringlatitude = Txtlatitude.getText().toString();
Intent in = new Intent(getActivity(), HistoryResult.class);
in.putExtra("longitude", Stringlongitude);
in.putExtra("latitude", Stringlatitude);
startActivity(in);
}
});
return rootView;
}
private class LoadDataAllChanelsToServer extends
AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pd.show();
}
@Override
protected String doInBackground(String... urls) {
jsonparser = new JSONParser();
Log.e("Facebook URl", URL);
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", jsonobject.getString(KEY_name));
map.put("id", jsonobject.getString(KEY_id));
map.put("uid", jsonobject.getString(KEY_uid));
map.put("type", jsonobject.getString(KEY_type));
map.put("title", jsonobject.getString(KEY_title));
map.put("lat", jsonobject.getString(KEY_lat));
map.put("time", jsonobject.getString(KEY_time));
map.put("image", jsonobject.getString(KEY_image));
map.put("long", jsonobject.getString(KEY_long));
itemList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return itemList.toString();
}
@Override
protected void onPostExecute(String result) {
try {
if (pd != null) {
pd.dismiss();
}
} catch (Exception e) {
}
try {
adapter = new Adapter(getActivity(), itemList, screenSize);
adapter.imageLoader.clearCache();
list.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
if (loader != null) {
loader.cancel(true);
loader = null;
}
} catch (Exception e) {
}
}
}
另一个片段的代码是相同的不同只是URL 如果有人知道解决方案,请帮助我谢谢