androidgridviewsimplecursoradapterandroid-cursorsectionindexer

gridview with AlphabetIndexer


I want to implement SecionIndexer in GridView but I am running into quite a lot of trouble. I have to change code of an already existing project to add it. From my research I gather that it should be possible if using cursor.

Does anyone have a working example from which I could see how to implement it?

this is the original adapter:

package si.comtron.tronpos.adapters;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Random;

import si.comtron.tronpos.Article;
import si.comtron.tronpos.R;
import si.comtron.tronpos.content.Global;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridViewArticlesAdapter extends BaseAdapter {

    private ArrayList<Article> listOfArticles;
    private final Context context;
    private Random rand;

    // the context is needed to inflate views in getView()
    public GridViewArticlesAdapter(Context context) {
        this.context = context;
        rand = new Random();
    } 

    public void setList(ArrayList<Article> listOfArticles)
    {
        this.listOfArticles = listOfArticles;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(listOfArticles != null)
            return listOfArticles.size();
        else
            return 0;

    }

    @Override
    public Object getItem(int arg0) {
        if(listOfArticles != null)
            return listOfArticles.get(arg0);
        else
            return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

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

        ImageView imageView;
        TextView articleTitleView;
        TextView articlePriceView;
        TextView articleStockView;

        if(convertView == null)
        {
            if(Global.gridViewMode == 1){
                convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item, parent, false);
            }else if(Global.gridViewMode == 2)
            {
                convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item2, parent, false);
            }else
            {
                convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item3, parent, false);
            }

            imageView = (ImageView) convertView.findViewById(R.id.grid_item_image);
            articleTitleView = (TextView) convertView.findViewById(R.id.grid_item_text);
            articlePriceView = (TextView) convertView.findViewById(R.id.grid_item_price);
            articleStockView = (TextView) convertView.findViewById(R.id.grid_item_stock);

            convertView.setTag(new ViewHolder(imageView, articleTitleView, articlePriceView, articleStockView));

        }
        else
        {
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            imageView = viewHolder.imageView;
            articleTitleView = viewHolder.articleTitleView;
            articlePriceView = viewHolder.articlePriceView;
            articleStockView = viewHolder.articleStockView;
        }

        Article article = (Article) getItem(position);

        //imageView.setImageResource(flags[rand.nextInt(flags.length)]);
        if(article.getArticlePicture() != null && article.getArticlePicture().length() > 0)
        {
            //Bitmap image = BitmapFactory.decodeFile(article.getArticlePicture());
            //imageView.setImageBitmap(image);

            imageView.setImageURI(Uri.parse(article.getArticlePicture()));
        }
        else {
            imageView.setImageDrawable(null);
        }

        String articleTitle = article.getArticleTitle();    //predolge nazive porezemo

        if(Global.gridViewMode==1){
            if(articleTitle.length() > 13)
                articleTitle = articleTitle.substring(0, 13);

        }
        else
        {
            if(articleTitle.length() > 40)
                articleTitle = articleTitle.substring(0, 40);
        }

        articleTitleView.setText(articleTitle);
        articleTitleView.setTag(article);       //v article title view-u imamo shranjen artikel
        articlePriceView.setText(NumberFormat.getCurrencyInstance(Global.locale).format(article.getPriceWTax()));

        if(article.getArticleStock() != null)
        {
            if(article.getArticleStock() <= 0)
                articleStockView.setTextColor(Color.RED);
            else 
                articleStockView.setTextColor(Color.BLACK);

            articleStockView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button_style_orange));
            articleStockView.setText(article.getArticleStock().toString().replace('.', ','));
        }
        else
        {
            articleStockView.setBackgroundColor(android.R.drawable.btn_default);
            articleStockView.setText(null);
        }

        return convertView;
    }

    private static class ViewHolder
    {
        public final ImageView imageView;
        public final TextView articleTitleView;
        public final TextView articlePriceView; 
        public final TextView articleStockView;

        public ViewHolder(ImageView imageView, TextView articleTitleView, TextView articlePriceView, TextView articleStockView)
        {
            this.imageView = imageView;
            this.articleTitleView = articleTitleView;
            this.articlePriceView = articlePriceView;
            this.articleStockView = articleStockView;
        }
    }
}

And this are the changes I made:

package si.comtron.tronpos.adapters;

import java.text.NumberFormat;
import java.util.ArrayList;

import si.comtron.tronpos.Article;
import si.comtron.tronpos.R;
import si.comtron.tronpos.content.Global;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class AlphabetScroll extends SimpleCursorAdapter implements SectionIndexer {

    private ArrayList<Article> listOfArticles;
    private final Context context;
    private AlphabetIndexer mAlphabetIndexer;

    // the context is needed to inflate views in getView()
    public AlphabetScroll(Context context, int simpleListItem1, Cursor cursor, String[] strings, int[] is)
    {
        super(context, simpleListItem1, cursor, strings, is);

        mAlphabetIndexer = new AlphabetIndexer(cursor, cursor.getColumnIndex("ArticleTitle"), "ABCDEFGHIJKLMNOPQRTSUVWXYZ");
        mAlphabetIndexer.setCursor(cursor);//Sets a new cursor as the data set and resets the cache of indices.

        this.context = context;
    }

    public void setList(ArrayList<Article> listOfArticles)
    {
        this.listOfArticles = listOfArticles;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(listOfArticles != null)
            return listOfArticles.size();
        else
            return 0;

    }

    @Override
    public Object getItem(int arg0) {
        if(listOfArticles != null)
            return listOfArticles.get(arg0);
        else
            return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

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

        ImageView imageView;
        TextView articleTitleView;
        TextView articlePriceView;
        TextView articleStockView;

        if(convertView == null)
        {
            //TODO: naredi gridview3
            if(Global.gridViewMode == 1){
                convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item, parent, false);
            }else if(Global.gridViewMode == 2)
            {
                convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item2, parent, false);
            }else
            {
                convertView = LayoutInflater.from(context).inflate(R.layout.main_gridview_item3, parent, false);
            }

            imageView = (ImageView) convertView.findViewById(R.id.grid_item_image);
            articleTitleView = (TextView) convertView.findViewById(R.id.grid_item_text);
            articlePriceView = (TextView) convertView.findViewById(R.id.grid_item_price);
            articleStockView = (TextView) convertView.findViewById(R.id.grid_item_stock);

            convertView.setTag(new ViewHolder(imageView, articleTitleView, articlePriceView, articleStockView));
        }
        else
        {
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            imageView = viewHolder.imageView;
            articleTitleView = viewHolder.articleTitleView;
            articlePriceView = viewHolder.articlePriceView;
            articleStockView = viewHolder.articleStockView;
        }

        Article article = (Article) getItem(position);

        //imageView.setImageResource(flags[rand.nextInt(flags.length)]);
        if(article.getArticlePicture() != null && article.getArticlePicture().length() > 0)
        {
            //Bitmap image = BitmapFactory.decodeFile(article.getArticlePicture());
            //imageView.setImageBitmap(image);

            imageView.setImageURI(Uri.parse(article.getArticlePicture()));
        }
        else {
            imageView.setImageDrawable(null);
        }

        String articleTitle = article.getArticleTitle();    //predolge nazive porezemo

        if(Global.gridViewMode==1){
            if(articleTitle.length() > 13)
                articleTitle = articleTitle.substring(0, 13);

        }
        else
        {
            if(articleTitle.length() > 40)
                articleTitle = articleTitle.substring(0, 40);
        }

        articleTitleView.setText(articleTitle);
        articleTitleView.setTag(article);       //v article title view-u imamo shranjen artikel
        articlePriceView.setText(NumberFormat.getCurrencyInstance(Global.locale).format(article.getPriceWTax()));

        if(article.getArticleStock() != null)
        {
            if(article.getArticleStock() <= 0)
                articleStockView.setTextColor(Color.RED);
            else 
                articleStockView.setTextColor(Color.BLACK);

            articleStockView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.button_style_orange));
            articleStockView.setText(article.getArticleStock().toString().replace('.', ','));
        }
        else
        {
            articleStockView.setBackgroundColor(android.R.drawable.btn_default);
            articleStockView.setText(null);
        }

        return convertView;
    }

    private static class ViewHolder
    {
        public final ImageView imageView;
        public final TextView articleTitleView;
        public final TextView articlePriceView; 
        public final TextView articleStockView;

        public ViewHolder(ImageView imageView, TextView articleTitleView, TextView articlePriceView, TextView articleStockView)
        {
            this.imageView = imageView;
            this.articleTitleView = articleTitleView;
            this.articlePriceView = articlePriceView;
            this.articleStockView = articleStockView;
        }
    }

    @Override
    public Object[] getSections() {
        return mAlphabetIndexer.getSections();
    }

    @Override
    public int getPositionForSection(int section) {
        return mAlphabetIndexer.getPositionForSection(section);
    }

    @Override
    public int getSectionForPosition(int position) {
        return mAlphabetIndexer.getSectionForPosition(position);
    }
}

Solution

  • I finnaly figured it out and solved my problem:

    I implemented these 2 methods:

    private void getIndexList(ArrayList<yourObject> articles) {
        mapIndex = new LinkedHashMap<String, Integer>();
        for (int i = 0; i < yourObject.size(); i++) {
            String articleTitle = yourObject.get(i).yourObjectTitle();
            String index = yourObjectTitle.substring(0, 1);
    
            if (mapIndex.get(index) == null)
                mapIndex.put(index, i);
    
        }
    }
    
    private void displayIndex() {
        if(((LinearLayout) indexLayout).getChildCount() > 0) 
            ((LinearLayout) indexLayout).removeAllViews(); 
    
        TextView textView;
        List<String> indexList = new ArrayList<String>(mapIndex.keySet());
        for (String index : indexList) {
            textView = (TextView) activityContext.getLayoutInflater().inflate(
                    R.layout.side_index_item, null);
            textView.setText(index);
            textView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    TextView selectedIndex = (TextView) view;
                    gridViewyourObject.setSelection(mapIndex.get(selectedIndex.getText()));
                }
            });
    
            indexLayout.addView(textView);
        }
    }
    

    In the main xml added linearlayout beside the gridview:

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:layout_weight="2"
        android:background="#ffffff"
        android:orientation="vertical"
        android:id="@+id/llllllll" >
    
        <LinearLayout
            android:id="@+id/gridview_buttons_layout"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="6"
            android:orientation="horizontal">
    
            <GridView
                android:id="@+id/gridview_buttons"
               android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:horizontalSpacing="3dp"
                android:stretchMode="columnWidth"
                android:verticalSpacing="3dp" />
    
            <LinearLayout
                android:id="@+id/side_index"
                android:layout_width="35dp"
                android:layout_height="fill_parent"
                android:background="@color/payment_lightblue"
                android:gravity="center_horizontal"
                android:orientation="vertical" />
    
       </LinearLayout>
    

    And added an addition xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/side_list_item"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:padding="3dp"
    android:textSize="14sp" />
    

    And it works :) Hope it helps anyone.