ListView不同的TextViews

时间:2017-04-16 12:13:14

标签: android listview

package com.example.sander.app;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Sander on 15-4-2017.
 */

public class ListAdapter extends ArrayAdapter<Points> {
    private Context context;
    private boolean useList = true;
    public ListAdapter(Context context, ArrayList items){
        super(context, android.R.layout.simple_list_item_1, items);
        this.context = context;
    }
    public class ViewHolder{
        TextView titleText;
        TextView numberText;
    }
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        Points item = (Points)getItem(position);
        View viewToUse = null;
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            if(useList){
                viewToUse = mInflater.inflate(R.layout.list_item, null);
            }
            holder = new ViewHolder();
            holder.titleText = (TextView)viewToUse.findViewById(R.id.hoods);

            if(item.getPoints() >= 4.0){
                holder.numberText = (TextView)viewToUse.findViewById(R.id.number_good);
            }
            else if(item.getPoints() < 4.0 && item.getPoints() > 3.0){
                holder.numberText = (TextView)viewToUse.findViewById(R.id.number_average);
            }
            else if(item.getPoints() < 3.0){
                holder.numberText = (TextView)viewToUse.findViewById(R.id.number_view);
            }
            viewToUse.setTag(holder);
        } else {
            viewToUse = convertView;
            holder = (ViewHolder) viewToUse.getTag();
        }
        holder.titleText.setText(item.getHoodName() + " (Wijk " + item.getHoodId() + ")");
        holder.numberText.setText(String.valueOf(item.getPoints()) + "/5.0");
        return viewToUse;
    }
}

我试图根据他们的评级为这些项目设置颜色,但不知何故他们不匹配。除此之外,当我订购它们时它们不再工作了。我确实使用自定义ArrayList所以项目是相互连接。例如:

如果评级低于3,则颜色应为红色,如果评级高于4,则应为绿色。如果介于3和4之间,则应为黄色。

1 个答案:

答案 0 :(得分:0)

您不应该更改持有者视图引用,ViewHolder模式的重点是避免对findViewById进行过多的运行时调用。

试试这个:

public class ListAdapter extends ArrayAdapter<Points> {
    private Context context;
    private boolean useList = true;
    public ListAdapter(Context context, ArrayList items){
        super(context, android.R.layout.simple_list_item_1, items);
        this.context = context;
    }
    public class ViewHolder{
        TextView titleText;
        TextView numberTextGood;
        TextView numberTextAverage;
        TextView numberTextBad;
    }
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        Points item = (Points)getItem(position);
        View viewToUse = null;
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            viewToUse = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.titleText = (TextView)viewToUse.findViewById(R.id.hoods);
            holder.numberTextGood = (TextView) viewToUse.findViewById(R.id.number_good);
            holder.numberTextAverage = (TextView) viewToUse.findViewById(R.id.number_average);
            holder.numberTextBad = (TextView) viewToUse.findViewById(R.id.number_bad);
            viewToUse.setTag(holder);
        } else {
            viewToUse = convertView;
            holder = (ViewHolder) viewToUse.getTag();
        }
        holder.titleText.setText(item.getHoodName() + " (Wijk " + item.getHoodId() + ")");

        // first clear all text-views
        holder.numberTextGood.setText("");
        holder.numberTextAverage.setText("");
        holder.numberTextBad.setText("");

        // now set the value in the proper one
        if(item.getPoints() >= 4.0){
            holder.numberTextGood.setText(String.valueOf(item.getPoints()) + "/5.0");
        } else if(item.getPoints() < 4.0 && item.getPoints() > 3.0){
            holder.numberTextAverage.setText(String.valueOf(item.getPoints()) + "/5.0");
        } else if(item.getPoints() < 3.0){
            holder.numberTextBad.setText(String.valueOf(item.getPoints()) + "/5.0");
        }
        return viewToUse;
    }
}