0

I have an issue in my application, i have a list view generated from database, i want when

the user click on an item that he will be deleted, i tried many solution but it's not

working, i did as a solution

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

                        adapter.notifyDataSetChanged(); 

help please .

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();
1
  • how to solve that ??? Commented Jan 23, 2014 at 14:05

2 Answers 2

1

remove the item in adapter then automatically it deletes in listview

adapter.remove(adapter.getItem((position));
Sign up to request clarification or add additional context in comments.

1 Comment

i have this error : The method remove(Object) is undefined for the type SimpleCursorAdapter !!
0

in your SQLiteOpenHelper class add this

                 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)}

change the yours to your variables only.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.