我有一个带有 SimpleCursorAdapter 的 ListView,它从数据库加载信息,我希望 onlongclicklistener 从数据库中删除一个项目,它工作正常,但列表不会刷新,我尝试使用 listView.refreshDrawableState(); 以及我找到的一些其他方法作为答案,但没有一种方法有效,这是我的 onCreate 函数:
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;
}
});