簡體   English   中英

如何從Android中的AsyncTask刷新活動

[英]How to refresh Activity from AsyncTask in Android

在我的程序中,我從json URL中檢索值,然后在listView中顯示它們。 現在,我想從列表中刪除一些項目,因此,當我單擊某個項目時,刪除API會調用AsyncTask的doInBackground()。現在,在完成AsyncTask之后,我想重新加載列表,以便可以看到更改。

如何重新列出列表以查看更改。

我的AsyncTask是一個單獨的類。

這是我的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);

提前致謝..

我通常將Activity作為參數傳遞給AsyncTask,並在工作完成后使用它在Activity上調用公共方法。 例如,它可以通過構造函數傳遞。 然后,我將在列表上執行所需的更改,如果您將其用作ListView,則必須記住在適配器上調用notifyDataSetChanged()。

你可以試試這個

adapter.notifyDataSetChanged();

使觀察者調用適配器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM