如何使用上下文菜单从基于游标的列表视图和数据库中删除特定行
How to delete a particular row from my cursor based listview and data base using context menu
我想从 cursoradapter 生成的列表视图以及我的数据库中删除特定行。
在我的数据库中,_id 列是主键并且也是自动递增的,所以如果我删除特定行,它的 id 将被删除,所以 getposition() 在此工作 case.I 只希望删除该特定行。
public class DataViewer extends Fragment{
static ListView listdata;
DataAdapter data;
Cursor cursor;
DataCursorAdapter datacursor;
public DataViewer() {
// TODO Auto-generated constructor stub
}
public void update(){
cursor=data.fetchdata();
datacursor.swapCursor(cursor);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootview =inflater.inflate(R.layout.data_fragment, container, false);
listdata=(ListView) rootview.findViewById(R.id.listdata);
data=new DataAdapter(getActivity());
cursor=data.fetchdata();
datacursor=new DataCursorAdapter(getActivity(), cursor);
listdata.setAdapter(datacursor);
registerForContextMenu(listdata);
return rootview;
}
class DataCursorAdapter extends CursorAdapter{
public DataCursorAdapter(Context context, Cursor c) {
super(context, c,0);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
TextView time=(TextView) arg0.findViewById(R.id.texttime);
TextView descriptiontext=(TextView) arg0.findViewById(R.id.textdescription);
String timer=arg2.getString(arg2.getColumnIndexOrThrow("date"));
time.setText(timer);
String description=
arg2.getString(arg2.getColumnIndexOrThrow("description"));
descriptiontext.setText(description);
String mood=arg2.getString(arg2.getColumnIndexOrThrow("mood"));
int i=arg2.getInt(0);
Log.d("The value of id is", ""+i);
ImageView image=(ImageView) arg0.findViewById(R.id.dataimage);
switch (mood) {
case "Excited":
image.setImageResource(R.drawable.excited);
break;
case "Happy":
image.setImageResource(R.drawable.happy);
break;
case "Lonely":
image.setImageResource(R.drawable.lonely);
break;
default:
image.setImageResource(R.drawable.others);
break;
}
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return
LayoutInflater.from(arg0).inflate(R.layout.data_row, arg2,false);
}}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, R.id.menudelete, 0, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menudelete:
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
我认为像下面这样的东西应该可以工作,但我还没有测试过...
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menudelete:
deleteItem(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
private void deleteItem(int position) {
Cursor c = dataCursor.getCursor();
c.moveToPosition(position);
int databaseId = c.getInt(c.getColumnIndex("_id"));
// Use databaseId to delete the row from the database...
// ...then re-query the database to update the Cursor...
// ...and call notifyDatasetChanged() on the Adapter.
}
我想从 cursoradapter 生成的列表视图以及我的数据库中删除特定行。 在我的数据库中,_id 列是主键并且也是自动递增的,所以如果我删除特定行,它的 id 将被删除,所以 getposition() 在此工作 case.I 只希望删除该特定行。
public class DataViewer extends Fragment{
static ListView listdata;
DataAdapter data;
Cursor cursor;
DataCursorAdapter datacursor;
public DataViewer() {
// TODO Auto-generated constructor stub
}
public void update(){
cursor=data.fetchdata();
datacursor.swapCursor(cursor);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootview =inflater.inflate(R.layout.data_fragment, container, false);
listdata=(ListView) rootview.findViewById(R.id.listdata);
data=new DataAdapter(getActivity());
cursor=data.fetchdata();
datacursor=new DataCursorAdapter(getActivity(), cursor);
listdata.setAdapter(datacursor);
registerForContextMenu(listdata);
return rootview;
}
class DataCursorAdapter extends CursorAdapter{
public DataCursorAdapter(Context context, Cursor c) {
super(context, c,0);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
TextView time=(TextView) arg0.findViewById(R.id.texttime);
TextView descriptiontext=(TextView) arg0.findViewById(R.id.textdescription);
String timer=arg2.getString(arg2.getColumnIndexOrThrow("date"));
time.setText(timer);
String description=
arg2.getString(arg2.getColumnIndexOrThrow("description"));
descriptiontext.setText(description);
String mood=arg2.getString(arg2.getColumnIndexOrThrow("mood"));
int i=arg2.getInt(0);
Log.d("The value of id is", ""+i);
ImageView image=(ImageView) arg0.findViewById(R.id.dataimage);
switch (mood) {
case "Excited":
image.setImageResource(R.drawable.excited);
break;
case "Happy":
image.setImageResource(R.drawable.happy);
break;
case "Lonely":
image.setImageResource(R.drawable.lonely);
break;
default:
image.setImageResource(R.drawable.others);
break;
}
}
@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return
LayoutInflater.from(arg0).inflate(R.layout.data_row, arg2,false);
}}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, R.id.menudelete, 0, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menudelete:
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
我认为像下面这样的东西应该可以工作,但我还没有测试过...
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.menudelete:
deleteItem(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
}
private void deleteItem(int position) {
Cursor c = dataCursor.getCursor();
c.moveToPosition(position);
int databaseId = c.getInt(c.getColumnIndex("_id"));
// Use databaseId to delete the row from the database...
// ...then re-query the database to update the Cursor...
// ...and call notifyDatasetChanged() on the Adapter.
}