Adapter.notifyDataSetChanged () not working

There is antivity extends Activity, there is a list in this exercise to display the data requested from the SQlite database. When I click on one of the list items and alertdialog appears to make sure that I really want to delete it. But when I delete one of the data, I again request the data from the database and use notifyDataSetChanged()to update the list, but this does 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();

There is a whole 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 can’t fix it, so someone will help me, thank you very much.

+1
source share
1 answer

Try this:

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

UPDATE:

Declare this Runnable somewhere from your handler.

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

then call this from your handler.

yourActivity.runOnUiThread(run);

, Handler, .

, .

.

+3

All Articles