从列表视图中删除项目

时间:2014-01-23 12:52:33

标签: android listview

我的应用程序中存在问题,我有一个从数据库生成的列表视图,我希望何时

用户点击他将被删除的项目,我尝试了很多解决方案,但它不是

工作,我做了一个解决方案

                     arg0.removeView(arg0.getChildAt(position));              

                        adapter.notifyDataSetChanged(); 
请帮忙。

private void populateListViewFromDB() {
Cursor cursor = db.getAllRecords();
startManagingCursor(cursor);    

String[] databaseColumnNames = new String[] { DBAdapter.col_Region };
int[] toViewIDs = new int[] { android.R.id.text1, android.R.id.text2 };

SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_expandable_list_item_1, cursor,
        databaseColumnNames, toViewIDs, FLAG_REGISTER_CONTENT_OBSERVER);

final ListView list = (ListView) findViewById(android.R.id.list);

        list.setAdapter(myCursordapter);

        final String items[] = { "Completer le questionnaire" };

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(final AdapterView<?> arg0, View arg1,
            final int position, long arg3) {

        Toast.makeText(getApplicationContext(), " " + position,
                Toast.LENGTH_LONG).show();


        AlertDialog.Builder builder = new AlertDialog.Builder(
                MainActivity.this);
  final String s = ((TextView)arg1).getText().toString();


  builder.setTitle(s);

   builder.setItems(items, new   
           DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {

             db.insertest(s);

             arg0.removeView(arg0.getChildAt(position));


                        adapter.notifyDataSetChanged();                         
              Intent intent = new Intent(MainActivity.this, ActivityUn.class);
             intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
             startActivity(intent);             


   }
   });

   AlertDialog alert = builder.create();
   alert.show();

2 个答案:

答案 0 :(得分:1)

删除适配器中的项目,然后在listview中自动删除

adapter.remove(adapter.getItem((position));

答案 1 :(得分:0)

在您的SQLiteOpenHelper类中添加此

                 public void delete(int id){
                 List<your_object> ids = new ArrayList<your_object>();
                 SQLiteDatabase db = this.getWritableDatabase();
                 Cursor c = db.rawQuery("SELECT*FROM "+your_table,null);
                 while(c.moveToNext()){
                 ids.add(c.getString(0));
                 }
                 db.delete(your_table,your_id+" =?",new String[]
                 {String.valueOf(ids.get(id)}

仅将您的变量更改为变量。