从列表片段中删除项目无法正常工作 [英] delete item from listfragment is not working properly

查看:37
本文介绍了从列表片段中删除项目无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个基本的 android 应用程序来了解 sqlite.该应用程序只有一个列表片段,其中列出了一些产品和价格,例如:

Im doing a basic android app to learn about sqlite. The app just have a listfragment that list some products and the price like:

Pencil    1
Pen       1.20
...

并且可以单击列表中的项目将其删除.我使用游标加载器,因此数据库操作在后台完成.但我遇到了一个问题:

And its possible to click in a item of the list to delete it. Im using cursorloader so the db operations are done in background. But Im getting a issue:

  • 当用户单击某个项目时,该项目不会从列表中删除,但如果我关闭并再次打开该应用,列表中的所有项目都已删除.

你知道问题出在哪里吗?为什么在项目中点击后通过删除点击的项目没有更新片段列表,为什么所有项目都被删除?

Do you know where is the issue? Why the fragmentlist is not updated by removing the clicked item after click in the item and why all items are being removed?

//列出产品和各自的价格

public class ProductsFragment extends ListFragment implements OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {
  private static final String[] PROJECTION=new String[] {
      Provider.Products._ID, Provider.Products.TITLE,
      Provider.Products.PRICE };

      private CursorAdapter cursorAdapter;


  public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);

    Cursor cursor = getActivity().getContentResolver().query(Provider.Products.CONTENT_URI, DBHelper.ALL_COLUMNS,
            null,null,null,null);
    String[] from = {DBHelper.TITLE, DBHelper.PRICE};
    int[] to = {R.id.title, R.id.price};

    cursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.item, null, from, to, 0);

    setListAdapter(cursorAdapter);

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

  }

//删除产品

@Override
  public void onListItemClick(ListView l, View v, int pos, long id) {
    super.onListItemClick(l, v, pos, id);

    getActivity().getContentResolver().delete(Provider.Products.CONTENT_URI,String.valueOf(id), null);

    Toast.makeText(getActivity(), "Item " + id + " clicked", Toast.LENGTH_SHORT).show();
  }

//游标加载器方法

 @Override
  public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    return new CursorLoader(getActivity().getApplicationContext(), Provider.Products.CONTENT_URI, null, null, null, null);
  }


 @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    cursorAdapter.swapCursor(cursor);
  }


public void onLoaderReset(Loader<Cursor> loader) {
    cursorAdapter.swapCursor(null);
  }

//Provider 类删除方法:

@Override
  public int delete(Uri url, String where, String[] whereArgs) {
    return database.delete(DBHelper.TABLE_PRODUCTS, where, whereArgs);
  }

同样的问题:

 @Override
  public int delete(Uri url, String where, String[] whereArgs) {
    int count=db.getWritableDatabase().delete(TABLE, where, whereArgs);

    getContext().getContentResolver().notifyChange(url, null);

    return(count);
  }

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <TextView
      android:id="@+id/title"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      />
  <TextView
      android:id="@+id/price"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true"
      />
</RelativeLayout>

完整的产品片段:

public class ConstantsFragment extends ListFragment implements OnClickListener, LoaderManager.LoaderCallbacks<Cursor> {

  private CursorAdapter cursorAdapter;

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

    setHasOptionsMenu(true);
  }

  public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);

    Cursor cursor = getActivity().getContentResolver().query(Provider.Constants.CONTENT_URI, DatabaseHelper.ALL_COLUMNS,
            null,null,null,null);
    String[] from = {DatabaseHelper.TITLE, DatabaseHelper.VALUE};
    int[] to = {R.id.title, R.id.value};

    cursorAdapter = new SimpleCursorAdapter(getActivity(), R.layout.row, null, from, to, 0);

    setListAdapter(cursorAdapter);

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

  }

  @Override
  public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.actions, menu);

    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.add) {
      add();
      return(true);
    }

    return(super.onOptionsItemSelected(item));
  }


  @Override
  public void onClick(DialogInterface dialog, int which) {
    ContentValues values=new ContentValues();
    AlertDialog dlg=(AlertDialog)dialog;
    EditText title=(EditText)dlg.findViewById(R.id.title);
    EditText value=(EditText)dlg.findViewById(R.id.value);

    values.put(DatabaseHelper.TITLE, title.getText().toString());
    values.put(DatabaseHelper.VALUE, value.getText().toString());

 getActivity().getContentResolver().insert(Provider.Constants.CONTENT_URI, values);

    getLoaderManager().restartLoader(0, null, this);
  }

  @Override
  public void onListItemClick(ListView l, View v, int pos, long id) {
    super.onListItemClick(l, v, pos, id);

    getLoaderManager().restartLoader(0, null, this);
    getActivity().getContentResolver().delete(Provider.Constants.CONTENT_URI,String.valueOf(id), null);

    Toast.makeText(getActivity(), "Item id " + id + "clicked", Toast.LENGTH_SHORT).show();
  }

  private void add() {
    LayoutInflater inflater=getActivity().getLayoutInflater();
    View addView=inflater.inflate(R.layout.add_edit, null);
    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.add_title).setView(addView)
        .setPositiveButton(R.string.ok, this)
        .setNegativeButton(R.string.cancel, null).show();
  }

  @Override
  public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    return new CursorLoader(getActivity().getApplicationContext(), Provider.Constants.CONTENT_URI, null, null, null, null);
  }

  public void insertNote(String title, Double value){
    ContentValues values = new ContentValues();
    values.put(DatabaseHelper.TITLE, title);
    values.put(DatabaseHelper.VALUE, value);

    Uri noteUri = getActivity().getContentResolver().insert(Provider.Constants.CONTENT_URI, values);
    Log.d("MainActivity", "Inserted" + noteUri.getLastPathSegment());
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    cursorAdapter.swapCursor(cursor);
  }


  @Override
  public void onLoaderReset(Loader<Cursor> loader) {
    cursorAdapter.swapCursor(null);
  }


}

推荐答案

in on click after item delete call to notifyDatasetChanged()

in on click after item deleted call to notifyDatasetChanged()

这篇关于从列表片段中删除项目无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆