Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AstActivity extends Activity {
- ArrayList<Product> products = new ArrayList<Product>();
- ArrayList<Product_place> products2 = new ArrayList<Product_place>();
- ArrayList<Item> productsAstNews = new ArrayList<Item>();
- Context ctxx;
- BoxAdapter boxAdapter;
- BoxAdapter2 boxAdapter2;
- void fillData() {
- products.add(new Product("КОСТАНАЙ", R.drawable.ic_announcement_grey));
- products.add(new Product("АСТАНА", R.drawable.ic_assignment_grey));
- products.add(new Product("НАСТРОЙКИ", R.drawable.ic_settings_grey));
- products.add(new Product("О ПРИЛОЖЕНИИ", R.drawable.ic_info_grey));
- products.add(new Product("ВЫХОД", R.drawable.ic_exit_to_app_grey));
- }
- void fillData2() {
- products2.add(new Product_place("Хан Шатыр", "пр. Туран 37", getString(R.string.xml5), R.drawable.hansh));
- products2.add(new Product_place("Ак Орда", "Левобережье", getString(R.string.xml4), R.drawable.akorda));
- products2.add(new Product_place("Байтерек", "ул. Сарайшык 38" , getString(R.string.xml1), R.drawable.baiterek));
- products2.add(new Product_place("Дворец мира и согласия", "пр. Тауелсиздик 57", getString(R.string.xml3), R.drawable.museum2));
- products2.add(new Product_place("Музей первого президента", "ул. Бейбитшилик 11", getString(R.string.xml2), R.drawable.museum));
- products2.add(new Product_place("Думан", "Есильский р-н", getString(R.string.xml6), R.drawable.duman));
- products2.add(new Product_place("Атамекен", "Коргалжинское ш 2/1", getString(R.string.xml7), R.drawable.atameken));
- products2.add(new Product_place("Цирк", "пр.Кабанбай Батыра 5", getString(R.string.xml8), R.drawable.cirk));
- }
- void fillData3() {
- loadXml();
- }
- @Override
- public void onBackPressed() {
- finish();
- startActivity(new Intent(AstActivity.this, AstActivity.class));
- }
- String valOfTag(String s, String tag) // выдаёт значение тега (tag) и удаляет его из строки.
- {
- String TAG_START = "<" + tag + ">";
- String TAG_END = "</" + tag + ">";
- int l = s.indexOf(TAG_START);
- int r = s.indexOf(TAG_END);
- if(r == -1 || l == -1)
- {
- return "not_found";
- }
- else
- {
- String valueOfTag = s.substring(l + TAG_START.length() + 1, r );
- return valueOfTag;
- }
- }
- String valOfLink(String s)
- {
- String tag = "enclosure" ;
- String TAG_START = "<" + tag;
- String TAG_END = "</" + tag + ">";
- int l = s.indexOf(TAG_START);
- int r = s.indexOf(TAG_END);
- if(r == -1 || l == -1)
- {
- return "not_found";
- }
- else
- {
- String valueOfTag = s.substring(l + TAG_START.length() + 1, r );
- valueOfTag = valueOfTag.substring(valueOfTag.indexOf("url="") + 5, valueOfTag.length() - 3);
- //Log.d("borolis", valueOfTag);
- return valueOfTag;
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_ast);
- TabHost tabs = (TabHost) findViewById(R.id.tabhosts);
- ctxx = getApplicationContext();
- tabs.setup();
- TabHost.TabSpec spec = tabs.newTabSpec("tag1");
- spec.setContent(R.id.tabs1);
- spec.setIndicator("О городе");
- tabs.addTab(spec);
- spec = tabs.newTabSpec("tag2");
- spec.setContent(R.id.tabs2);
- spec.setIndicator("Что посетить?");
- tabs.addTab(spec);
- spec = tabs.newTabSpec("tag3");
- spec.setContent(R.id.tabs3);
- spec.setIndicator("Новости");
- tabs.addTab(spec);
- tabs.setCurrentTab(0);
- fillData();
- fillData2();
- fillData3();
- boxAdapter = new BoxAdapter(this, products);
- boxAdapter2 = new BoxAdapter2(this, products2);
- ListView lvMain2 = (ListView) findViewById(R.id.left_drawerAst);
- ListView lvMain3 = (ListView) findViewById(R.id.llInfo2);
- lvMain2.setAdapter(boxAdapter);
- lvMain3.setAdapter(boxAdapter2);
- lvMain2.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- DrawerLayout mDrawerLayout;
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
- mDrawerLayout.closeDrawers();
- if (position == 2) {
- startActivity(new Intent(AstActivity.this, SettingsActivity.class));
- finish();
- }
- if (position == 0) {
- startActivity(new Intent(AstActivity.this, RssActivity.class));
- finish();
- }
- if (position == 4) {
- Process.killProcess(Process.myPid());
- }
- }
- });
- lvMain3.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- DrawerLayout mDrawerLayout;
- mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout1);
- mDrawerLayout.closeDrawers();
- Intent i = new Intent(AstActivity.this, mbrowser.class);
- String url = "http://slmusum.esy.es/ast/" + Integer.toString(position) + ".html";
- Product_place now = products2.get(position);
- i.putExtra("gps", "Алматы, " + now.adress);
- i.putExtra("nameOfPlace", now.name);
- i.putExtra("url", url);
- startActivity(i);
- }
- });
- }
- private void loadXml()
- {
- AsyncTask<String, Element, String> task = new AsyncTask<String, Element, String>() {
- @Override
- protected void onPreExecute()
- {
- super.onPreExecute();
- }
- @Override
- protected String doInBackground(String... params)
- {
- String doc = "";
- try
- {
- doc = new Scanner(new URL("https://vlast.kz/feed/").openStream(), "UTF-8").useDelimiter("\A").next();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- String itemx = "";
- while(itemx != "not_found")
- {
- itemx = valOfTag(doc, "item");
- if(itemx != "not_found")
- {
- int l = doc.indexOf("<item>");
- int r = doc.indexOf("</item>");
- doc = doc.substring(0, l) + doc.substring(r + 7, doc.length());
- String nameI = valOfTag(itemx, "title"); //разбираем запись на нужные нам данные
- String linkI = valOfTag(itemx, "link"); //
- String pubDateI = valOfTag(itemx, "pubDate"); //
- String descriptionI = valOfTag(itemx, "description");
- String imageLinkI = valOfLink(itemx);
- descriptionI= descriptionI.replaceAll("(?s)\<em\>.*s?\<\/em\>", "");
- descriptionI= descriptionI.replaceAll("(?s)\<i\>.*s?\<\/i\>", "");
- nameI = nameI.replaceAll(""", "");
- descriptionI= descriptionI.replaceAll("\<.*?\>", "");
- descriptionI= descriptionI.substring(9, descriptionI.length()-4);
- String pubDate="";
- SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
- Date d = null;
- try
- {
- d = org.apache.http.impl.cookie.DateUtils.parseDate(pubDateI);
- pubDate = format.format(d);
- }
- catch (DateParseException e)
- {
- e.printStackTrace();
- }
- Item newx = new Item(nameI, imageLinkI, descriptionI, linkI, pubDate);
- productsAstNews.add(newx);
- }
- }
- return "allDone";
- }
- @Override
- protected void onPostExecute(String aVoid)
- {
- AdapterItem2 boxAdapter3 = new AdapterItem2(ctxx, productsAstNews);
- ListView lvMain4 = (ListView) findViewById(R.id.vlastView);
- lvMain4.setAdapter(boxAdapter3);
- }
- };
- task.execute();
- }
- }
- public class AdapterItem2 extends BaseAdapter {
- Context ctx;
- LayoutInflater lInflater;
- ArrayList<Item> objects;
- AdapterItem2(Context context, ArrayList<Item> products) {
- ctx = context;
- objects = products;
- lInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- }
- // кол-во элементов
- @Override
- public int getCount() {
- return objects.size();
- }
- // элемент по позиции
- @Override
- public Object getItem(int position) {
- return objects.get(position);
- }
- // id по позиции
- @Override
- public long getItemId(int position) {
- return position;
- }
- // пункт списка
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null)
- {
- view = lInflater.inflate(R.layout.city_item, parent, false);
- }
- Item p = objects.get(position);
- ((TextView) view.findViewById(R.id.cityTitle)).setText(p.getTitle());
- ((TextView) view.findViewById(R.id.cityDescription)).setText(p.getDescription());
- ((TextView) view.findViewById(R.id.cityAdress)).setText(p.getDate());
- loadImage(p.getUrlImage(), ((ImageView) view.findViewById(R.id.cityImage)));
- return view;
- }
- private void loadImage(final String urlImage,final ImageView imageView)
- {
- AsyncTask<String,ImageView,Bitmap> taskx = new AsyncTask<String, ImageView, Bitmap>()
- {
- @Override
- protected Bitmap doInBackground(String... params)
- {
- Bitmap image = null;
- try
- {
- URL url = new URL(params[0]);
- image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- return image;
- }
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- imageView.setImageBitmap(bitmap);
- }
- };
- taskx.execute(urlImage);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement