如果我使用 AsyncTask 或 IntentService 从服务器检索数据,我的应用程序使用 ViewPager 包含四个 Fragment 具有不同的行为。最初,我使用 AsyncTask 实现了所有功能,并且只执行一次数据下载。现在我已经维护了相同的代码(用于构建视图和初始化变量),但我添加了一个 IntentService 调用以连续下载数据。问题是,如果我使用 IntentService,它不会调用 Fragments 的 onCreateView,并且我不能调用 Fragment Classess 的函数(onCreateView 检索也可以在 Fragment 内部修改的小部件)。这是我使用 AsyncTask 的主要活动的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = new DatabaseHelper(this);
// drop all
database.clearAll();
// download data
downloadData();
// build blocks
pager = (ViewPager) this.findViewById(R.id.pager);
FragmentManager fm = this.getSupportFragmentManager();
adapter = new FragmentAdapter(fm);
// set properties
pager.setAdapter(adapter);
pager.setPageMargin(0);
pager.setOffscreenPageLimit(3);
// retrieve Fragment object
tableFragment = (TableFragment) adapter.getTableFragment();
tempFragment = (TempFragment) adapter.getTempFragment();
humFragment = (HumFragment) adapter.getHumFragment();
mixFragment = (MixFragment) adapter.getMixFragment();
// listener for show action bar
pager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
int event = arg1.getAction();
if (event == MotionEvent.ACTION_DOWN) {
showActionBar();
return true;
}
return false;
}
});
stored_data = new ArrayList<String[]>();
}
public void downloadData() {
DownloadDataTask async_task = new DownloadDataTask(this, database);
async_task.execute(null, null, null);
}
public void saveDataToList(ArrayList<String[]> list) {
stored_data = list;
// refresh fragments
tableFragment.refresh(list);
tempFragment.refresh(list);
humFragment.refresh(list);
mixFragment.refresh(list);
}
saveDataToList()
它在从数据库中检索数据并将其解析为 ArrayList 后由 AsyncTask 调用。
ant 这是 Fragment 实现之一(其他的都差不多)
public class TableFragment extends Fragment {
MainActivity main;
ListView list_view;
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_layout, container, false);
list_view = (ListView) v.findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(v.getContext(),
android.R.layout.simple_list_item_1);
list_view.setAdapter(adapter);
return v;
}
/*
* refresh current view with data retrieved from database
*/
public void refresh(ArrayList<String[]> lista) {
adapter.clear();
for (int i = 0; i < lista.size(); i++) {
adapter.add("T: " + lista.get(i)[0] + " H: " + lista.get(i)[1]
+ " D: " + lista.get(i)[2]);
}
}
}
启动an的mainActivity的代码IntentService
是一样的,没有calldownloadData()
方法,但是在intentService的开头有start onCreate()
:
@Override
protected void onCreateView(Bundle savedInstanceState) {
Intent intent = new Intent(this, DownloadIntent.class);
startService(intent);
//same code
}
@Override
protected void onResume() {
while (true) {
Cursor data = database.getData();
if (data.getCount() > 0) {
ArrayList<String[]> list = new ArrayList<String[]>();
String[] to_add;
if (data.moveToFirst()) {
while (!data.isAfterLast()) {
to_add = new String[3];
for (int i = 0; i < 3; i++) {
to_add[i] = data.getString(i);
}
data.moveToNext();
list.add(to_add);
}
}
data.close();
saveDataToList(list);
}
try {
Log.i("AAAAAAA!", "main sleep");
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
怎么了?