0

XMLここに、PC WorldsRSSフィードからファイルを取得する単純なアプリケーションがあります。

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

タイトルの名前を表示したいのですがListView、ユーザーがタイトルを選択すると、記事がブラウザ ウィンドウに表示されます。

アプリケーションは機能していますが、ListView でタイトルが正しく表示されていません。

次のようになります。

Windows 8 で Web サイトを目立たせる

しかし、代わりにこれは次のとおりです。

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;
}

}

4

2 に答える 2

3

toString()のメソッドをオーバーライドしItemます。

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

これで問題が解決するはずです。現在、ArrayAdapter はビューのテキストを Item.toString() に設定していますが、これはオブジェクトの ID を返すオブジェクトのデフォルトのメソッドです。それをオーバーライドすることで、意味のある値を与えます。あなたの場合はタイトルです。

于 2013-03-22T22:18:24.227 に答える
-1

問題は次の点にあると思います。

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

getName()これは、Java Class オブジェクトのメソッドです必要なメソッドはreadContent()だと思います。私はこのライブラリを使用していないので、正確ではないかもしれませんが、docsで必要なものを確実に見つけることができます。

于 2013-03-22T22:18:49.180 に答える