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);*/
}
}
謝謝,我得到了答案。 – Yugesh 2013-03-18 06:05:37