Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:17:37+00:00 2026-05-28T17:17:37+00:00

Hi I want to delete selected item from the data base. My code is

  • 0

Hi I want to delete selected item from the data base. My code is

  **Deletion.java**

  package com.my.project;
  import java.util.ArrayList;
  import android.app.Activity;
  import android.content.Intent;
 import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
  impor t android.os.Bundle;
  import android.preference.PreferenceManager;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
  import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.Spinner;
 import android.widget.Toast;
 import android.widget.AdapterView.OnItemSelectedListener;

 public class Deletion extends Activity
{
SharedPreferences my_Shared_Data;
SQLiteDatabase myDelete=null;
String DataBase_Name="deletedata";
String Table_Name="deletedetails";
Cursor c1,c2;
ListView lv1;
ArrayList<String> nameArray;
Spinner deleterecord;
ArrayAdapter<String> deleteAdapter;
private static String[] aname;

 @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.delete);

         lv1=(ListView)findViewById(R.id.list);
         try{
             myDelete=this.openOrCreateDatabase(DataBase_Name, MODE_PRIVATE, null);
                System.out.println("databse has been creates.....");
                myDelete.execSQL("create table if not exists " +Table_Name+ "(name varchar(50))");
                System.out.println("table has been created.....");
                c1=myDelete.rawQuery("select * from "+Table_Name , null);
                c1.moveToFirst();
                int count1=c1.getCount();
                System.out.println("columns --->"+count1);
                if(count1==1)
                {
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('asha')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('karthick')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('arunthathi')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('vasanth')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('nithyakavini')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('abhimanyu')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('charles')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('samyuktha')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('vijayalayan')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('samantha')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('shasvathi')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('yazhini')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('kannan')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('mirunalini')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('adhavan')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('subhathra')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('muhammad')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('mayadevi')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('bhazeer')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('aadhityaa')");
                    myDelete.execSQL("insert into "+Table_Name+ "(name)" +"values('vijaynarayanan')");

                    System.out.println("data has been inserted.....");
                }

                c2=myDelete.rawQuery("select * from "+Table_Name , null);
                c2.moveToFirst();
                int count2=c2.getCount();
                aname=new String[count2];
                System.out.println("columns --->"+count2);
                //final int column1=c2.getColumnIndex("name");
                nameArray= new ArrayList<String>();
                for(int i=0;i<count2;i++)
                {
                    aname[i]=c2.getString(c2.getColumnIndex("name"));
                    c2.moveToNext();
                    nameArray.add(aname[i]);
                    //System.out.println(aname[i]);

                }
                 for( int j = 0 ; j < nameArray.size();j++)
                    {

                        System.out.println(nameArray.get(j));
                    }


                 lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameArray));
                 lv1.setTextFilterEnabled(true);
                 lv1.setOnItemClickListener(new OnItemClickListener()
                 {

                 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                 {

                 }
                 });
                 }
         catch (Exception e) 
            {
                e.printStackTrace();
            }}
                 @Override
                    public boolean onCreateOptionsMenu(Menu menu)
                    {
                        MenuInflater inflater = getMenuInflater();
                        inflater.inflate(R.menu.menu, menu);
                        return true;
                    }

                    @Override
                    public boolean onOptionsItemSelected(MenuItem item) 
                    {
                        switch (item.getItemId()) 
                        {
                            case R.id.delete:     Toast.makeText(this, "You pressed the delete menu!", Toast.LENGTH_SHORT).show();
                            Intent myIntent = new Intent(Deletion.this,DeleteData.class);
                            setResult(Activity.RESULT_OK);
                            myIntent.putExtra("my_name", nameArray);
                            finish();
                            startActivity(myIntent);
                                                break;
                            case R.id.cancel: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_SHORT).show();
                                                break;
                        }
                        return true;

    }    

  }

The above java class shows the data base items in the list view. Now going to select the items to delete.

   **DeleteData.java**

   package com.my.project;
   import java.util.ArrayList;
   public class DeleteData extends Activity 
   {

private static String[] select_data;
private static String[] data_base_data;
ArrayList<String> data_base_Array;
 ArrayList<String> my_name1;
 ListView lv2;
 Button delete;
 @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.deletedata);

         delete = (Button)findViewById(R.id.getchoice);

         Intent sender=getIntent();
         my_name1= new ArrayList<String>();
         my_name1=this.getIntent().getStringArrayListExtra("my_name");
         for( int k = 0 ; k < my_name1.size();k++)
            {

                System.out.println(my_name1.get(k));
            }
         lv2=(ListView)findViewById(R.id.deletelist);
         final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,my_name1);

        // lv2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,my_name1));
         lv2.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
         lv2.setAdapter(adapter);

         lv2.setTextFilterEnabled(true);

         lv2.setOnItemClickListener(new OnItemClickListener()
         {

         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
         {

         }
         });


          delete.setOnClickListener(new View.OnClickListener()
          {               
             @Override
             public void onClick(View v) 
             {  
                  String selected = "";
                   int cntChoice = lv2.getCount();
                   System.out.println(cntChoice);
                   my_name1= new ArrayList<String>();
                   SparseBooleanArray sparseBooleanArray = lv2.getCheckedItemPositions();
                   for(int i = 0; i < cntChoice; i++)
                   {
                        if(sparseBooleanArray.get(i)) 
                        {      
                          selected += lv2.getItemAtPosition(i).toString() + "\n";
                        //  adapter.remove(selected);
                        /*  select_data[i]=lv2.getItemAtPosition(i).toString();
                          my_name1.add(select_data[i]);*/
                         }
                  }
                  /* for( int j = 0 ; j < my_name1.size();j++)
                    {

                        System.out.println("checked data-->"+my_name1.get(j));
                    }*/

                 System.out.println("seleted items for deletion"+selected);
                 Toast.makeText(DeleteData.this,selected,Toast.LENGTH_LONG).show();
                 for(int i=0;i<cntChoice;i++)
                 {
                     System.out.println(i);                      
                 }

              }});
    }

        }
     }

It displays the checked items in toast. Now I want to delete the selected items. I can’t store this selected items into Array List. how can i do this.Any body help me? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:17:38+00:00Added an answer on May 28, 2026 at 5:17 pm

    Your approach to how you are using these “selected” items is incorrect. You should be having checkboxes that are found in your listview, upon the click of done or back then these items should be found in the ListView.

    Meaning all those that were selected (ideally you’d keep track of this via a list at runtime) and then done is clicked, then you submit and remove all these items from the Listview’s adapter and then notify the listview that data has changed.

    Your current approach of Iterating through them is not a good approach, You should be using the fact that each item maps to a particular ID that points to a particular row in the database.

    Use this list and remove the rows as necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete a document in lucene 2.4 with java. My code is
I want to delete a range of data from a text file using PHP.
I want to delete an item from a ListView, to which have attached a
I want to delete foo() if foo() isn't called from anywhere.
I want to delete from many files each instance of a paragraph . I
I have a radajaxpanel that is populated with the selected item from a combobox
I want to delete all the selected rows when the delete button is pressed.
My application has an <asp:ListView> . When one item is selected, I just want
I want to delete multiple users from db table with checkboxes at once. But
I want to delete a row from gridview and database - I write some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.