我有一个简单的应用程序,可以在此处从pc世界XML
Feed中提取RSS
文件:
http://feeds.pcworld.com/pcworld/latestnews
我希望在ListView
中显示标题的名称,然后当用户选择一个文章在浏览器窗口中打开时。
应用程序正在运行唯一的事情是标题未在ListView中正确显示。
它应该是这样的:
让您的网站在Windows 8中脱颖而出
但它是这样的:
com.example.simplerss.Item@424b9998
有什么想法吗?
这是我MainActivity
public class MainActivity extends ListActivity {
ArrayAdapter<Item> adapter;
List<Item>items;//Holds item objects containing info relating to element pulled from XML file.
Item item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize variables
items = new ArrayList<Item>();
new PostTask().execute();
adapter= new ArrayAdapter<Item>(this, android.R.layout.simple_list_item_1, items);
setListAdapter(adapter);
}
private InputStream getInputStream(URL url) {
try{
return url.openConnection().getInputStream();
}catch(IOException e){
return null;
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = items.get(position).getLink();
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//ASYNC CLASS
private class PostTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg0) {
try{
//link to data source
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
//Set up parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
//get XML from input stream
InputStream in = getInputStream(url);
if (in == null) {
throw new Exception("Empty inputstream");
}
xpp.setInput(in, "UTF_8");
//Keep track of which tag inside of XML
boolean insideItem = false;
//Loop through the XML file and extract data required
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
Log.v("ENTER", String.valueOf(xpp.getEventType()));
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
//Create new item object
item = new Item();
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem){
item.setTitle(xpp.nextText());
Log.i("title", item.getTitle());
}
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem){
item.setDescription(xpp.nextText());
}
}
else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem){
item.setLink(Uri.parse(xpp.nextText()));
}
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
//add item to list
items.add(item);
}
eventType = xpp.next(); //move to next element
publishProgress();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
return "COMPLETED";
}
@Override
protected void onProgressUpdate(Integer... values) {
adapter.notifyDataSetChanged();
}
public void onPostExecute(String s) {
Toast.makeText(getApplicationContext(), s + " Items: " + items.size(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
}
}
和Item
类
public class Item {
//Variables
private String title;
private Uri link;
private String description;
public Item() {
super();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Uri getLink() {
return link;
}
public void setLink(Uri link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
答案 0 :(得分:3)
覆盖toString()
中的Item
方法。
@Override
public String toString() {
return title;
}
这应该可以解决您的问题。 现在,ArrayAdapter将其View的文本设置为Item.toString(),但这是Object的默认方法,它返回Object的ID。覆盖它,你给它一个有意义的值,在你的情况下标题。
答案 1 :(得分:-1)
我认为问题在于:
else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem){
item.setTitle(xpp.nextText());
Log.i("title", item.getTitle());
}
}
这里,getName()
是来自Java Class对象的方法。你想要的方法是readContent()我想。我没有使用过这个库,因此可能不完全正确,但您可以在docs中找到所需内容。