我正在做一个基本的Android应用程序来了解sqlite。该应用程序只有一个listfragment列出一些产品和价格,如:
Pencil 1
Pen 1.20
...
可以单击列表中的项目将其删除。我使用cursorloader所以db操作在后台完成。但是我遇到了一个问题:
你知道问题出在哪里吗?为什么在点击项目后删除点击的项目以及为什么删除所有项目都不会更新片段列表?
//列出产品和相应的价格
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();
}
// cursorloader methods
@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);
}
//提供者类删除方法:
@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);
}
}
答案 0 :(得分:0)
在项目删除调用后点击notifyDatasetChanged()