如何从android中的列表中删除所选项目

时间:2017-04-23 12:25:38

标签: java android

我有数据库,我使用simple_list_item_checked布局在列表中显示内容。现在我想删除用户从该列表中选择的项目。我怎么能这样做? 这是xml活动:

<ImageButton
    android:id="@+id/ib_delete"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/delete"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/tv_page_mySpace2"
    android:layout_marginTop="20dp"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/theuserideaslist"
    android:background="@drawable/brain1"
    android:layout_below="@id/ib_delete"
    android:layout_marginTop="20dp"/>

这是他们告诉我把它作为我的开放助手的删除方法:

public void removeIedas (long id) {
    String string = String.valueOf(id);

    database.execSQL("DELETE FROM name WHERE _id = '" + string + "'");
}

这是我的活动的java类:

final MyOpenHelper myOpenHelper = new MyOpenHelper( getApplicationContext());
    final ListView theuserideaslist = (ListView) findViewById(R.id.theuserideaslist);

    ArrayList<String> n ;
    n = myOpenHelper.ShowTheUserIdeas();
    final ArrayAdapter<String> myadapter = new ArrayAdapter<String> (getApplicationContext() , android.R.layout.simple_list_item_checked , n ) ;
    theuserideaslist.setAdapter(myadapter);
    theuserideaslist.setChoiceMode(2);

    final ImageButton ib_delete = (ImageButton) findViewById(R.id.ib_delete);

    theuserideaslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {

            ib_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myOpenHelper.removeIedas(id); //create removemethod in database class

                }
            });

        }
    });

  /*  ib_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            theuserideaslist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                  //  ArrayList database = null;
                    myOpenHelper.removeIedas(id); //create removemethod in database class

                }
            });
        }
    });*/

我想要的是:当我点击ib_delete时,删除选中的项目。 对不起,我还是新人。 谢谢。

1 个答案:

答案 0 :(得分:1)

使用此:

listview.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         database.remove(id); //Create a remove method in your database class
     }
});

并在删除方法中:

public void remove(long id){
    String string = String.valueOf(id);
    database.execSQL("DELETE FROM favorite WHERE _id = '" + string + "'");
}

收集自 this stackoverflow question

<强> 更新

不要在按钮上设置onclick lister ,实际上您可能希望在点击时从列表视图中删除该项目。

假设您的listView是您的活动的ListView的ID,

下面:

myDBHelper是类的对象,它扩展了SQLiteOpenHelper,如

public class MyDBHelper extends SQLiteOpenHelper{

//codes..

    public void DeleteFromDB(String d) {
    SQLiteDatabase db = getWritableDatabase();

    String dm = "\"" + d + "\"";

    try {
        db.execSQL("delete from " + db_table + " where taskName=" + dm);
    } catch (SQLException e) {
        Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
    }

   }
}

然后在主要活动中使用此功能:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        myDBHelper.DeleteFromDB(arrayList.get(position));

        }
    });