在UI线程上执行任务的onCreateLoader()

时间:2018-08-26 22:32:12

标签: java android multithreading asynchronous loader

正如标题所述,onCreateLoader正在UI上进行工作。我不知道为什么会这样。我试图创建一个自定义的AsyncLoader,该自定义AsyncLoader扩展了AsyncLoader,但也无法解决问题。

我看到在stackoverflow上也有类似的问题,但我无法弄清楚使用该错误。任何帮助将不胜感激,这是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        // Other stuff here....

        linlaHeaderProgress =  (LinearLayout) findViewById(R.id.linlaHeaderProgress);

        linlaHeaderProgress.setVisibility(View.VISIBLE);

        getLoaderManager().initLoader(LOADER_ID, null, this);

        // Other stuff here....

    } catch (Exception e){
        e.printStackTrace();
    }


@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    for(int i =0;i  <25;i++) {
        fillDataBase();
    }
    String [] projection = {
            FoodContract.FoodEntry._ID,
            FoodContract.FoodEntry.COLUMN_FOOD_ITEM_NAME,
            FoodContract.FoodEntry.COLUMN_FOOD_ITEM_DESC,
            FoodContract.FoodEntry.COLUMN_FOOD_ITEM_PRICE,
            FoodContract.FoodEntry.COLUMN_FOOD_ITEM_RATING,
            FoodContract.FoodEntry.COLUMN_FOOD_ITEM_CATEGORY

    };

    final String CONTENT_AUTHORITY = "com.example.android.food";
    final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
    final String PATH_FOOD = "food";

    Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_FOOD);

    Cursor foodCursor = getContentResolver().query(
            CONTENT_URI, null,null,null, null);

    try {

         //Iterating through the cursor code here...

    } catch (Exception e){
        e.printStackTrace();
    }

    // This loader will execute the ContentProvider's query method on a background thread
    return new CursorLoader(this,  
            CONTENT_URI,           
            projection,            
            null,                  
            null,                  
            null);                 


}


@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    try {
        adapter = new MyAdapter(this, foodItemArrayList);
        rv.setAdapter(adapter);

        linlaHeaderProgress.setVisibility(View.GONE);

    } catch (Exception e){
        e.printStackTrace();
    }


}

0 个答案:

没有答案