Android应用程序,用于显示RSS源的标题和说明

时间:2013-01-09 05:30:46

标签: android rss

我是Android新手,我正在尝试创建一个应用程序,以便从显示“标题”和“描述”的网站获取RSS源。但在这里,我只得到标题而不是描述。在XML部分中,我有一个listview,我获取标题但不是描述。

public class abcreaderextends ListActivity  {
    List<String> headlines;
    List<String> links;
    List<String> description;

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


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

        // Initializing instance variables
        headlines = new ArrayList<String>();
        links = new ArrayList<String>();
        description= new ArrayList<String>();


        try {
            URL url = new URL("http://xxx.xxx.xxx/abc");

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xpp = factory.newPullParser();

                // We will get the XML from an input stream
            xpp.setInput(getInputStream(url), "UTF_8");

               int i=0;
              boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {

                i++;



                //Log.i("Tag : ",xpp.getName().toString());
                //Log.i("Text : ",xpp.nextText().toString());

                if (eventType == XmlPullParser.START_TAG)
                {

                    Log.i("Tag : ",xpp.getName().toString());

                    //Log.i("Text : ",xpp.nextText().toString());

                    if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;
                    } 

                    else if (xpp.getName().equalsIgnoreCase("title")) {
                        if (insideItem)
                        {   
                            String var=xpp.nextText().toString();

                        headlines.add(var); //extract the description of article
                        Log.i("Title : ",var);      
                        //Log.i("Count : ",i+"");
                        }
                    } 

                    else if (xpp.getName().equalsIgnoreCase("description")) {
                        if (insideItem)
                        {   
                            String desc=xpp.nextText().toString();

                        description.add(desc); //extract the description of article
                        Log.i("Desc : ",desc);      
                        //Log.i("Count : ",i+"");
                        }
                    } 




                    else if (xpp.getName().equalsIgnoreCase("link")) {
                        if (insideItem)
                            links.add(xpp.nextText()); //extract the link of article
                    }
                }else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
                    insideItem=false;

                }




                eventType = xpp.next(); //move to next element
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Binding data
    ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, description);

        setListAdapter(adapter);


        ArrayAdapter adaptr = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, headlines);

        setListAdapter(adaptr);


    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
       Uri uri = Uri.parse((String) links.get(position));
       Intent intent = new Intent(Intent.ACTION_VIEW, uri);
       startActivity(intent);
    }


}

2 个答案:

答案 0 :(得分:0)

您将相同的参考说明和标题绑定到同一参考, android.R.layout.simple_list_item_1

答案 1 :(得分:0)

首先创建一个类

    class myData
    {
    String title;
    String desc;

    }

声明标题和描述的setter和getter方法 现在在解析中你可以创建

ArrayList<myData> arrData=new ArrayList<MyData>();


And save your parsing data 
with arraData.add(new MyData(title,desc));
so finally you got arrayList of Your Data 

现在创建listView的自定义适配器

public class MyDataAdapter extends ArrayAdapter<myData> {
    private int resource;
    private List<myData> items;
    private Context c1;

    //In resource parameter you have to pass you cutom row xml layout likr row.xml


    public MyDataAdapter(Context context, int resorce,
            List<myData> items) {
        super(context, resorce, items);
        this.c1 = context;
        this.resource = resorce;
        this.items = items;

    }

    public class ViewHolder {
        private TextView title;
        private TextView description;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
            if (convertView == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            convertView = layoutInflater.inflate(resource, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView
                    .findViewById(R.id.title);
            holder.description = (TextView) convertView.findViewById(R.id.desc);

        holder.title.setText(items.get(position)
                .getTitle());
        holder.description.setText(items.get(position)
                .getDesc());


        return convertView;
    }

}