我的应用程序包含一个列表视图.列表视图中的每个项目都有一个删除按钮.但是删除按钮无法删除列表视图中的项目.请告诉我如何删除列表视图以及database.my中的项目.
我的来源:
public voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.sharefolioedit); Log.e("string",s.toString()); add = (Imagebutton) findVIEwByID(R.ID.add); add.setonClickListener(this); done = (button) findVIEwByID(R.ID.done); done.setonClickListener(this); getList(); } public voID onCreate1(Bundle savedInstanceState){ super.onCreate(savedInstanceState); delete= (Imagebutton) findVIEwByID(R.ID.delete); delete.setonClickListener(this); } protected voID onListItemClick(ListVIEw l, Imagebutton delete, int position,long ID) { super.onListItemClick( l, delete, position, ID); ID=ID; final Integer index =l.getSelectedItemposition(); Log.e("delete method","delete"); Log.e("ID value",index.toString()); new AlertDialog.Builder(ShareFolioEditactivity.this) .setTitle("Delete") .setMessage("Are you sure ??? ") .setNeutralbutton("no",null) .setPositivebutton("yes", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int whichbutton) { confirmdelete(index); } }) .show(); } public voID myClickHandler(VIEw v) { {// int position = 0;// onListItemClick(lv, delete, position, ID);// ID=ID; Log.e("delete method","delete"); final Integer a = new Integer(v.getID()); Log.e("ID value",a.toString()); new AlertDialog.Builder(ShareFolioEditactivity.this) .setTitle("Delete") .setMessage("Are you sure ??? ") .setNeutralbutton("no",null) .setPositivebutton("yes", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int whichbutton) { confirmdelete(a); } }) .show(); } } public voID onClick(VIEw v) { if(v.equals(add)) { Intent in=new Intent(ShareFolioEditactivity.this,Addcategory.class); startActivity(in); } if(v.equals(done)) { Intent i=new Intent(ShareFolioEditactivity.this,ShareFolioActivity.class); startActivity(i); } } public voID confirmdelete(int index) { db.delete("shareList", "_ID="+index, null); Toast.makeText(this, "row deleted"+index, Toast.LENGTH_SHORT).show(); Bundle b=null; onCreate(b); } public voID getList() { int x = 0; String where=null; csh=new createsqliteHelper(this); try{ //db.open(); db=csh.getReadableDatabase(); }catch(Exception e){ System.out.println(e); } if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { where=getIntent().getStringExtra(SearchManager.query); x=1; } if(x==1) { cur=db.rawquery("SELECT * FROM shareList where category liKE '%"+where+"%'",null); }else{ cur=db.rawquery("SELECT * FROM shareList ORDER BY category",null);} Log.e("cursor",cur.toString()); SimpleCursorAdapter List=new SimpleCursorAdapter(this,R.layout.edititems,cur,s,i ); getListVIEw(); setlistadapter(List); }}
sharefolioedit xml布局是
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/text" androID:text="CATEGORIES" androID:layout_centerHorizontal="true" /> <button androID:ID="@+ID/done" androID:text="Done" androID:layout_alignParenttop="true" androID:layout_alignParentRight="true" androID:layout_wIDth="90px" androID:layout_height="20px" androID:layout_above="@ID/androID:List" /> <Imagebutton androID:ID="@+ID/add" androID:text="Add" androID:layout_alignParenttop="true" androID:layout_alignParentleft="true" androID:layout_wIDth="80px" androID:layout_height="20px" androID:layout_above="@ID/androID:List" androID:src="@drawable/addicon"/> <ListVIEw androID:layout_wIDth="500px" androID:layout_height="700px" androID:ID="@ID/androID:List" androID:layout_alignParentBottom="true" /> </relativeLayout>
edititems.xml是
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <Imagebutton androID:ID="@+ID/delete" androID:layout_wIDth="70px" androID:paddingleft="2px" androID:paddingRight="2px" androID:paddingtop="2px" androID:text="Delete" androID:layout_height="wrap_content" androID:onClick="myClickHandler" androID:src="@drawable/removeicon" /> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="horizontal" androID:padding="10sp"> <TextVIEw androID:ID="@+ID/category" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="" androID:layout_toRightOf="@+ID/catagory" /> </relativeLayout></linearLayout>
在edititems.xml中,我还定义了一个删除按钮,因此请检查并告诉我解决方案.
解决方法:
在您的Confirmdelete方法中,在db.delete之后,请使用AsyncTask
查询数据库并在doInBackground方法中获取新的Cursor.然后在onPostExecute方法中,在您的SimpleCursorAdapter上调用changeCursor
,您已经调用了List,它应该是一个类级别的字段而不是本地字段,以便您的所有方法都可以访问它.这将告诉ListVIEw适配器中的数据已更改,需要刷新自身.无需在底部的Confirmdelete方法中调用onCreate.
这是一些代码:
private class UpdateCursor extends AsyncTask<VoID, VoID, Cursor> { protected Cursor doInBackground(URL... urls) { Cursor result = db.query(...); return result; } protected voID onPostExecute(Cursor result) { List.changeCursor(result); }}
祝好运.
总结以上是内存溢出为你收集整理的如何从Android中的列表视图中删除项目全部内容,希望文章能够帮你解决如何从Android中的列表视图中删除项目所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)