MessageDetailActivity.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package com.usai.apex;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8. import com.usai.util.Network;
  9. import android.app.ListActivity;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.os.AsyncTask;
  13. import android.os.Bundle;
  14. import android.text.Html;
  15. import android.text.method.LinkMovementMethod;
  16. import android.util.Log;
  17. import android.view.Gravity;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.AutoCompleteTextView;
  22. import android.widget.BaseAdapter;
  23. import android.widget.ListView;
  24. import android.widget.RadioGroup;
  25. import android.widget.TextView;
  26. import android.widget.Toast;
  27. public class MessageDetailActivity extends ListActivity
  28. {
  29. long msgcount = 0;
  30. String e_id = "";
  31. String s_id = "";
  32. int limit = 10;
  33. int offset = 0;
  34. private SearchTask m_task = null;
  35. SearchResult searchresult = new SearchResult();
  36. BaseAdapter adapter = null;
  37. View view_page_footer;
  38. // String module_name = "";
  39. public void requestdata()
  40. {
  41. if (m_task != null)
  42. {
  43. return;
  44. }
  45. // mStatusMessageView.setText(R.string.str_Loading);
  46. // showProgress(true);
  47. m_task = new SearchTask();
  48. TextView text_page = (TextView) view_page_footer
  49. .findViewById(R.id.text_page);
  50. text_page.setText("Loading...");
  51. text_page.setEnabled(false);
  52. m_task.execute();
  53. }
  54. @Override
  55. protected void onListItemClick(ListView l, View v, int position, long id)
  56. {
  57. super.onListItemClick(l, v, position, id);
  58. Intent intent = new Intent();
  59. intent.setClass(this, DetailActivity.class);
  60. intent.putExtra("action0", "Tracking");
  61. intent.putExtra("function_name", "Cargo Tracking");
  62. intent.putExtra("cargo_criterion", searchresult.getData().get(position)
  63. .get("url").toString());
  64. intent.putExtra("actions_count", 1);
  65. intent.putExtra("_id", "dumb");
  66. // String h_field;
  67. intent.putExtra("criterion_type", 1);
  68. // h_field = "container_no";
  69. startActivity(intent);
  70. }
  71. @Override
  72. public void onDestroy()
  73. {
  74. if (m_task != null)
  75. m_task.cancel(false);
  76. super.onDestroy();
  77. }
  78. @Override
  79. protected void onCreate(Bundle savedInstanceState)
  80. {
  81. // setTitle("Booking Result");
  82. super.onCreate(savedInstanceState);
  83. setContentView(R.layout.message_list);
  84. adapter = new AnnouncementAdapter(searchresult, this);
  85. s_id = getIntent().getStringExtra("s_id");
  86. e_id = getIntent().getStringExtra("e_id");
  87. msgcount = getIntent().getLongExtra("msgcount", 0);
  88. view_page_footer = LayoutInflater.from(this).inflate(
  89. R.layout.view_page_footer, null);
  90. getListView().addFooterView(view_page_footer);// 添加底部视图
  91. TextView text_page = (TextView) view_page_footer
  92. .findViewById(R.id.text_page);
  93. text_page.setOnClickListener(new View.OnClickListener()
  94. {
  95. // 点击按钮 追加数据 并通知适配器
  96. @Override
  97. public void onClick(View v)
  98. {
  99. // TODO Auto-generated method stub
  100. // TextView tv = (TextView) v;
  101. // tv.setText("Loading...");
  102. offset += 10;
  103. requestdata();
  104. // tv.setText("下一页");
  105. // adapter.notifyDataSetChanged();
  106. }
  107. });
  108. setListAdapter(adapter);
  109. // this.getListView().setBackgroundColor(Color.WHITE);
  110. requestdata();
  111. }
  112. class SearchTask extends AsyncTask<Void, Void, Boolean>
  113. {
  114. int errorcode;
  115. boolean bfinish = false;
  116. @Override
  117. protected Boolean doInBackground(Void... params)
  118. {
  119. Log.d("SearchTask", "doInBackground");
  120. if (!Network.NetworkIsAvailable())
  121. {
  122. errorcode = Network.RESULT_NET_NOTAVAILABLE;
  123. return false;
  124. }
  125. String jstr = Network.get_push(e_id, s_id, limit, offset);
  126. if (jstr == null || jstr.length() <= 0)
  127. {
  128. // Log.d(TAG, "json is wrong");
  129. errorcode = Network.RESULT_NET_ERROR;
  130. return false;
  131. }
  132. JSONObject jsobj;
  133. //
  134. // array = new JSONArray(json);
  135. try
  136. {
  137. jsobj = new JSONObject(jstr);
  138. // if (searchresult.get_fieldscount() == 0)
  139. // {
  140. // JSONObject objfields = jsobj.getJSONObject("fields");
  141. // if (objfields != null)
  142. // searchresult.init_fields(objfields.toString());
  143. // }
  144. int count = jsobj.getInt("total");
  145. if (msgcount - offset <= limit)
  146. bfinish = true;
  147. JSONObject objrecords = jsobj.getJSONObject("records");
  148. if (objrecords != null)
  149. searchresult.add_records(objrecords.toString(), count);
  150. errorcode = Network.RESULT_TRUE;
  151. return true;
  152. }
  153. catch (JSONException e)
  154. {
  155. // TODO Auto-generated catch block
  156. e.printStackTrace();
  157. }
  158. errorcode = Network.RESULT_NET_ERROR;
  159. return false;
  160. }
  161. @Override
  162. protected void onPostExecute(Boolean success)
  163. {
  164. Log.i("onPostExecute", "entry");
  165. m_task = null;
  166. // showProgress(false);
  167. switch (errorcode)
  168. {
  169. case Network.RESULT_NET_NOTAVAILABLE:
  170. {
  171. Toast toast = Toast.makeText(
  172. ApexTrackingApplication.get_instance(),
  173. getText(R.string.msg_connection_none),
  174. Toast.LENGTH_LONG);
  175. toast.setGravity(Gravity.CENTER, 0, 0);
  176. toast.show();
  177. break;
  178. }
  179. case Network.RESULT_NET_ERROR:
  180. {
  181. Toast toast = Toast.makeText(
  182. ApexTrackingApplication.get_instance(),
  183. getText(R.string.msg_net_error), Toast.LENGTH_LONG);
  184. toast.setGravity(Gravity.CENTER, 0, 0);
  185. toast.show();
  186. break;
  187. }
  188. case Network.RESULT_ERROR:
  189. // case Network.RESULT_RESPONSE_NULL:
  190. {
  191. Toast toast = Toast.makeText(
  192. ApexTrackingApplication.get_instance(),
  193. getText(R.string.msg_net_resulterror),
  194. Toast.LENGTH_LONG);
  195. toast.setGravity(Gravity.CENTER, 0, 0);
  196. toast.show();
  197. break;
  198. }
  199. default:
  200. break;
  201. }
  202. if (success)
  203. {
  204. if (bfinish)
  205. {
  206. // getListView().removeFooterView(view_page_footer);
  207. // Toast.makeText(AnnouncementActivity.this, "Load all!",
  208. // Toast.LENGTH_LONG).show();
  209. TextView tv = (TextView) view_page_footer
  210. .findViewById(R.id.text_page);
  211. tv.setText("No more items");
  212. tv.setEnabled(false);
  213. }
  214. else
  215. {
  216. TextView tv = (TextView) view_page_footer
  217. .findViewById(R.id.text_page);
  218. tv.setText("More...");
  219. tv.setEnabled(true);
  220. }
  221. adapter.notifyDataSetChanged();
  222. // getListAdapter().notifyDataSetChanged;
  223. // BaseAdapter mJson = null;
  224. // mJson.notifyDataSetChanged();
  225. }
  226. super.onPostExecute(success);
  227. }
  228. @Override
  229. protected void onCancelled()
  230. {
  231. m_task = null;
  232. // showProgress(false);
  233. }
  234. }
  235. private class SearchResult
  236. {
  237. List<Map<String, Object>> datalist = new ArrayList<Map<String, Object>>();
  238. public int get_count()
  239. {
  240. return datalist.size();
  241. }
  242. public void add_records(String source, int count)
  243. {
  244. JSONObject objrecords;
  245. try
  246. {
  247. objrecords = new JSONObject(source);
  248. for (int i = 0; i < count; i++)
  249. {
  250. // offset++;
  251. JSONObject objrec = objrecords.getJSONObject("record" + i);
  252. String title = objrec.getString("title");
  253. // String content = objrec.getString("content");
  254. // String image = objrec.getString("image");
  255. // String id = objrec.getString("id");
  256. String url = objrec.getString("url");
  257. // lastid = id;
  258. Map<String, Object> map = new HashMap<String, Object>();
  259. map.put("title", title);
  260. // map.put("content", content);
  261. // map.put("image", image);
  262. // map.put("id", id);
  263. map.put("url", url);
  264. datalist.add(map);
  265. // Iterator<?> it = rec.keys();
  266. // HashMap<String, String> record = new HashMap<String,
  267. // String>();
  268. // while (it.hasNext()) // loop for each function
  269. // {
  270. // String field_name = (String) it.next();
  271. // String val = rec.getString(field_name)
  272. // .replace("\n", "");
  273. // record.put(field_name, val);
  274. // }
  275. // records.add(record);
  276. }
  277. }
  278. catch (JSONException e)
  279. {
  280. // TODO Auto-generated catch block
  281. e.printStackTrace();
  282. }
  283. }
  284. public List<Map<String, Object>> getData()
  285. {
  286. return datalist;
  287. }
  288. }
  289. private class AnnouncementAdapter extends BaseAdapter
  290. {
  291. private LayoutInflater mInflater; // 动态布局映射
  292. private SearchResult result;
  293. // private Context context;
  294. // private int i = 0;
  295. public AnnouncementAdapter(SearchResult result, Context context)
  296. {
  297. this.result = result;
  298. // this.context = context;
  299. this.mInflater = LayoutInflater.from(context);
  300. }
  301. @Override
  302. public int getCount()
  303. {
  304. // TODO Auto-generated method stub
  305. return result.get_count();
  306. }
  307. @Override
  308. public Object getItem(int position)
  309. {
  310. // TODO Auto-generated method stub
  311. return null;
  312. }
  313. @Override
  314. public long getItemId(int position)
  315. {
  316. // TODO Auto-generated method stub
  317. return 0;
  318. }
  319. @Override
  320. public View getView(int position, View convertView, ViewGroup parent)
  321. {
  322. // TODO Auto-generated method stub
  323. convertView = mInflater.inflate(R.layout.message_detail_item, null);// 根据布局文件实例化view
  324. TextView title = (TextView) convertView.findViewById(R.id.tv_title);// 找某个控件
  325. title.setText(Html.fromHtml(result.getData().get(position)
  326. .get("title").toString()));
  327. // title.setMovementMethod(LinkMovementMethod.getInstance());
  328. // title.setText(result.getData().get(position).get("title")
  329. // .toString());// 给该控件设置数据(数据从集合类中来)
  330. // TextView content = (TextView) convertView
  331. // .findViewById(R.id.tv_content);
  332. // content.setText(result.getData().get(position).get("content")
  333. // .toString());
  334. // if
  335. // (result.getData().get(position).get("image").toString().length()
  336. // > 0)
  337. // {
  338. // byte[] gzipBuff = Base64.decode(result.getData().get(position)
  339. // .get("image").toString(), 0);
  340. //
  341. // ByteArrayInputStream memstream = new ByteArrayInputStream(
  342. // gzipBuff, 0, gzipBuff.length);
  343. //
  344. // ByteArrayOutputStream baos = new ByteArrayOutputStream(
  345. // gzipBuff.length);
  346. // ImageView iv_thumb = (ImageView) convertView
  347. // .findViewById(R.id.iv_thumb);
  348. // try
  349. // {
  350. // baos.write(gzipBuff);
  351. //
  352. // Bitmap bmp = BitmapFactory.decodeStream(memstream);
  353. //
  354. // // ImageView image = new ImageView(this);
  355. //
  356. // iv_thumb.setImageBitmap(bmp);
  357. // }
  358. // catch (IOException e)
  359. // {
  360. // // TODO Auto-generated catch block
  361. // e.printStackTrace();
  362. // }
  363. // }
  364. // img.setBackgroundResource((Integer)jObject.get("img"));
  365. return convertView;
  366. }
  367. }
  368. }