android-studio - 在 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);
}
}
}
解决方案
推荐阅读
- sql - 将现有记录的副本创建到新行中并更新一些值
- sql - 基于具有不同值的单独列重复一组列的选择
- html - 使用引导程序将 svg/div 居中到中心
- webpack - webpack 开发服务器不运行 piz 帮帮我
- rust - 正确 2 TcpStream
- javascript - 尝试提升 React 状态,同时保持对较低计算变量的访问
- debugging - IntelliJ 或 VSCode 是否能够设置不停止执行的断点,而是拍摄您可以查看的堆栈/变量的快照?
- java - 掷骰子模拟无限循环
- sqlite - 如何打开Sqlite3中通常关闭的功能?
- python - M1 Mac - 安装 pygame 2.0