0

i m new to android i m just trying to load rss(xml data) in to listview,i need to add title and pubdata on listview...i m loading title in listview but not idea how to load pubdate as well..pls help me for the same.....thanxs in advance...

here is a code,,,title is loading sucsessfully,need to load date in listview..on single row

try {

             URL url = new URL("rss link <contain title,pubDate tag> ");


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


             xpp.setInput(getInputStream(url), "UTF_8");

             boolean insideItem = false;


             int eventType = xpp.getEventType();
             while (eventType != XmlPullParser.END_DOCUMENT) {

                 if (eventType == XmlPullParser.START_TAG) {

                     if (xpp.getName().equalsIgnoreCase("item")) {
                         insideItem = true;
                     } else if (xpp.getName().equalsIgnoreCase("title")) {
                         if (insideItem)
                             headlines.add(xpp.nextText()); //extract the headline
                     } else if (xpp.getName().equalsIgnoreCase("link")) {
                         if (insideItem)
                             links.add(xpp.nextText()); //extract the link of article
                     } else if (xpp.getName().equalsIgnoreCase("pubDate")) {
                         if (insideItem)
                             pd.add(xpp.nextText()); //extract the pub date 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_expandable_list_item_1, headlines);

         setListAdapter(adapter);


     }
veer1987
  • 53
  • 1
  • 6

2 Answers2

0

You need to create a CustomArrayAdapter and pass both the arrays to that adapter.

Override getView method in adapter which gives you position based on it create a view with two textviews and set them respectively.

Follow this for more info on customArrayAdapter:

https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0

Before coding pls read the below

How ListView's recycling mechanism works

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

You can use a Custom Adapter

listview.setAdapter (new CustomAdapter(ActivityName.this,R.layout.customlayout,pub,headlines));

Then

public class CustomAdapter extends ArrayAdapter
{

  ArrayList<String> pub,headlines;
  LayoutInflater mInlfater;   
  public CUstomAdapter(Context context,int layout,ArrayList<String> pub,ArrayList<String> headlines)
  {
      super(context,layout,pub);
      mInflater = LayoutInflater.from(context);
      this.pub= pub;
      this.headlines = headlines;  
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder holder; 

   if (convertView == null) {
    convertView = mInflater.inflate(R.layout.list_row, parent,false);
    holder = new ViewHolder();
    holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
    holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
    convertView.setTag(holder);
   }
   else {
    // view already exists, get the holder instance from the view
    holder = (ViewHolder) convertView.getTag();
   }
    holder.tv1.setText(pub.get(position).toString());
    holder.tv2.setText(headlines.get(position).toString());
    return convertView
   }
    static class ViewHolder {
      TextView tv1;
      TextView tv2;

    }  

}

list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="44dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="64dp"
        android:text="TextView" />

</RelativeLayout>
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @Viren just create a new xml. a custom array adapter and copy the above and it should work assuming pub and headlines are a arraylist of strings already populated with items – Raghunandan Jan 12 '14 at 07:58
  • @ Raghunandan i cant get what i need,two textview uses in custome adapter and you have mention the set adapter like listview.setAdapter (new CustomAdapter(ActivityName.this,R.layout.customlayout,pub,headlines) ); the list view with different xml? which bind in load rss java file?, sooo where i have to put this in my above code or wher i dont understand i put this code at my above post code it not work,Force Close error occures......so i dont know what to do i require this is as my project work if possible email me,if u hve avilable,..i m very thankful to you. – veer1987 Jun 13 '14 at 09:38
  • @Viren you have 2 textview in the given code above and set the adapter to listview. `list_item.xml` has 2 textviews. You have a Custom adapter set to listview. What else do you want?? – Raghunandan Jun 13 '14 at 09:40
  • in my first post code what i have to call in oncreate list_item or my listview xml? – veer1987 Jun 13 '14 at 09:44
  • @Viren you have to set the content of the layout which has to listview to the activity. Then in your cusotm adapter inflate a layout which has 2 textviews. This is already shown in my post. – Raghunandan Jun 13 '14 at 09:45
  • @Viren SO is not mailing... Pls understand the working code provided. Nobody will mail. – Raghunandan Jun 13 '14 at 09:46
  • ok,just tell me in custome adapter i have call list_item.xml and in the above post code how i call,is there any listview.xml file need to create,which file will load data,only setlistadapter will load data? – veer1987 Jun 13 '14 at 09:49
  • @Viren you read about custom adapters first. Adapter acts as a bridge between your data and the listview. setAdapter set the adapter to listview. Now what is your problem?? – Raghunandan Jun 13 '14 at 09:55
  • i load data in listview are in two language it may be gujarati and may be english for listview,english are load successfully but gujarati not load(boxes are seen) so how would i load gujarati as well.can i do this with put gujarati font in assets folder,how to do this pls help... – veer1987 Jun 13 '14 at 10:12
  • @Viren read about localization. If the language is not supported by android you need to use a some other work around. pls ask a new question reagarding the same. – Raghunandan Jun 13 '14 at 10:15
  • lv.setAdapter (new CustomAdapter(Results.this,R.layout.cir_list,pd,headlines)); cir_list contain two text view and lv is listview in xml.....force close error occures. – veer1987 Jun 13 '14 at 10:15
  • can i use gujarati.ttf for loading data,how can i do any idea?example? – veer1987 Jun 13 '14 at 10:17
  • @Viren cannot answer everything in comment as a different question or you will end up asking a few more in the comment section – Raghunandan Jun 13 '14 at 10:18
  • Raghunandan ,dont know what happen every ting what u said i do but,not working for me, Force close occures.pls help.i m new to android.Thnx – veer1987 Jun 19 '15 at 11:38