【问题标题】:how to delete an item from listview in android如何从android的listview中删除一个项目
【发布时间】:2011-05-27 11:27:14
【问题描述】:

我的应用程序包含一个列表视图。列表视图中的每个项目都有一个删除按钮。但删除按钮无法删除列表视图中的项目。请告诉我如何删除列表视图和 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 中我还定义了一个删除按钮,所以请检查并告诉我解决方案。

【问题讨论】:

    标签: android xml listview button


    【解决方案1】:

    在您的confirmdelete 方法中,在db.delete 之后使用AsyncTask 查询数据库并在doInBackground 方法中获取新的Cursor。然后在onPostExecute 方法中调用changeCursor 上的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);
        }
    }
    

    祝你好运。

    【讨论】:

    • 在我的情况下,我再次创建适配器,然后将其设置为 ListView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多