首页 > 解决方案 > 在 RSS 提要标签内显示数据

问题描述

我试图只显示描述标签中的位置和大小。

出发日期/时间:Mon, 18 Jan 2021 07:41:02 ; 地点:洛沙林,高地;纬度/经度:56.552,-5.815;深度:8公里;幅度:1.9

我正在从 URL 解析数据并解析它。这是我到目前为止显示标签中所有内容的代码:

public class MainActivity extends AppCompatActivity {

        ListView lvRss;
        ArrayList mLink;
        ArrayList mTitle;
        ArrayList mGeolat;
        ArrayList mGeolong;
        ArrayList mDescription;
        ArrayList mPubdate;
        ArrayList mCategory;
        ArrayList mLocation;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            lvRss = (ListView) findViewById(R.id.lvRss);

            mTitle = new ArrayList();
            mDescription = new ArrayList();
            mLink = new ArrayList();
            mGeolat = new ArrayList();
            mGeolong = new ArrayList();
            mPubdate = new ArrayList();
            mCategory = new ArrayList();
            mLocation = new ArrayList();


            lvRss.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // TODO: Process clicked item here
                   Uri uri = Uri.parse((mLink.get(position)).toString());
                   Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                   startActivity(intent);
               }
          });

            new ProcessBackground().execute();
        }

        public InputStream getInputStream(URL url) {
            try {
                return url.openConnection().getInputStream();
            } catch (IOException e) {
                return null;
            }
        }

        public class ProcessBackground extends AsyncTask<Object, Void, Exception> {

            Exception exception = null;

            @Override
            protected Exception doInBackground(Object[] params) {

                try {
                    URL url = new URL("http://quakes.bgs.ac.uk/feeds/MhSeismology.xml");
                    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    factory.setNamespaceAware(false);
                    XmlPullParser xpp = factory.newPullParser();
                    xpp.setInput(getInputStream(url), "UTF_8");
                    boolean insideItem = false;
                    int eventType = xpp.getEventType();
                    while (eventType != XmlPullParser.END_DOCUMENT) {
                        if (eventType == XmlPullParser.START_TAG) {
                            if (xpp.getName().equalsIgnoreCase("item")) {
                                insideItem = true;
                            } else if (xpp.getName().equalsIgnoreCase("title")) {
                                if (insideItem) {
                                    mTitle.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("description")) {
                                if (insideItem) {
                                    mDescription.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("link")) {
                                if (insideItem) {
                                    mLink.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("pubdate")) {
                                if (insideItem) {
                                    mPubdate.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("category")) {
                                if (insideItem) {
                                    mCategory.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("geo:lat")) {
                                if (insideItem) {
                                    mGeolat.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("geo:long")) {
                                if (insideItem) {
                                    mGeolong.add(xpp.nextText());
                                }
                            } else if (xpp.getName().equalsIgnoreCase("description")) {
                                if (insideItem) {
                                    mLocation.add(xpp.nextText());
                                }
                            }
                        } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                            insideItem = false;
                        }
                        eventType = xpp.next();
                    }
                } catch (MalformedURLException e) {
                    exception = e;
                } catch (IOException e) {
                    exception = e;
                } catch (XmlPullParserException e) {
                    exception = e;
                }
                return exception;
            }

            @Override
            protected void onPostExecute(Exception s) {
                super.onPostExecute(s);

                ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mDescription);
                lvRss.setAdapter(adapter);

            }
        }
    }

标签: android-studioxml-parsingdisplay

解决方案


推荐阅读