android - custom list view with section headers -


newbie question

i have json file contains multiple categories same name.

for example

{"long_description":"test",  "short_description":"test",  "category_name":"work",  "id":1}, {"long_description":"test2",  "short_description":"test2",  "category_name":"work",  "id":2}, {"long_description":"test",  "short_description":"test",  "category_name":"home",  "id":3} 

my question how create list view sections (category name) i'm using custom adapter heres loop

try {      jsonobject responseobject = new jsonobject(response);     jsonobject itemsobject = responseobject.getjsonobject("items");       iterator<?> keysiterator = itemsobject.keys();      while(keysiterator.hasnext()) {          string keystring = (string)keysiterator.next();          jsonobject coupon = couponsobject.getjsonobject(keystring);           item item = new item();          item.setlongdescription(item.getstring("long_description"));          itemlist.add(item);      }  } catch (exception e) {     e.printstacktrace(); }  adapter.notifydatasetchanged(); 

use below links examples of listview header.

http://applidium.github.io/headerlistview/

http://javatechig.com/android/listview-with-section-header-in-android

http://sunil-android.blogspot.in/2013/08/section-header-listview-in-android.html

in given below code update data header , list item example simplest

<?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:background="#2f2f2f"     android:gravity="center_vertical" >      <textview         android:id="@+id/textseparator"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:padding="8dp"         android:text=""         android:textallcaps="true"         android:textappearance="?android:attr/textappearancelarge"         android:textcolor="#fff"         android:textstyle="bold"         android:visibility="visible" />  </linearlayout> 

customadapter.java

import java.util.arraylist; import java.util.treeset; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview;  class customadapter extends baseadapter {      private static final int type_item = 0;     private static final int type_separator = 1;      private arraylist<string> mdata = new arraylist<string>();     private treeset<integer> sectionheader = new treeset<integer>();      private layoutinflater minflater;      public customadapter(context context) {         minflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);     }      public void additem(final string item) {         mdata.add(item);         notifydatasetchanged();     }      public void addsectionheaderitem(final string item) {         mdata.add(item);         sectionheader.add(mdata.size() - 1);         notifydatasetchanged();     }      @override     public int getitemviewtype(int position) {         return sectionheader.contains(position) ? type_separator : type_item;     }      @override     public int getviewtypecount() {         return 2;     }      @override     public int getcount() {         return mdata.size();     }      @override     public string getitem(int position) {         return mdata.get(position);     }      @override     public long getitemid(int position) {         return position;     }      public view getview(int position, view convertview, viewgroup parent) {         viewholder holder = null;         int rowtype = getitemviewtype(position);          if (convertview == null) {             holder = new viewholder();             switch (rowtype) {             case type_item:                 convertview = minflater.inflate(r.layout.snippet_item1, null);                 holder.textview = (textview) convertview.findviewbyid(r.id.text);                 break;             case type_separator:                 convertview = minflater.inflate(r.layout.snippet_item2, null);                 holder.textview = (textview) convertview.findviewbyid(r.id.textseparator);                 break;             }             convertview.settag(holder);         } else {             holder = (viewholder) convertview.gettag();         }         holder.textview.settext(mdata.get(position));          return convertview;     }      public static class viewholder {         public textview textview;     }  } 

sectionlistview.java

import android.app.listactivity; import android.os.bundle;  public class sectionlistview extends listactivity {      private customadapter madapter;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         madapter = new customadapter(this);         (int = 1; < 30; i++) {             madapter.additem("row item #" + i);             if (i % 4 == 0) {                 madapter.addsectionheaderitem("section #" + i);             }         }         setlistadapter(madapter);     }  } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -