我有一個ListView與SimpleCursorAdapter,它從數據庫加載信息,我想onlongclicklistener從數據庫中刪除一個項目,它工作正常,但列表不刷新,我試着用listView。 refreshDrawableState();而且,我發現作爲答案,但沒有一個人工作的一些其他的方法,這是我的onCreate功能:刷新ListView後從數據庫中刪除
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_act);
imzero = (ImageView)findViewById (R.id.iv_zero_pers);
tvzero = (TextView)findViewById (R.id.tv_zero_pers);
dbConnector = new DatabaseConnectorrso(MyListActivity.this);
dbConnector.checkCursor();
if (DatabaseConnector.de)//check if database is emplty
{
try{
dbConnector.open();
dbConnector.load();//load information from database
dbConnector.close();
imzero.setVisibility(View.GONE);
tvzero.setVisibility(View.GONE);
}
catch(Exception ex) {
ex.printStackTrace();
dbConnector.close();
}
conAdapter =(new ImageAndTextAdapter(MyListActivity.this,
R.layout.meniu_lista_element, null, null, null));
setListAdapter(conAdapter);
}
else{
dbConnector.close();
imzero.setVisibility(View.VISIBLE);
tvzero.setVisibility(View.VISIBLE);
}
,我試圖刷新列表這樣
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
deleteDialog(Integer.parseInt (DatabaseConnector.id.get(position))); //delete from database
dbConnector.checkCursor();
if (DatabaseConnector.de)
{
try{
dbConnector.open();
dbConnector.loadPersoane();
listView.refreshDrawableState();
dbConnector.close();
imzero.setVisibility(View.GONE);
tvzero.setVisibility(View.GONE);
}
catch(Exception ex) {
ex.printStackTrace();
dbConnector.close();
}
conAdapter =(new ImageAndTextAdapter(AltePersoane.this,
R.layout.meniu_lista_element, null, null, null));
setListAdapter(conAdapter);
}
else{
dbConnector.close();
imzero.setVisibility(View.VISIBLE);
tvzero.setVisibility(View.VISIBLE);
}
return true;
}
});
再次查詢數據庫,得到新的光標,然後通知數據集在列表視圖 –