Listview适配器弄乱了图标

时间:2018-09-01 14:53:24

标签: android listview

我的ListView适配器有问题。我正在显示来自Firebase的带有自定义图标的对象,具体取决于该对象的特定值。显示多个项目后,适配器开始显示错误的图标并将其混合。 这是我的函数:

public class ListAdapterExpenses extends ArrayAdapter<Expenses>{

private static final String TAG = "ListAdapterExpenses";

private LayoutInflater mInflater;
private Context mContext;
private List<Expenses> mList;
private int categoryID;

public ListAdapterExpenses(@NonNull Context context, ArrayList<Expenses> arrayList) {
    super(context, 0, arrayList);

    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mContext = context;

    mList = arrayList;
}



private static class ViewHolder{
    TextView name, date, value, location, euro;
    ImageView circle_color;
    ImageView category_icon;
    ProgressBar progressBar;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    final ViewHolder holder;

    if(convertView == null){

        convertView = mInflater.inflate(R.layout.expenses_list, parent, false);
        holder = new ViewHolder();

        holder.name = (TextView) convertView.findViewById(R.id.tv_name_exList);
        holder.date = (TextView) convertView.findViewById(R.id.tv_date_exList);
        holder.value = (TextView) convertView.findViewById(R.id.tv_value_exList);
        holder.circle_color = (ImageView) convertView.findViewById(R.id.bg_circle_exList);
        holder.category_icon = (ImageView) convertView.findViewById(R.id.category_icon_exList);
        holder.euro= (TextView) convertView.findViewById(R.id.euro);
        holder.circle_color.setImageResource(R.drawable.bg_circle_silvers);
        holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressbar);

        categoryID = (int) getItem(position).getCategory_id();



        switch (categoryID){

            case 0:
                holder.category_icon.setImageResource(R.drawable.ic_computer_red_24dp);
                break;
            case 1:
                holder.category_icon.setImageResource(R.drawable.ic_attach_gebuehren_red_24dp);
                break;
            case 2:
                holder.category_icon.setImageResource(R.drawable.ic_healing_red_24dp);
                break;
            case 3:
                holder.category_icon.setImageResource(R.drawable.ic_handy_internet_red_24dp);
                break;
            case 4:
                holder.category_icon.setImageResource(R.drawable.ic_haushalt_red_24dp);
                break;
            case 5:
                holder.category_icon.setImageResource(R.drawable.ic_children_red_24dp);
                break;
            case 6:
                holder.category_icon.setImageResource(R.drawable.ic_business_suit_red_24dp);
                break;
            case 7:
                holder.category_icon.setImageResource(R.drawable.ic_cosmetics_red_12dp);
                break;
            case 8:
                holder.category_icon.setImageResource(R.drawable.ic_grocery_red_24dp);
                break;
            case 9:
                holder.category_icon.setImageResource(R.drawable.ic_restaurant);
                break;
            case 10:
                holder.category_icon.setImageResource(R.drawable.ic_vacation);
                break;
            case 11:
                holder.category_icon.setImageResource(R.drawable.ic_import_export_red_24dp);
                break;
            case 12:
                holder.category_icon.setImageResource(R.drawable.ic_public_transport);
                break;
            case 13:
                holder.category_icon.setImageResource(R.drawable.ic_additional_costs_red_24dp);
                break;
            case 14:
                holder.category_icon.setImageResource(R.drawable.ic_sport_red_24dp);
                break;
            case 15:
                holder.category_icon.setImageResource(R.drawable.ic_entertainment);
                break;
            case 16:
                holder.category_icon.setImageResource(R.drawable.ic_more);
                break;

            default:
                holder.category_icon.setImageResource(R.drawable.ic_attach_file);

        }

        convertView.setTag(holder);

    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    if(mList.get(position).getDate().equals("0")){

        holder.date.setVisibility(View.GONE);

        holder.name.setGravity(Gravity.CENTER_HORIZONTAL);

        holder.progressBar.setVisibility(View.VISIBLE);

    }

    //set Expense name

    holder.name.setText(getItem(position).getName());

    //set Expense date
    holder.date.setText(getItem(position).getDate());

    holder.value.setText("€ -" + String.valueOf(getItem(position).getValue()));
    //set Expense category

    return convertView;
}

this is what it looks like when it messes up

您知道一种更好的方法吗?

1 个答案:

答案 0 :(得分:0)

我看到的问题是,类别和开关的获取应该在[[0, 1, 0, 1, 1, 1, 0], [0, 1, 1, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 1, 1], [0, 1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0], [0, 1, 1, 1, 0, 0, 0], [0, 1, 1, 1, 0, 1, 1], [0, 1, 1, 1, 0, 1, 1], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 1], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 1]] 之外。

当converView不为null时,您不会重新评估类别,也不会重新设置类别图标。

它应该像这样:

if(convertView == null)