2

我正在制作一个显示邮政编码列表的应用程序(在 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

谢谢,

4

3 回答 3

0

您正在调用forceLoad()initLoader(). 这将导致加载器重新加载数据,即使它之前已经检索过该数据。您不需要调用此方法,只需调用initLoader().

于 2015-02-01T06:21:28.997 回答
0

forceLoad()不会自动调用,但需要启动数据加载。

但是forceLoad()应该在内部完成onStartLoading()而不是调用initLoader()

AsyncTaskLoaders 从 API 级别 28(Android Pie)开始被弃用。

于 2019-02-07T04:09:31.500 回答
0

与调用时自动运行后台任务的 AsyncTask 不同,调用时AsyncTaskLoadermyAsyncTask.execute()不会自动运行后台线程getSupportLoaderManager().initLoader(MY_LOADER_ID, null, this);要强制它运行后台线程,您需要通过两种方式在 Loader 对象上显式调用 forceLoad() :

第一:使用 LoaderManager 初始化/激活加载器时通过方法链getSupportLoaderManager().initLoader(MY_LOADER_ID, null, this).forceLoad();

第二:在由onStartLoading()自动触发的自定义加载器类回调中getSupportLoaderManager().initLoader(...)

@Override
protected void onStartLoading() {
    super.onStartLoading();
    forceLoad(); // call loadInBackground()
}

显然对于配置更改,这两种方法都会调用loadInBackground()方法,因为 onCreate() 方法将在屏幕旋转时触发,然后getSupportLoaderManager().initLoader(...)forceLoad()被相应地调用,但隐含地,您可以使用第二种方法来停止loadInBackground()不必要的调用,deliverResult()方法是使用传递结果的方法先前加载到已注册侦听器的onLoadFinished()方法,这反过来又允许我们跳过loadInBackground()调用。

因此,您需要在代码中更改:

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    setRetainInstance(true);
    getLoaderManager().initLoader(LOADER_ID, null, this);
}

这是装载机

public class DataLoader extends AsyncTaskLoader<List<RowItem>> {

    Context activityContext;
    private String TAG="DataLoader";
    private List<RowItem> rowItems;

    public DataLoader(Context context){
        super(context);
        this.activityContext = context;
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        if (rowItems != null) {
            deliverResult(rowItems); // skip loadInBackground() call
        } else {
            forceLoad(); // call loadInBackground()
        }
    }


    @Override
    public List<RowItem> loadInBackground(){
        Log.v(TAG, "loadinBackground() IN");
        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;
    }
}

值得一提的是,从 Android P (API 28) 开始,Loaders已被弃用。

于 2019-02-09T16:42:08.153 回答