Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.25 KB | None | 0 0
  1. public class AstActivity extends Activity {
  2.  
  3. ArrayList<Product> products = new ArrayList<Product>();
  4. ArrayList<Product_place> products2 = new ArrayList<Product_place>();
  5. ArrayList<Item> productsAstNews = new ArrayList<Item>();
  6. Context ctxx;
  7.  
  8. BoxAdapter boxAdapter;
  9. BoxAdapter2 boxAdapter2;
  10.  
  11. void fillData() {
  12. products.add(new Product("КОСТАНАЙ", R.drawable.ic_announcement_grey));
  13. products.add(new Product("АСТАНА", R.drawable.ic_assignment_grey));
  14. products.add(new Product("НАСТРОЙКИ", R.drawable.ic_settings_grey));
  15. products.add(new Product("О ПРИЛОЖЕНИИ", R.drawable.ic_info_grey));
  16. products.add(new Product("ВЫХОД", R.drawable.ic_exit_to_app_grey));
  17. }
  18.  
  19. void fillData2() {
  20. products2.add(new Product_place("Хан Шатыр", "пр. Туран 37", getString(R.string.xml5), R.drawable.hansh));
  21. products2.add(new Product_place("Ак Орда", "Левобережье", getString(R.string.xml4), R.drawable.akorda));
  22. products2.add(new Product_place("Байтерек", "ул. Сарайшык 38" , getString(R.string.xml1), R.drawable.baiterek));
  23. products2.add(new Product_place("Дворец мира и согласия", "пр. Тауелсиздик 57", getString(R.string.xml3), R.drawable.museum2));
  24. products2.add(new Product_place("Музей первого президента", "ул. Бейбитшилик 11", getString(R.string.xml2), R.drawable.museum));
  25. products2.add(new Product_place("Думан", "Есильский р-н", getString(R.string.xml6), R.drawable.duman));
  26. products2.add(new Product_place("Атамекен", "Коргалжинское ш 2/1", getString(R.string.xml7), R.drawable.atameken));
  27. products2.add(new Product_place("Цирк", "пр.Кабанбай Батыра 5", getString(R.string.xml8), R.drawable.cirk));
  28. }
  29.  
  30. void fillData3() {
  31. loadXml();
  32. }
  33.  
  34. @Override
  35. public void onBackPressed() {
  36. finish();
  37. startActivity(new Intent(AstActivity.this, AstActivity.class));
  38. }
  39.  
  40. String valOfTag(String s, String tag) // выдаёт значение тега (tag) и удаляет его из строки.
  41. {
  42. String TAG_START = "<" + tag + ">";
  43. String TAG_END = "</" + tag + ">";
  44.  
  45. int l = s.indexOf(TAG_START);
  46. int r = s.indexOf(TAG_END);
  47.  
  48. if(r == -1 || l == -1)
  49. {
  50. return "not_found";
  51. }
  52. else
  53. {
  54. String valueOfTag = s.substring(l + TAG_START.length() + 1, r );
  55. return valueOfTag;
  56. }
  57. }
  58. String valOfLink(String s)
  59. {
  60. String tag = "enclosure" ;
  61. String TAG_START = "<" + tag;
  62. String TAG_END = "</" + tag + ">";
  63.  
  64. int l = s.indexOf(TAG_START);
  65. int r = s.indexOf(TAG_END);
  66.  
  67. if(r == -1 || l == -1)
  68. {
  69. return "not_found";
  70. }
  71. else
  72. {
  73. String valueOfTag = s.substring(l + TAG_START.length() + 1, r );
  74. valueOfTag = valueOfTag.substring(valueOfTag.indexOf("url="") + 5, valueOfTag.length() - 3);
  75. //Log.d("borolis", valueOfTag);
  76. return valueOfTag;
  77. }
  78. }
  79.  
  80.  
  81. @Override
  82. public void onCreate(Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84.  
  85. setContentView(R.layout.activity_ast);
  86.  
  87. TabHost tabs = (TabHost) findViewById(R.id.tabhosts);
  88. ctxx = getApplicationContext();
  89. tabs.setup();
  90.  
  91. TabHost.TabSpec spec = tabs.newTabSpec("tag1");
  92.  
  93. spec.setContent(R.id.tabs1);
  94. spec.setIndicator("О городе");
  95. tabs.addTab(spec);
  96.  
  97. spec = tabs.newTabSpec("tag2");
  98. spec.setContent(R.id.tabs2);
  99. spec.setIndicator("Что посетить?");
  100. tabs.addTab(spec);
  101.  
  102. spec = tabs.newTabSpec("tag3");
  103. spec.setContent(R.id.tabs3);
  104. spec.setIndicator("Новости");
  105. tabs.addTab(spec);
  106.  
  107. tabs.setCurrentTab(0);
  108.  
  109. fillData();
  110. fillData2();
  111. fillData3();
  112.  
  113. boxAdapter = new BoxAdapter(this, products);
  114. boxAdapter2 = new BoxAdapter2(this, products2);
  115.  
  116. ListView lvMain2 = (ListView) findViewById(R.id.left_drawerAst);
  117. ListView lvMain3 = (ListView) findViewById(R.id.llInfo2);
  118.  
  119. lvMain2.setAdapter(boxAdapter);
  120. lvMain3.setAdapter(boxAdapter2);
  121.  
  122.  
  123. lvMain2.setOnItemClickListener(new OnItemClickListener() {
  124. public void onItemClick(AdapterView<?> parent, View view,
  125. int position, long id) {
  126. DrawerLayout mDrawerLayout;
  127. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
  128. mDrawerLayout.closeDrawers();
  129.  
  130. if (position == 2) {
  131. startActivity(new Intent(AstActivity.this, SettingsActivity.class));
  132. finish();
  133. }
  134. if (position == 0) {
  135. startActivity(new Intent(AstActivity.this, RssActivity.class));
  136. finish();
  137. }
  138.  
  139. if (position == 4) {
  140. Process.killProcess(Process.myPid());
  141. }
  142.  
  143. }
  144. });
  145.  
  146. lvMain3.setOnItemClickListener(new OnItemClickListener() {
  147. public void onItemClick(AdapterView<?> parent, View view,
  148. int position, long id) {
  149. DrawerLayout mDrawerLayout;
  150. mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
  151. mDrawerLayout.closeDrawers();
  152.  
  153. Intent i = new Intent(AstActivity.this, mbrowser.class);
  154. String url = "http://slmusum.esy.es/ast/" + Integer.toString(position) + ".html";
  155.  
  156. Product_place now = products2.get(position);
  157.  
  158. i.putExtra("gps", "Алматы, " + now.adress);
  159. i.putExtra("nameOfPlace", now.name);
  160. i.putExtra("url", url);
  161. startActivity(i);
  162. }
  163. });
  164. }
  165.  
  166. private void loadXml()
  167. {
  168. AsyncTask<String, Element, String> task = new AsyncTask<String, Element, String>() {
  169. @Override
  170. protected void onPreExecute()
  171. {
  172. super.onPreExecute();
  173. }
  174.  
  175. @Override
  176. protected String doInBackground(String... params)
  177. {
  178. String doc = "";
  179. try
  180. {
  181. doc = new Scanner(new URL("https://vlast.kz/feed/").openStream(), "UTF-8").useDelimiter("\A").next();
  182. }
  183.  
  184. catch (IOException e)
  185. {
  186. e.printStackTrace();
  187. }
  188. String itemx = "";
  189. while(itemx != "not_found")
  190. {
  191. itemx = valOfTag(doc, "item");
  192. if(itemx != "not_found")
  193. {
  194. int l = doc.indexOf("<item>");
  195. int r = doc.indexOf("</item>");
  196. doc = doc.substring(0, l) + doc.substring(r + 7, doc.length());
  197.  
  198. String nameI = valOfTag(itemx, "title"); //разбираем запись на нужные нам данные
  199. String linkI = valOfTag(itemx, "link"); //
  200. String pubDateI = valOfTag(itemx, "pubDate"); //
  201. String descriptionI = valOfTag(itemx, "description");
  202. String imageLinkI = valOfLink(itemx);
  203.  
  204.  
  205. descriptionI= descriptionI.replaceAll("(?s)\<em\>.*s?\<\/em\>", "");
  206. descriptionI= descriptionI.replaceAll("(?s)\<i\>.*s?\<\/i\>", "");
  207. nameI = nameI.replaceAll(""", "");
  208. descriptionI= descriptionI.replaceAll("\<.*?\>", "");
  209. descriptionI= descriptionI.substring(9, descriptionI.length()-4);
  210.  
  211. String pubDate="";
  212. SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
  213. Date d = null;
  214. try
  215. {
  216. d = org.apache.http.impl.cookie.DateUtils.parseDate(pubDateI);
  217. pubDate = format.format(d);
  218. }
  219. catch (DateParseException e)
  220. {
  221. e.printStackTrace();
  222. }
  223.  
  224. Item newx = new Item(nameI, imageLinkI, descriptionI, linkI, pubDate);
  225. productsAstNews.add(newx);
  226. }
  227. }
  228. return "allDone";
  229. }
  230.  
  231. @Override
  232. protected void onPostExecute(String aVoid)
  233. {
  234. AdapterItem2 boxAdapter3 = new AdapterItem2(ctxx, productsAstNews);
  235. ListView lvMain4 = (ListView) findViewById(R.id.vlastView);
  236. lvMain4.setAdapter(boxAdapter3);
  237. }
  238. };
  239. task.execute();
  240. }
  241. }
  242.  
  243. public class AdapterItem2 extends BaseAdapter {
  244.  
  245. Context ctx;
  246. LayoutInflater lInflater;
  247. ArrayList<Item> objects;
  248.  
  249. AdapterItem2(Context context, ArrayList<Item> products) {
  250. ctx = context;
  251. objects = products;
  252. lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  253. }
  254.  
  255. // кол-во элементов
  256. @Override
  257. public int getCount() {
  258. return objects.size();
  259. }
  260.  
  261. // элемент по позиции
  262. @Override
  263. public Object getItem(int position) {
  264. return objects.get(position);
  265. }
  266.  
  267. // id по позиции
  268. @Override
  269. public long getItemId(int position) {
  270. return position;
  271. }
  272.  
  273. // пункт списка
  274. @Override
  275. public View getView(int position, View convertView, ViewGroup parent) {
  276. View view = convertView;
  277. if (view == null)
  278. {
  279. view = lInflater.inflate(R.layout.city_item, parent, false);
  280. }
  281. Item p = objects.get(position);
  282. ((TextView) view.findViewById(R.id.cityTitle)).setText(p.getTitle());
  283. ((TextView) view.findViewById(R.id.cityDescription)).setText(p.getDescription());
  284. ((TextView) view.findViewById(R.id.cityAdress)).setText(p.getDate());
  285. loadImage(p.getUrlImage(), ((ImageView) view.findViewById(R.id.cityImage)));
  286.  
  287. return view;
  288. }
  289.  
  290.  
  291.  
  292.  
  293. private void loadImage(final String urlImage,final ImageView imageView)
  294. {
  295. AsyncTask<String,ImageView,Bitmap> taskx = new AsyncTask<String, ImageView, Bitmap>()
  296. {
  297. @Override
  298. protected Bitmap doInBackground(String... params)
  299. {
  300. Bitmap image = null;
  301. try
  302. {
  303. URL url = new URL(params[0]);
  304. image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
  305. }
  306. catch (Exception ex)
  307. {
  308. ex.printStackTrace();
  309. }
  310. return image;
  311. }
  312. @Override
  313. protected void onPostExecute(Bitmap bitmap) {
  314. imageView.setImageBitmap(bitmap);
  315. }
  316. };
  317. taskx.execute(urlImage);
  318. }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement