我正在制作一个显示邮政编码列表的应用程序(在 ListView 中),当我单击它时,它会使用 OpenWeather API 向我显示详细的天气报告。邮政编码列表存储在 SQLite 数据库中,要从中检索数据,我使用的是 AsyncTaskLoader。数据显示正确。问题是在配置更改期间,正在调用 loadinBackground() 方法,这不应该发生。
public class DataLoader extends AsyncTaskLoader<List<RowItem>>
{
Context activityContext;
private String TAG="DataLoader";
public DataLoader(Context context)
{
super(context);
this.activityContext = context;
}
@Override
public List<RowItem> loadInBackground()
{
Log.v(TAG, "loadinBackground() IN");
List<RowItem> rowItems = new ArrayList<RowItem>();
DbReaderHelper mDbHelper = new DbReaderHelper(activityContext);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] columns = {DbReaderHelper.COLUMN_CITYNAME,DbReaderHelper.COLUMN_ZIPCODE};
Cursor cur = db.query(DbReaderHelper.TABLE_NAME, columns,null,null,null,null,null);
cur.moveToFirst();
while(!cur.isAfterLast())
{
String city = cur.getString(cur.getColumnIndex(DbReaderHelper.COLUMN_CITYNAME));
String zipcode = cur.getString(cur.getColumnIndex(DbReaderHelper.COLUMN_ZIPCODE));
rowItems.add(new RowItem(city,Integer.parseInt(zipcode)));
cur.moveToNext();
}
cur.close(); // release all the resources
db.close();
mDbHelper.close();
Log.v(TAG, "loadinBackground() OUT");
return rowItems;
}
}
这是Loader Callbacks的实现
public class ListFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<RowItem>>
{
private TextView emptyListTV;
private EditText zipcodeET;
private CustomAdapter customAdapter;
private List<RowItem> rowItems;
private ListView listView;
private final String TAG = "ListFragment";
private boolean fromRemoveItemFromListView = false; // TODO check this again
private final int LOADER_ID = 0x0;
public ListFragment()
{
// empty constructor
}
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG,"onCreate() IN");
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
Log.v(TAG,"onCreate() OUT");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list, container, false);
initializeLayoutViews(view);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
getLoaderManager().initLoader(LOADER_ID,null,this).forceLoad();
}
@Override
public Loader<List<RowItem>> onCreateLoader(int id, Bundle args)
{
Toast.makeText(getActivity(),"onCreateLoader()",Toast.LENGTH_SHORT).show();
return new DataLoader(getActivity());
}
@Override
public void onLoadFinished(Loader<List<RowItem>> loader, List<RowItem> rowItems)
{
Toast.makeText(getActivity(),"onLoadFinished",Toast.LENGTH_SHORT).show();
customAdapter.setData(rowItems);
customAdapter.notifyDataSetChanged();
setMessageIfListEmpty();
}
@Override
public void onLoaderReset(Loader<List<RowItem>> loader)
{
Toast.makeText(getActivity(),"onLoaderReset()",Toast.LENGTH_SHORT).show();
customAdapter.setData(new ArrayList<RowItem>());
customAdapter.notifyDataSetChanged();
}
}
这是我在方向改变时的日志
01-31 22:07:12.532 15019-15019/com.siddhant.myweatherapp D/AbsListView﹕ onDetachedFromWindow
01-31 22:07:12.583 15019-15019/com.siddhant.myweatherapp I/PersonaManager﹕ getPersonaService() name persona_policy
01-31 22:07:12.583 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onCreate() IN
01-31 22:07:12.653 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onStart() IN
01-31 22:07:12.733 15019-15019/com.siddhant.myweatherapp D/AbsListView﹕ Get MotionRecognitionManager
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() IN
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() OUT
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() IN
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() OUT
01-31 22:07:12.743 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onStart() OUT
01-31 22:07:12.753 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ OnResume() IN
01-31 22:07:12.753 15019-15522/com.siddhant.myweatherapp V/DataLoader﹕ loadinBackground() IN
01-31 22:07:12.753 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onResume() OUT
01-31 22:07:12.803 15019-15019/com.siddhant.myweatherapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-31 22:07:12.813 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onCreateOptionsMenu() IN
01-31 22:07:12.813 15019-15019/com.siddhant.myweatherapp V/MainActivity﹕ onCreateOptionsMenu() OUT
01-31 22:07:12.953 15019-15522/com.siddhant.myweatherapp V/DataLoader﹕ loadinBackground() OUT
01-31 22:07:12.963 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() IN
01-31 22:07:12.963 15019-15019/com.siddhant.myweatherapp V/ListFragment﹕ onLoadFinished() OUT
谢谢,