2013-03-18 63 views
0

在我的應用程序中,我爲AsynchronousTask創建了一個單獨的類。從主類我執行異步任務類,是否有可能在異步任務類中使用列表視圖?是否有可能在Android的單獨的類中添加列表視圖?

主類

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

異步任務CLASS

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

    } 
} 

回答

0

是,讓Search_Class的AsyncTask的構造ListViewContext參數。在setContentView()之後定義ListViewonCreate()之後再呼叫AsyncTask ..

Something Like;

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

謝謝,我得到了答案。 – Yugesh 2013-03-18 06:05:37

相關問題