有一个动态扩展活动,此活动中有一个列表视图,用于显示从SQlite数据库查询的数据。当我长按其中一个listview的项目时,会出现一个alertdialog以确保我确实要删除它。
但是当我删除其中一个数据时,我再次从数据库中查询数据并使用notifyDataSetChanged()
来查看listview刷新,但它不起作用。
onCreate()
serviceListView.setOnItemLongClickListener(new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,final int arg2, long arg3)
{
Map<String, String> map = new HashMap<String, String>();
if (data!=null)
{
String[] options = new String[]{ "edit", "delete" };
map = data.get(arg2);
final String companyname= map.get("companyname");
final String serviceaddress = map.get("serveraddress");
final String id = map.get("id");
new AlertDialog.Builder(ServiceListActivity.this)
.setTitle("options")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(options,-1,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
if (which == 0)
{
dialog.dismiss();
Toast.makeText(ServiceListActivity.this, id+companyname+serviceaddress+"edit", Toast.LENGTH_SHORT).show();
}
else
{
dialog.dismiss();
AlertDialog.Builder builder = new Builder(ServiceListActivity.this);
builder.setMessage("delete?");
builder.setTitle("Notice");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Log.i("ServiceListActivity data1 = ", data.size()+"");
dialog.dismiss();
Message message = Message.obtain();
message.what = 1;
message.obj = id;
messageHandler.sendMessage(message);
}
});
builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which)
{
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}
}
}).setNegativeButton("no", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
dialog.dismiss();
}
})
.show();
有一个天生的课程:
class MessageHandler extends Handler
{
public MessageHandler(Looper looper)
{
super(looper);
}
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 1:
String id = msg.obj.toString();
int i = DatabaseTools.DeleteServer(database, Integer.parseInt(id));
if (i==1)
{
data = DatabaseTools.queryDataFromDatabase(database,cursor);
Log.i("ServiceListActivity data2 = ", data.size()+"");
myListAdapter.notifyDataSetChanged();
Toast.makeText(ServiceListActivity.this, "finished", Toast.LENGTH_SHORT).show();
}
break;
}
}
我无法解决这个问题,所以请有人帮助我,非常感谢你。
答案 0 :(得分:3)
尝试这样称呼:
runOnUiThread(new Runnable() {
public void run() {
myListAdapter.notifyDataSetChanged();
}
});
更新:
在Handler的某个地方声明这个Runnable。
Runnable run = new Runnable(){
public void run(){
myListAdapter.notifyDataSetChanged();
}
};
然后从你的处理程序中调用它。
yourActivity.runOnUiThread(run);
我认为你从Handler打来的电话正在制造问题。
希望这能解决你的问题。
感谢。