AnnouncementActivity.java 10 KB

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