Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用McClickListener从ListView和数据库中删除项_Android_Sqlite_Listview_Android Sqlite - Fatal编程技术网

Android 使用McClickListener从ListView和数据库中删除项

Android 使用McClickListener从ListView和数据库中删除项,android,sqlite,listview,android-sqlite,Android,Sqlite,Listview,Android Sqlite,我创建了一个数据库,并设法将添加的项目显示到列表视图中。现在我需要一个方法从ListView和数据库中删除一个项 public class ZeigeFaecherListe extends AppCompatActivity { DatabaseHelper myDb; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(sav

我创建了一个数据库,并设法将添加的项目显示到列表视图中。现在我需要一个方法从ListView和数据库中删除一个项

public class ZeigeFaecherListe extends AppCompatActivity {

    DatabaseHelper myDb;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zeige_faecher);

        myDb = new DatabaseHelper(this);
        ListView listViewFaecher = (ListView) findViewById(R.id.listViewFaecher);

        final ArrayList<String> faecherListe = new ArrayList<>();
        Cursor res = myDb.zeigeFaecher();

        if (res.getCount() == 0){
            Toast.makeText(ZeigeFaecherListe.this, "Keine Fächer gefunden", Toast.LENGTH_LONG).show();
        } else {
            while (res.moveToNext()){
                faecherListe.add(res.getString(1));
                ListAdapter fachListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, faecherListe);
                listViewFaecher.setAdapter(fachListAdapter);
            }
        }

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

            }
        });
    }
}
公共类ZeigeFaecherListe扩展了AppCompative活动{
数据库助手myDb;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.zeige_faecher);
myDb=新数据库助手(此);
ListView listViewFaecher=(ListView)findViewById(R.id.listViewFaecher);
最终ArrayList FaecherList=新ArrayList();
Cursor res=myDb.zeigeFaecher();
如果(res.getCount()==0){
Toast.makeText(ZeigeFaecherListe.this,“Keine Fächer gefunden”,Toast.LENGTH_LONG.show();
}否则{
while(res.moveToNext()){
faecherListe.add(res.getString(1));
ListAdapter fachListAdapter=新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,faecherListe);
listViewFaecher.setAdapter(传真适配器);
}
}
listViewFaecher.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id){
}
});
}
}
listViewFaecher.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView AdapterView,视图视图,整型位置,长id){
//通过传递位置在此处调用delete方法
//位置将包含用户单击的项目的位置。
}
});

简而言之,您需要能够根据ListView可用的数据来区分要删除的行。如果从光标检索到的值作为第二列(即使用
res.getString(1)提取的字符串),
且该值将是唯一的,则可以检索该值并将其用于删除

但是,有一些问题,使用
ListAdapter
可能是不够的。还有其他适配器,例如提供更多功能的ArrayAdapter,更重要的是,还有一个
notifyDatasetChanged
方法(该方法将刷新关联的ListView)

为游标的每次迭代创建一个新适配器是一种浪费。因此,适配器应该在循环之外创建,并且只创建一次

我建议在项目点击时删除太容易发生意外点击,而在项目长点击时删除则不太容易发生意外删除

如果将变量移动为类变量,则不必将它们声明为final

因此,基于上述情况,您可以:-

阵列适配器方法
  • 在哪里
    • TB001是设置为表名的常量字符串
    • COL_TB001_DATA是第二列的列名
警告只有在第二列包含唯一数据时,上述解决方案才能正常工作,否则将删除多行

还有一个假设是删除有效,最好是:-

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (myDb.deleteRow((String)fachListAdapter.getItem(position))<0) {
                faecherListe.remove(position);
            }
            fachListAdapter.notifyDataSetChanged(); 
            return true; //<<<< Indicate that this longclick has been used
        }

注意由于
\u id
列始终是唯一的,如果显示的值不唯一,此方法将仅删除特定行而不是多行。

否。您需要一种方法从数据库中删除该项,然后相应地更新ListView。是的,但是我需要一种方法来选择具有listview的项目,我不知道如何实现。请参阅传递给
onItemClick
方法的最后一个参数您有
id
参数,该参数用于此。用户如何删除项目?只要点击它?或者他们必须选择它,然后按下删除按钮?
public class ZeigeFaecherListe extends AppCompatActivity {

    DatabaseHelper myDb;
    Cursor res;
    ListView listViewFaecher;
    ArrayAdapter<String> fachListAdapter;
    ArrayList<String> faecherListe;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zeige_faecher);

        listViewFaecher = (ListView) this.findViewById(R.id.listview);
        myDb = new DatabaseHelper(this);
        addSomeData(); //<<<<<<<<<< ADDED for testing

        faecherListe = new ArrayList<>();
        res = myDb.zeigeFaecher();
        while (res.moveToNext()) {
            faecherListe.add(res.getString(1));
        }

        //<<<< NOTE outside of the loop as this only needs to be done once
        fachListAdapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_list_item_1,
                faecherListe
        );
        listViewFaecher.setAdapter(fachListAdapter);

        //<<<<< NOTE used LONG CLICK listener (less likely to accidentally delete)
        listViewFaecher.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                myDb.deleteRow((String)fachListAdapter.getItem(position));
                faecherListe.remove(position);
                fachListAdapter.notifyDataSetChanged(); 
                return true; //<<<< Indicate that this longclick has been used
            }
        });
    }

    private void addSomeData() {
        for (int i=1; i <= 10; i++) {
            myDb.addRow("Row " + String.valueOf(i));
        }
    }
}
public int deleteRow(String col2) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(TB001,COL_TB001_DATA + "=?",new String[]{col2});
}
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (myDb.deleteRow((String)fachListAdapter.getItem(position))<0) {
                faecherListe.remove(position);
            }
            fachListAdapter.notifyDataSetChanged(); 
            return true; //<<<< Indicate that this longclick has been used
        }
public class ZeigeFaecherListe extends AppCompatActivity {

    DatabaseHelper myDb;
    Cursor res;
    ListView listViewFaecher;
    SimpleCursorAdapter fachSimpleCursorAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zeige_faecher);

        listViewFaecher = (ListView) this.findViewById(R.id.listview);
        myDb = new DatabaseHelper(this);
        addSomeData(); //<<<<<<<<<< ADDED for testing

        faecherListe = new ArrayList<>();
        res = myDb.zeigeFaecher();
        fachSimpleCursorAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, //<<<< The layout
                res, //<<<< The Cursor
                new String[]{"_data"}, //<<<< The column names from which to get the data
                new int[]{android.R.id.text1} //<<<< The ids of the views in which the data is placed
                );
        listViewFaecher.setAdapter(fachSimpleCursorAdapter);
        listViewFaecher.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                // id is the value of the respective _id column
                //<<<< Normally you would have the delete method in the Databasehelper >>>>
                myDb.getWritableDatabase().delete("mytable","_id=?",new String[]{String.valueOf(id)});
                fachSimpleCursorAdapter.swapCursor(myDb.zeigeFaecher()); // Tell the adapter about the new cursor
                return true;
            }
        });
    }
}