我怎样才能让我的ArrayAdapter按照ViewHolder格局? [英] How can I make my ArrayAdapter follow the ViewHolder pattern?

查看:132
本文介绍了我怎样才能让我的ArrayAdapter按照ViewHolder格局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的ArrayAdapter。我想使这更有效地遵循ViewHolder方式:

<一个href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

但我不知道如何做到这一点。

更新:ViewHolder模式

 私有类QuoteAdapter扩展ArrayAdapter&LT;报价&GT; {

    私人的ArrayList&LT;报价&GT;项目;
    //用来保持选定的位置中的ListView
    私人诠释selectedPos = -1; //为不选择初值

    公共QuoteAdapter(上下文的背景下,INT textViewResourceId,ArrayList的&LT;报价&GT;项目){
        超(背景下,textViewResourceId,项目);
        this.items =项目;
    }

    公共无效setSelectedPosition(INT POS){
        selectedPos = POS机;
        //告知这一变化的看法
        notifyDataSetChanged();
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        视图V = convertView;
        ViewHolder持有人; //引用子视图供以后行动

        如果(V == NULL){
            LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            V = vi.inflate(R.layout.mainrow,NULL);

            //缓存视场到支架
            持有人=新ViewHolder();
            holder.nameText =(TextView中)v.findViewById(R.id.nameText);
            holder.priceText =(TextView中)v.findViewById(R.id.priceText);
            holder.changeText =(TextView中)v.findViewById(R.id.changeText);

            //持有这样的观点,供以后查询相关联
            v.setTag(保持器);
        }
        其他 {
            //视图已经存在,从视图中获得持有人的实例
            支架=(ViewHolder)v.getTag();
        }

        根据选定的状态//改变该行的颜色
        如果(selectedPos ==位置){
            v.setBackgroundResource(R.drawable.stocks_selected_gradient);
            holder.nameText.setTextColor(Color.WHITE);
            holder.priceText.setTextColor(Color.WHITE);
            holder.changeText.setTextColor(Color.WHITE);
        } 其他 {
            v.setBackgroundResource(R.drawable.stocks_gradient);
            holder.nameText.setTextAppearance(getApplicationContext(),R.style.BlueText);
            holder.priceText.setTextAppearance(getApplicationContext(),R.style.BlueText);
            holder.changeText.setTextAppearance(getApplicationContext(),R.style.BlueText);
        }

        报价Q = items.get(位置);
        如果(Q!= NULL){
            如果(holder.nameText!= NULL){
                holder.nameText.setText(q.getSymbol());
            }
            如果(holder.priceText!= NULL){
                holder.priceText.setText(q.getLastTradePriceOnly());
            }
            如果(holder.changeText!= NULL){
                尝试 {
                    浮floatedChange = Float.valueOf(q.getChange());
                    如果(floatedChange℃,){
                        如果(selectedPos!=位置)
                            holder.changeText.setTextAppearance(getApplicationContext(),R.style.RedText); // 红
                    } 其他 {
                        如果(selectedPos!=位置)
                            holder.changeText.setTextAppearance(getApplicationContext(),R.style.GreenText); // 绿色
                    }
                }赶上(NumberFormatException异常E){
                    的System.out.println(非数字);
                }赶上(NullPointerException异常E){
                    的System.out.println(空号);
                }
                holder.changeText.setText(q.getChange()+(+ q.getPercentChange()+));
            }
        }
        返回伏;
    }
}
 

解决方案

该ViewHolder基本上是你有一个观点联系起来它创建时的静态类的实例,缓存的子视图您正在寻找在运行时。如果视图已经存在,检索持有人实例,并使用 findViewById ,而不是调用它的领域。

在你的情况:

  @覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    视图V = convertView;
    ViewHolder持有人; //引用子视图供以后行动

    如果(V == NULL){
        LayoutInflater VI =
            (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        V = vi.inflate(R.layout.mainrow,NULL);
        //缓存视场到支架
        持有人=新ViewHolder();
        holder.nameText =(TextView中)v.findViewById(R.id.nameText);
        holder.priceText =(TextView中)v.findViewById(R.id.priceText);
        holder.changeText =(TextView中)v.findViewById(R.id.changeText);
        //持有这样的观点,供以后查询相关联
        v.setTag(保持器);
    }
    其他 {
        //视图已经存在,从视图中获得持有人的实例
        支架=(ViewHolder)v.getTag();
    }
    //与findViewById没有局部变量在这里

    //使用holder.nameText你在哪里
    //使用前局部变量nameText

    返回伏;
}

//别的地方在你的类定义
静态类ViewHolder {
    TextView的nameText;
    TextView的priceText;
    TextView的changeText;
}
 

警告:我没有尝试进行编译,所以采取与一粒盐

Here is my ArrayAdapter. I would like to make this more efficient by following the ViewHolder pattern:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

but am not sure how to accomplish this.

UPDATE: ViewHolder Pattern

private class QuoteAdapter extends ArrayAdapter<Quote> {

    private ArrayList<Quote> items;
    // used to keep selected position in ListView
    private int selectedPos = -1; // init value for not-selected

    public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    public void setSelectedPosition(int pos) {
        selectedPos = pos;
        // inform the view of this change
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ViewHolder holder; // to reference the child views for later actions

        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.mainrow, null);

            // cache view fields into the holder
            holder = new ViewHolder();
            holder.nameText = (TextView) v.findViewById(R.id.nameText);
            holder.priceText = (TextView) v.findViewById(R.id.priceText);
            holder.changeText = (TextView) v.findViewById(R.id.changeText);

            // associate the holder with the view for later lookup
            v.setTag(holder);
        }
        else {
            // view already exists, get the holder instance from the view
            holder = (ViewHolder)v.getTag();
        }

        // change the row color based on selected state
        if (selectedPos == position) {
            v.setBackgroundResource(R.drawable.stocks_selected_gradient);
            holder.nameText.setTextColor(Color.WHITE);
            holder.priceText.setTextColor(Color.WHITE);
            holder.changeText.setTextColor(Color.WHITE);
        } else {
            v.setBackgroundResource(R.drawable.stocks_gradient);
            holder.nameText.setTextAppearance(getApplicationContext(), R.style.BlueText);
            holder.priceText.setTextAppearance(getApplicationContext(), R.style.BlueText);
            holder.changeText.setTextAppearance(getApplicationContext(), R.style.BlueText);
        }

        Quote q = items.get(position);
        if (q != null) {
            if (holder.nameText != null) {
                holder.nameText.setText(q.getSymbol());
            }
            if (holder.priceText != null) {
                holder.priceText.setText(q.getLastTradePriceOnly());
            }
            if (holder.changeText != null) {
                try {
                    float floatedChange = Float.valueOf(q.getChange());
                    if (floatedChange < 0) {
                        if (selectedPos != position)
                            holder.changeText.setTextAppearance(getApplicationContext(), R.style.RedText); // red
                    } else {
                        if (selectedPos != position)
                            holder.changeText.setTextAppearance(getApplicationContext(), R.style.GreenText); // green
                    }
                } catch (NumberFormatException e) {
                    System.out.println("not a number");
                } catch (NullPointerException e) {
                    System.out.println("null number");
                }
                holder.changeText.setText(q.getChange() + " (" + q.getPercentChange() + ")");
            }
        }
        return v;
    }
}

解决方案

The ViewHolder is basically a static class instance that you associate with a view when it's created, caching the child views you're looking up at runtime. If the view already exists, retrieve the holder instance and use its fields instead of calling findViewById.

In your case:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder; // to reference the child views for later actions

    if (v == null) {
        LayoutInflater vi = 
            (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.mainrow, null);
        // cache view fields into the holder
        holder = new ViewHolder();
        holder.nameText = (TextView) v.findViewById(R.id.nameText);
        holder.priceText = (TextView) v.findViewById(R.id.priceText);
        holder.changeText = (TextView) v.findViewById(R.id.changeText);
        // associate the holder with the view for later lookup
        v.setTag(holder);
    }
    else {
        // view already exists, get the holder instance from the view
        holder = (ViewHolder) v.getTag();
    }
    // no local variables with findViewById here

    // use holder.nameText where you were 
    // using the local variable nameText before

    return v;
}

// somewhere else in your class definition
static class ViewHolder {
    TextView nameText;
    TextView priceText;
    TextView changeText;
}

caveat: I didn't try to compile this, so take with a grain of salt.

这篇关于我怎样才能让我的ArrayAdapter按照ViewHolder格局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆