Android Listview显示更新后的旧内容和新内容
在我的应用程序中,listview是从SQLite数据库加载的,它使用SimpleAdapter来设置listview。当用户在listitem上执行长单击时,该项将从数据库中删除,并应更新listviw。但当我从数据库中删除该项时,listview会显示新旧数据。 请提出一些解决办法。 塔克斯 以下是我的代码:Android Listview显示更新后的旧内容和新内容,android,android-listview,listactivity,listadapter,simpleadapter,Android,Android Listview,Listactivity,Listadapter,Simpleadapter,在我的应用程序中,listview是从SQLite数据库加载的,它使用SimpleAdapter来设置listview。当用户在listitem上执行长单击时,该项将从数据库中删除,并应更新listviw。但当我从数据库中删除该项时,listview会显示新旧数据。 请提出一些解决办法。 塔克斯 以下是我的代码: public class FavouriteListActivity extends ListActivity { public final ArrayList&
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;
}
}
公共类FavoriteListActivity扩展ListActivity{
公共最终ArrayList歌曲列表=新ArrayList();
列表适配器;
public ArrayList songsListData=new ArrayList();
ArrayList titles=新的ArrayList();
字符串标题,艺术家;
sqliteadp;
SimpleCursorAdapter cursoradp;
游标游标,游标DB;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.FavoriteList);
//向ListView添加菜单项
最后一个字符串[]from={“songTitle”,“Artister”,“duration”};
final int[]to={R.id.fav_歌曲标题,R.id.fav_歌曲艺术家,R.id.fav_持续时间};
ListView lv=getListView();
songsListData=GetFavorites();
final ListAdapter=新的SimpleAdapter(此,songsListData,
R.layout.FavoriteList_项,从,到);
lv.setFastScrollEnabled(真);
setListAdapter(适配器);
lv.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener()
{
长点击(AdapterView av、View v、,
最终整数位置,最终长id){
final AlertDialog.Builder b=新建AlertDialog.Builder(FavoriteListActivity.this);
b、 setTitle(“您确定要删除吗?”);
b、 setIcon(android.R.drawable.ic_delete);
b、 setPositiveButton(“是”,新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
String title=songsListData.get(pos.get)(“songttitle”);
ListAdapter=null;
adp.删除标题(标题);
songsListData.clear();
setListAdapter(空);
songsListData=GetFavorites();
adapter=new SimpleAdapter(getApplicationContext(),songsListData,R.layout.FavoriteList_项,from,to);
setListAdapter(适配器);
Toast.makeText(FavoriteListActivity.this,“歌曲已从收藏夹中删除”,Toast.LENGTH_SHORT.show();
}
});
b、 setNegativeButton(“否”,新建DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
dialog.cancel();
}
});
b、 show();
返回true;
}
});
}
公共阵列列表GetFavorites()
{
ArrayList songsListData=新的ArrayList();
ArrayList titles=新的ArrayList();
adp=新的SQLiteAdapter(此);
adp.openToRead();
cursorDB=adp.queueAll();
if(cursorDB.moveToFirst())
{
对于(int i=0;i1)
{
对于(int j=1;j 对于(int i=0;i每次数据更改时调用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();
}
});
从数据库中删除数据后,调用
Listview.invalidateViews();
然后将适配器重置为列表
实际上,它会从数据库中删除,但它会在ListView中显示旧的列表,因此请使旧的视图无效并重置适配器。您从未清除songList,因此每次删除时都会将所有项目添加到songList中
这些数据依次被复制到songsListData。对onitemlongclick中的相同活动调用intent…只需在代码adp.delete_byTitle(title)中调用以下方法时验证该项是否已从数据库中删除。建议您无需再次设置适配器。只需调用notifyDataSetchanged()方法从列表中删除该项。在此处发布您的删除方法。如果删除成功,则应返回受影响的行数。在您的情况下,它返回的是什么。如果为0,则表示删除不成功。我建议使用TextUtils.join简化查询的构造,并建议改用while moveToNextfor loopwhat for?他正在您的适配器之前创建新的适配器2行。notifyDataSetChanged()…这将无助于做什么?他已经在代码中设置了适配器的位置setListAdapter(adapter);Toast.makeText(FavoriteListActivity。这首歌是删除的