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