gpt4 book ai didi

android - 带有 AlphabetIndexer 的 gridview

转载 作者:行者123 更新时间:2023-11-30 02:36:12 26 4
gpt4 key购买 nike

我想在 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);
}
}

最佳答案

我终于想通了并解决了我的问题:

我实现了这两种方法:

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 中,在 gridview 旁边添加了 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" />

它有效 :) 希望它对任何人都有帮助。

关于android - 带有 AlphabetIndexer 的 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26482234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com