1

我是 Android 新手,我正在尝试制作一个应用程序来从显示“标题”和“描述”的网站获取 RSS 提要。但在这里,我只得到标题而不是描述。在 XML 部分中,我有一个列表视图,可以在其中获取标题但没有描述。

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


}
4

2 回答 2

0

您将描述和标题绑定到同一个参考,android.R.layout.simple_list_item_1

于 2013-01-09T05:42:15.750 回答
0

首先创建一个类

    class myData
    {
    String title;
    String desc;

    }

声明title和description的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;
    }

}
于 2013-01-09T05:45:27.037 回答