是否可以在android中的单独类中添加列表视图?

时间:2013-03-18 05:40:31

标签: android android-listview

在我的应用程序中,我为AsynchronousTask创建了一个单独的类。从主类我执行异步任务类,是否可以在异步任务类中使用列表视图?

MAIN CLASS

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);*/

    }
}

1 个答案:

答案 0 :(得分:0)

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

喜欢的东西;

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