Adapter.notifyDataSetChanged()不工作 [英] Adapter.notifyDataSetChanged() is not working

查看:146
本文介绍了Adapter.notifyDataSetChanged()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个antivity延伸活动,还有这次活动列表视图来显示,从SQLite数据库查询数据。当我longclick ListView的项目之一,一alertdialog会显示出来,以确保我真的要删除它。
但是,当我删除其中的数据,然后我再从数据库中查询数据,并使用 notifyDataSetChanged()来makethe列表视图刷新,但它不能正常工作。
有没有在一些的onCreate code()

There is an antivity extends Activity, there is a listview in this activity to show the data that query from SQlite database. When I longclick one of the listview's items and a alertdialog will show up to make sure that I really want to delete it. But when I delete one of the data,then I query the data again from database and use notifyDataSetChanged() to makethe listview refresh, but it is not work. There's some code in 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();

有一个innter类:

There is a innter class:

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

我不能修复这个问题,所以有人请帮助我,非常感谢你。

I cant fix this, so somebody help me please, thank you very much.

推荐答案

尝试调用这种方式:

runOnUiThread(new Runnable() {
     public void run() {
         myListAdapter.notifyDataSetChanged();
     }
});

更新:

某处声明这Runnable的出你的处理器的。

Declare this Runnable somewhere out of your Handler.

Runnable run = new Runnable(){
     public void run(){
         myListAdapter.notifyDataSetChanged();
     }
};

然后再从处理程序调用此。

then call this from your Handler.

yourActivity.runOnUiThread(run);

我觉得你是从处理程序调用是创造问题。

I think you are calling from Handler is creating problem.

希望这能解决你的问题。

Hope this will solved your problem.

感谢。

这篇关于Adapter.notifyDataSetChanged()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆