我想删除cursoradapter和我的数据库生成的listview中的特定行。 在我的数据库中,_id列是主键,也是自动增量,所以如果我删除一个特定的行,它的id被删除,所以getposition()在这种情况下工作。我只想删除那个特定的行。
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);
}
}
答案 0 :(得分:0)
我认为以下内容应该有效,但我还没有测试过它......
@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.
}