-2

在我的應用程序中,listview是從SQLite數據庫加載的,它使用SimpleAdapter來設置ListView。當用戶對listitem執行longclick時,該項目將從數據庫中刪除,並且應該更新listviw。但是,當我從數據庫中刪除項目時,listview顯示舊數據和新數據。 請建議一些解決方案。 Thanx。Listview在更新後顯示舊的和新的內容

以下是我的代碼:

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; 
    } 
} 
+0

呼叫意圖到同一個活動內的onitemLongclick ... – appukrb

+0

appu巴拉什麼? – Selvin

+0

只需在代碼adp.delete_byTitle(標題)中調用以下方法,即可驗證該項目是否從數據庫中刪除。一個建議你不需要再次設置適配器。一旦從列表中刪除項目,就調用notifyDataSetchanged()方法。 –

回答

2

你永遠不會清除songList,因此在每次刪除時都會添加所有項目。

然後將其全部複製到songsListData。

+0

和'songList'在'FavouriteListActivity'不''getFavourites' ...所以這是一個很好的答案...不像其他人一樣猜測.. @ njzk2好的眼睛無論如何它很難通過這個代碼... – Selvin

+0

Thanx mate ...它的加工。 – zanky

-2

呼叫adapter.notifyDataSetChanged()每次你的數據的變化的時間。

b.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         String title = songsListData.get(pos).get("songTitle");       
         adp.delete_byTitle(title); 
         songsListData.clear(); 
         setListAdapter(null); 
         songsListData = getFavourites();                       
         adapter = new SimpleAdapter(getApplicationContext(), songsListData,R.layout.favouritelist_item, from, to); 

         setListAdapter(adapter); 
         adapter.notifyDataSetChanged(); 
         Toast.makeText(FavouriteListActivity.this,"Song is removed from Favourites", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
+1

什麼?他在你的adapter.notifyDataSetChanged()之前創建新的適配器2行...它不會幫助 – Selvin

0

從數據庫中刪除數據後,調用

Listview.invalidateViews(); 

和適配器復位到列表中。

實際上,它從數據庫中被移除,但它顯示ListView中較早的列表,因此使舊視圖無效並重置適配器。

+1

什麼?並且他已經在代碼中設置了適配器'setListAdapter(adapter);Toast.makeText(FavouriteListActivity.this,「Song is removed from Favorites」,Toast.LENGTH_SHORT).show();'...並且設置適配器將以從佈局中移除所有視圖結束...... – Selvin