gridIndexer的gridview

时间:2014-10-21 08:33:17

标签: android gridview simplecursoradapter android-cursor sectionindexer

我想在GridView中实现SecionIndexer但是我遇到了很多麻烦。我必须更改现有项目的代码才能添加它。从我的研究中我得知,如果使用光标应该是可能的。

有没有人有一个可以看到如何实现它的工作示例?

这是原始适配器:

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;
        }
    }
}

这是我所做的改变:

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);
    }
}

2 个答案:

答案 0 :(得分:1)

我终于弄明白并解决了我的问题:

我实现了这两种方法:

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);
    }
}

在gridview中的主xml中添加了linearlayout:

<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>

并添加了一个额外的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" />

它有效:)希望它可以帮助任何人。

答案 1 :(得分:0)

您应该在newView(Context context, Cursor cursor, ViewGroup parent)中覆盖bindView(View view, Context context, Cursor cursor)getView()而不是CursorAdapter

您也不必担心自己回收视图,CursorAdapter会为您做到这一点。以下是如何使用这些方法的示例:

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView cityName = (TextView) view.findViewById(R.id.tvCityName);
    cityName.setText(cursor.getString(cursor.getColumnIndex(columnName)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.list_item_checkbox, parent, false);
    return v;
}