1

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();

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
jxdwinter
  • 2,339
  • 6
  • 36
  • 56

1 Answers1

3

Try to call this way:

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

UPDATE :

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.

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • Sir, I dont know how to use this runOnUiThread(). Will you please tell me more about it. Thank you – jxdwinter Dec 08 '12 at 07:34
  • 1
    Thank you sir, but I dont use your code but I recall the myListAdapter = new MyListAdapter(...) and serviceListView.setAdapter(myListAdapter) and it works... – jxdwinter Dec 08 '12 at 09:28