1

In my application i am parsing data from XML.And mapping them into a hashmap and finally setting into an "ArrayList".

Below is my code:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
      TaplistingParser parser = new TaplistingParser();
      String xml= parser.getXmlFromUrl(URL);
      Document doc=parser.getDomElement(xml);        
//      System.out.println("sssss="+doc);
      NodeList nl=doc.getElementsByTagName("article");
      final String[] url= new String[nl.getLength()];
      for(int i=0; i < nl.getLength(); i++ )
      {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        map.put("Title", parser.getValue(e, "title"));       -------->
        map.put("Date", parser.getValue(e, "create_date"));  -------->Here is the array

        url[i]=parser.getValue(e, "url");
//          map.put("URL", parser.getValue(e, "url"));
        menuItems.add(map);
//          System.out.println("items="+menuItems);
      }  
//      System.out.println("items="+menuItems);
      ListView l1= (ListView)findViewById(R.id.list);
      ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.homelistrow,
            new String[] {"Title"}, new int[] 
                    {
                    R.id.name_label});              
            l1.setAdapter(adapter);        

Now in the above code i have Date and Title.

I have to display the list like this:

2012-11-09
qqqqqqqqqqqqqqqq
------------------
2012-11-09
ddddddddddddddd
-----------------

How can i show the 2 array in 2 textview in a list..I am new to this please help me.Thanks in advance.

prakash .k
  • 635
  • 2
  • 12
  • 24
  • 1
    check http://www.codercorp.com/blog/android/android-two-line-listview-with-custom-data.html – Nermeen Nov 09 '12 at 10:12
  • 1
    for what you need 2 array? just create some class with two attrivutes - title, date, then create List, parse data from XML to this object and create subclass of some adapter with List. – Simon Dorociak Nov 09 '12 at 10:13
  • Thanks deceiver..But in my above code..can you say what i have to do accomplish this.. – prakash .k Nov 09 '12 at 10:20
  • You have to have a custom adapter that extends base adapter.....then use that custom adapter to your listview..it should not be a simplelistadapter...+1 to @Nunu – Kunal Shah Nov 09 '12 at 10:25
  • consider creating a `DataItem` class to hold related pieces of data together, for each row in list. Then use an array/Collection of `DataItem` objects with the `ListView`'s adapter. – S.D. Nov 09 '12 at 10:39
  • i think this link help full for you http://stackoverflow.com/questions/11106418/how-to-set-adapter-in-case-of-multiple-textviews-per-listview – Sidhhu Patel Aug 31 '13 at 19:18

1 Answers1

0

You should implement your own Adapter which will extend ArrayAdapter.

Then in it, your getView() method should look like this

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;

    if (row == null)
    {
        // ROW INFLATION
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.buddy_list_item, parent, false);
    }

    // Get item
    Buddy buddy = getItem(position);
    buddy.refresh();

    buddyName = (TextView) row.findViewById(R.id.buddy_name);
    buddyName.setText(buddy.toString()); //set the first line somehow

    buddyStatus = (TextView) row.findViewById(R.id.buddy_mood);
    buddyStatus.setText(buddy.getMood());  //set the second line

    return row;
}

Then you need an buddy_list_item.XML file which will containt the two TextViews - buddy_name and buddy_mood. You can use simple_list_2 i think, but I do it this way.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckedTextView
        android:id="@+id/buddy_name"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:checkMark="?android:attr/textCheckMark"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:text="@string/buddy_name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/buddy_mood"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/empty_string"
        android:layout_marginLeft="-350dp"
        android:layout_marginTop="16dp"
        android:gravity="center_vertical|bottom"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

Just figure out how to put this to good use now :)

EDIT:

P.S. You know you can give the adapter anything it needs right? so you can even do ths

public YourArrayAdapter(Context context, int textViewResourceId, HashMap yourMap)
{
    super(context, textViewResourceId, objects);
    this.context = context;
    this.buddies = objects;
    //do something with the map or delegate demapping to the adapter
}

and then later in getView() you actually do what you need to get the data out of the map instead of getting the data out of the map and making an ArrayAdapter out of that.

Shark
  • 6,513
  • 3
  • 28
  • 50