在我关闭应用程序并再次打开之前,列表不会更新

时间:2019-07-29 06:27:21

标签: java android-contentprovider

我正在使用ContentProvider和加载程序。当我添加宠物时,列表会自动更新,但是当我按一下从溢出菜单中删除所有宠物时,列表将不会更新,直到我关闭应用程序并再次打开它。

公共类CatalogActivity扩展了AppCompatActivity,实现了LoaderManager.LoaderCallbacks {

/** Tag for the log messages */
public static final String LOG_TAG = CatalogActivity.class.getSimpleName();


PetCursorAdapter adapter;

private PetDbHelper mDbHelper;

private static final int URL_LOADER=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_catalog);

    // Setup FAB to open EditorActivity
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
            startActivity(intent);
        }
    });
    mDbHelper=new PetDbHelper(this);

    // Find the ListView which will be populated with the pet data
    ListView listView = (ListView) findViewById(R.id.list_item);

    // Find and set empty view on the ListView, so that it only shows when the list has 0 items.
    View emptyView = findViewById(R.id.empty_view);
    listView.setEmptyView(emptyView);
    listView.setLongClickable(true);

    adapter=new PetCursorAdapter(this,null);

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent=new Intent(CatalogActivity.this,EditorActivity.class);
            Uri currentUri= ContentUris.withAppendedId(PetEntry.CONTENT_URI,id);
            intent.setData(currentUri);
            startActivity(intent);
        }
    });



    getSupportLoaderManager().initLoader(URL_LOADER,null,this);
}

private void insertPets(){


    ContentValues values=new ContentValues();

    values.put(PetEntry.COLUMN_PET_NAME,"Toto");
    values.put(PetEntry.COLUMN_PET_BREED,"terrier");
    values.put(PetEntry.COLUMN_PET_GENDER,PetEntry.GENDER_MALE);
    values.put(PetEntry.COLUMN_PET_WEIGHT,7);

    Uri newRowId =getContentResolver().insert(PetEntry.CONTENT_URI,values);
    Log.v("CatalogActivity","new row ID"+newRowId);
}

private void deletePets(){

    int deletedPets=getContentResolver().delete(PetEntry.CONTENT_URI,null,null);

    adapter.
    Log.v("catalogActivity",deletedPets+" pets have been deleted..");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu options from the res/menu/menu_catalog.xml file.
    // This adds menu items to the app bar.
    getMenuInflater().inflate(R.menu.menu_catalog, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // User clicked on a menu option in the app bar overflow menu
    switch (item.getItemId()) {
        // Respond to a click on the "Insert dummy data" menu option
        case R.id.action_insert_dummy_data:
            insertPets();
            return true;
        // Respond to a click on the "Delete all entries" menu option
        case R.id.action_delete_all_entries:
            //delete all the pets from the list
            deletePets();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@NonNull
@Override
public Loader<Cursor> onCreateLoader(int loaderID, @Nullable Bundle bundle) {

    String[] projection ={PetEntry._ID,PetEntry.COLUMN_PET_NAME,PetEntry.COLUMN_PET_BREED};


            return new CursorLoader(this,
                    PetEntry.CONTENT_URI,
                    projection,
                    null,
                    null,
                    null);

}

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
    //update with this new cursor contenting updated new data
    adapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(@NonNull Loader loader) {
    //callback called when thw data needs to delete
    adapter.swapCursor(null);
}

}

0 个答案:

没有答案