2013-03-22 86 views
0

我有一個簡單的應用程序,它是直接從PC世界RSS飼料這裏的XML文件:ListView中顯示不正確的標題

http://feeds.pcworld.com/pcworld/latestnews

我想在ListView顯示標題的名稱,然後當用戶選擇一個文章在瀏覽器窗口中打開。

應用程序正在運行唯一的問題是標題在ListView中沒有正確顯示。

應該是這樣的:

讓你的網站脫穎而出,與Windows 8

但取而代之的則是這樣的:

[email protected]

任何想法?

這是我的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; 
} 

}

回答

3

覆蓋的toString()方法Item

@Override 
public String toString() { 
    return title; 
} 

這應該可以解決您的問題。 現在,ArrayAdapter將其視圖的文本設置爲Item.toString(),但這是返回Object的ID的Object的默認方法。重寫它,你給它一個有意義的價值,在你的情況下標題。

+0

謝謝修復它!當我被允許時將在2分鐘內接受 – Javacadabra 2013-03-22 22:19:32

-1

我認爲問題在於:

else if (xpp.getName().equalsIgnoreCase("title")) { 
     if (insideItem){ 
      item.setTitle(xpp.nextText()); 
      Log.i("title", item.getTitle()); 
     } 
} 

這裏,getName()是從Java Class對象的方法。你想要的方法是readContent()我想。我沒有使用這個庫,因此可能不完全正確,但您可以在docs中找到您需要的內容。