0

在我的应用程序中,我为AsynchronousTask. 从我执行异步任务类的主类中,是否可以在异步任务类中使用列表视图?

主要课程

search_items_task = new Search_class();
search_items_task.execute(search_str);

异步任务类

public class Search_class extends AsyncTask<String, Void, String> {

JSONObject json = new JSONObject();

JSONArray jsonarray;

String viewsubmenuSuccess;

//Activity activity;

ListView search_lv;

protected String doInBackground(String... params) {

    try {

        HttpClient client = new DefaultHttpClient();

        HttpResponse response;

        HttpPost post = new HttpPost("http://www.name.in/cakefoodnew/customer/submenus");

        post.setHeader("json", json.toString());
        StringEntity se = new StringEntity(json.toString());

        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        post.setEntity(se);
        response = client.execute(post);

        // get a data
        InputStream in = response.getEntity().getContent();
        String a = convertStreamToString(in);
        // Log.v("Search", ""+a);

        try {

            jsonarray = new JSONArray("[" + a + "]");
            json = jsonarray.getJSONObject(0);
            String menus = json.getString("submenus");
            viewsubmenuSuccess = json.getString("viewsubmenuSuccess");
            // Log.v("Search", ""+a);

            try {

                jsonarray = new JSONArray(menus);
                for (int ij = 0; ij < jsonarray.length(); ij++) {
                    json = jsonarray.getJSONObject(ij);
                    String name = json.getString("submenu");

                    if (name.toLowerCase().contains(params[0].toLowerCase())) {

                        String id = json.getString("submenu_id");
                        String price = json.getString("submenu_price");
                        String avaliable_quantity = json.getString("submenu_stock");

                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(MENU_ID, id);
                        map.put(MENU_NAME, name);
                        map.put(MENU_PRICE, price);
                        map.put(MENU_STOCK, avaliable_quantity);
                        search_details.add(map);
                        //Log.v("search_details", ""+search_details);
                    }
                }

            } catch (Exception e) {
                // TODO: handle exception
            }

        } catch (Exception e) {
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return viewsubmenuSuccess;
}

protected void onPostExecute(String result) {

    if (viewsubmenuSuccess.equalsIgnoreCase("1")) {

        //search_lv = (ListView)activity.findViewById(R.id.search_list_view);
//Order_page_custom for customized list view
        /*Order_page_custom adapter = new Order_page_custom(activity,search_details);
        search_lv.setAdapter(adapter);*/

    }
}
4

1 回答 1

0

是的,使用和参数创建AsyncTask的构造函数。在您的活动中定义,然后调用 AsyncTask..Search_ClassListViewContextListViewonCreate()setContentView()

就像是;

Context mContext
ListView search_lv;

public Search_class(Context context, ListView list)
{
  mContext = context;
  search_lv = list;
}

现在在

protected void onPostExecute(String result) {

    if (viewsubmenuSuccess.equalsIgnoreCase("1")) {   
        Order_page_custom adapter = new Order_page_custom(mContext,search_details);
        search_lv.setAdapter(adapter);
    }
}

并在主类(活动类)

setContentView(R.layout.<activity_layout>);

search_lv = (ListView)findViewById(R.id.search_list_view);

search_items_task = new Search_class(this, search_lv);
search_items_task.execute(search_str);
于 2013-03-18T05:42:51.817 回答