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

  • SEARCH
  • Home
  • 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 8967139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:10:53+00:00 2026-06-15T17:10:53+00:00

In my application, listview is loaded from SQLite database and it is using SimpleAdapter

  • 0

In my application, listview is loaded from SQLite database and it is using SimpleAdapter to set ListView. When user perform longclick on listitem, that item will be deleted from database and it should update listviw. But when i remove item from database, listview shows both old and new data.
Please suggest some solution.
Thanx.

Following is my code:

public class FavouriteListActivity extends ListActivity {       
    public final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    ListAdapter adapter;
    public ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>(); 
    ArrayList<String> titles = new ArrayList<String>();
    String title,artist;    
    SQLiteAdapter adp;
    SimpleCursorAdapter cursoradp;
    Cursor cursor,cursorDB; 

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

        // Adding menuItems to ListView
        final String[] from = {"songTitle", "artist","duration"};
        final int[] to={R.id.fav_songTitle,R.id.fav_songArtist,R.id.fav_duration};

        ListView lv = getListView();            
        songsListData = getFavourites();
        final ListAdapter adapter = new SimpleAdapter(this,songsListData,
                    R.layout.favouritelist_item, from, to);
        lv.setFastScrollEnabled(true);  
        setListAdapter(adapter);
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {
            public boolean onItemLongClick(AdapterView<?> av, View v,
                     final int pos,final long id) {
                final AlertDialog.Builder b = new AlertDialog.Builder(FavouriteListActivity.this);
                b.setTitle("Are you sure you want to delete?");
                b.setIcon(android.R.drawable.ic_delete);               
                b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String title = songsListData.get(pos).get("songTitle");
                        ListAdapter adapter = null;
                        adp.delete_byTitle(title);
                        songsListData.clear();
                        setListAdapter(null);
                        songsListData = getFavourites();                                                                                         
                        adapter = new SimpleAdapter(getApplicationContext(), songsListData,R.layout.favouritelist_item, from, to);

                        setListAdapter(adapter);
                        Toast.makeText(FavouriteListActivity.this,"Song is removed from Favourites", Toast.LENGTH_SHORT).show();
                    }
                });
                b.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                    }
                });
                b.show();
                return true;
            }           
        });        
    }
    public ArrayList<HashMap<String,String>> getFavourites()
    {
        ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
        ArrayList<String> titles = new ArrayList<String>();
        adp = new SQLiteAdapter(this);        
        adp.openToRead();
        cursorDB = adp.queueAll();
        if(cursorDB.moveToFirst())
        {
            for(int i=0;i<cursorDB.getCount();i++)
            {
                titles.add(cursorDB.getString(cursorDB.getColumnIndex("Title")));
                cursorDB.moveToNext();
            }
            String[] songTitles = new String[titles.size()];
            songTitles = titles.toArray(songTitles);
            if(songTitles == null)
            {
                songTitles =null;
            }
            String whereClause = MediaStore.Audio.Media.TITLE + " IN ( ?";
            if (songTitles.length>1) 
            {
                for (int j = 1 ; j < songTitles.length; ++j) 
                {
                    whereClause+=", ?";
                }
            }
            whereClause+=")";             
            Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null ,whereClause,songTitles,MediaStore.Audio.Media.TITLE);            
            if (cursor == null) 
            {
                  //Query Failed , Handle error.
            }
            else if (!cursor.moveToFirst()) 
            {
                 //No media on the device.
            }
            else
            {            
                int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);  
                int idColumn = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);                
                int artistcolumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
                int durationcolumn =cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
                for(int i=0;i<cursor.getCount();i++)
                {
                    String thisTitle = cursor.getString(titleColumn);                   
                    String path = cursor.getString(idColumn);
                    String artist = cursor.getString(artistcolumn);
                    Long duration = cursor.getLong(durationcolumn);
                    Utilities objUtilities = new Utilities();
                    String timeDuration = objUtilities.milliSecondsToTimer(duration);   
                    HashMap<String, String> song = new HashMap<String, String>();
                    song.put("songTitle",thisTitle);
                    song.put("songPath", path);
                    song.put("artist", artist);
                    song.put("duration",timeDuration);
                    // Adding each song to SongList
                    songsList.add(song);
                    cursor.moveToNext();
                 }
            }
                // looping through playlist
            for (int i = 0; i < songsList.size(); i++) {
                    // creating new HashMap
                HashMap<String, String> song = songsList.get(i);
                    // adding HashList to ArrayList
                songsListData.add(song);
            }       
        }
        return songsListData;
    }
}
  • 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-06-15T17:10:54+00:00Added an answer on June 15, 2026 at 5:10 pm

    You never clear songList, to which therefore is added all your items at each deletion.

    Which in turn are all copied to songsListData.

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

Sidebar

Related Questions

In short, in my application has ListView with data from SQLite and images from
In my android application, I've a ListView that is loaded with a lot of
In my application has a ListView. When long press on item the Context Menu
In my game application am using a listview to select levels. Am activating this
I have a ListView item in my application. The scroll position does not get
Hi I am using a Listview in my application and I created separate xml
I am using Animations in my application while switching among Activities populating ListView .
In my WPF application , I am using ListView GridView, and I implemented a
My application show a ListView with a button which allow user to add an
I'm trying to populate a ListView with data from a query using a CursorLoader.

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.