1

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

2 に答える 2

1

私は最終的にそれを理解し、私の問題を解決しました:

これらの2つの方法を実装しました:

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

メイン 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" />

そしてそれはうまくいきます:)それが誰かに役立つことを願っています.

于 2014-10-27T07:07:57.557 に答える
0

の代わりにandnewView(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;
}
于 2014-10-21T09:01:06.657 に答える