如何在Android中从AsyncTask刷新活动

如何在Android中从AsyncTask刷新活动,android,json,android-activity,android-alertdialog,Android,Json,Android Activity,Android Alertdialog,在我的程序中,我从json URL中检索值,然后在listView中显示它们。现在我想从列表中删除一些项目,因此当我单击某个项目时,就会在AsyncTask的doInBackground()中调用delete API。。现在,在完成异步任务后,我想重新加载列表,以便可以看到更改 如何重新标记列表以查看更改 我的AsyncTask是一个单独的类 这是我的异步任务代码: protected String doInBackground(String... DATA) { // TODO Aut

在我的程序中,我从json URL中检索值,然后在listView中显示它们。现在我想从列表中删除一些项目,因此当我单击某个项目时,就会在AsyncTask的doInBackground()中调用delete API。。现在,在完成异步任务后,我想重新加载列表,以便可以看到更改

如何重新标记列表以查看更改

我的AsyncTask是一个单独的类

这是我的异步任务代码:

protected String doInBackground(String... DATA) {
    // TODO Auto-generated method stub
    rqst_type = DATA[0];
    if(rqst_type.equals("del_top5"))
    {
        String url = DATA[1];
        JsonParser jParser = new JsonParser();
        JSONObject json = jParser.getJSONfromUrl(url+memberId);
        Log.v(TAG_LOG, "del url: "+url+memberId);
        try
        {
            message = json.getString(TAG_DELSCS);
            Log.v(TAG_LOG, "msg "+TAG_DELSCS);
        }
        catch(JSONException e)
        {
            Log.v(TAG_LOG, String.valueOf(e));
        }
    }
    return null;
}


protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    if(rqst_type.equals("del_top5"))
    {
        if(message.equals("true"))
        {
            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Course Deleted");
            alertDialog.setMessage("Course Sucessfully Deleted");
            alertDialog.setIcon(R.drawable.tick);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
            alertDialog.show();
        }
        else if(message.equals("false"))
        {
            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Error");
            alertDialog.setMessage("Course Not Deleted");
            alertDialog.setIcon(R.drawable.alert);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
            alertDialog.show();
        }
        else
        {
            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setTitle("Error");
            alertDialog.setMessage("Unknown Erroe Occured");
            alertDialog.setIcon(R.drawable.alert);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
            alertDialog.show();
        }
    }
    progress.dismiss();
}
我的活动的名称是
MyTop5.class
,当我单击AlertDialog的OK按钮时,我要重新加载它

这是我的ListAdapter的涂层:

ListAdapter adapter = new SimpleAdapter(this, LoadingScreen.top5List, R.layout.top5_list,
            new String[] { TAG_GLFCRSNAME, TAG_GLFCRSID, TAG_CRTDATE, TAG_FCLTY, TAG_HOLES },
            new int[] { R.id.top_golfname, R.id.top_courseid, R.id.top_createdate, R.id.top_fclty, R.id.top_holes });
setListAdapter(adapter);

提前感谢。

我通常将活动作为参数传递给AsyncTask,并在工作完成时使用该参数调用活动上的公共方法。例如,它可以通过构造函数传入。然后,我将对列表执行所需的更改,如果您将适配器用作ListView,则必须记住在适配器上调用notifyDataSetChanged()。

您可以尝试此操作

adapter.notifyDataSetChanged();

在适配器上进行观察者调用。

我现在可以找到notifyDataSetChanged()。我的AsyncTask类的名称是LoadingScreen,因此我编写的代码是LoadingScreen.this.activity.notifyAll(),并收到一条强制关闭消息。我只收到notify()或notifyAll()。我如何才能获得notifyDataSetChanged();您必须在适配器类的实例上为ListView搜索该方法。我还将activity作为参数与上下文一起传递给AsyncTask。但是我现在可以找到notifyDataSetChanged()。我的AsyncTask类的名称是LoadingScreen,因此我编写的代码是LoadingScreen.this.activity.notifyAll(),并收到一条强制关闭消息。我只收到notify()或notifyAll()。我如何才能获得notifyDataSetChanged();notifyDataSetChanged()是适配器上的方法,而不是活动上的方法。您可以传递适配器,而不是传递活动?适配器可以访问该列表,因此您可以更改该列表,完成后,您可以访问adapter.notifyDataSetChanged()。请小心,不要更改列表的指向位置,否则会从适配器中删除它的连接。请提供一些示例代码。。。因为我仍然无法在为ListView创建适配器的同一个类中调用notifyDataSetChanged()。当我使用下面的代码时,我得到了相同的notify()和notifyAll():adapter.notify…()。我恐怕没有时间写一些东西,但请查看此链接,希望它会很好:)