1

私はAndroidが初めてで、「タイトル」と「説明」を表示するWebサイトから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

最初に 1 つのクラスを作成します

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

}
于 2013-01-09T05:45:27.037 に答える