Android Categorized listview with heading

12,848

Hey i was also having the same problem long before. After much googling i found this tutorial http://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/ . It worked for me.

Share:
12,848
dev_android
Author by

dev_android

Updated on June 04, 2022

Comments

  • dev_android
    dev_android almost 2 years

    I have to display data from the database in a listview. I have fetch all the data in group by categoty and displayed in a Listview. I have used the following code.

    private ListView infos;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
      super.onCreate(savedInstanceState);
      .........
      .........
      infos = new ListView(this);
      model = infoDataHelper.getCursor(addType);
      adapter = new InfoAdapter(model);
      infos.setAdapter(adapter);
      .........
      .........
    }
    
    
    class InfoAdapter extends CursorAdapter {
    
      public InfoAdapter(Cursor c) {
        super(getParent(), c);
        // TODO Auto-generated constructor stub
      }
    
      @Override
       public void bindView(View row, Context ctxt, Cursor c) {
        InfoHolder holder = (InfoHolder) row.getTag();
        holder.populateTable(c, infoDataHelper);
       }
    
       @Override
       public View newView(Context ctxt, Cursor c, ViewGroup parent) {
    
        LayoutInflater inflater = getLayoutInflater();
    
        View row;
            row = inflater.inflate(R.layout.inforow, parent, false);
    
        InfoHolder holder = new InfoHolder(row);
        row.setTag(holder);
        table_id++;
        return (row);
       }
    
    }
    

    Now I want to add heading categoty name on result set so that it looks like that-

    Categoty Fruit
      Apple
      Mango
      Grape
    
    Category Flower
     Rose
     Lotus
     Jesmine
    

    and so on..

    How can I make it? Does the addHeaderView work for it? If it is, How I can add it?

  • dev_android
    dev_android about 13 years
    In this tutorial it is used AlphabetIndexer class to get the index. but for me it not alphabetic order. it is categorical display. How to replace AlphabetIndexer?
  • Kartik Domadiya
    Kartik Domadiya about 13 years
    Hey i got something related to your problem. stackoverflow.com/questions/1966802/android-listview-headers