滚动时网格视图视图持有者设置错误的图像

时间:2015-04-27 11:42:47

标签: android gridview baseadapter android-viewholder

问题:滚动后,在GridView单元格中更改了ImageView。

没有ViewHolder,它可以正常工作 这里食物图标变为最后一项,这是默认值。

//网格视图适配器

    public class FlagTypAdapter extends BaseAdapter {
    Context ctx;
    ArrayList<Accounts> list;
    FlagTypAdapter adapter;
    Boolean isAtm;
    Activity mActivity;
    static Integer[] imageId = {
            R.drawable.electricity_bill,
            R.drawable.food_icon_large,
            R.drawable.loan_large,
            R.drawable.movies_large,
            R.drawable.other_large,
            R.drawable.petrol_icon_large,
            R.drawable.phone_bill_large,
            R.drawable.rent_large,
            R.drawable.shopping_large,
            R.drawable.travel_large,
            R.drawable.water_bill_large,
            R.drawable.salary_large,
            R.drawable.cash_large,
            R.drawable.creditcard_large,
            R.drawable.cash_withdraw_large,
            R.drawable.newspaper_large

    };
//constructor for getting list 

    public FlagTypAdapter(ArrayList<Accounts> paramArrayList, Context paramContext, Activity activity, Boolean Atm) {
        this.list = paramArrayList;
        this.ctx = paramContext;
        this.mActivity = activity;
        this.isAtm = Atm;
    }

    public int getCount() {

        return this.list.size();
    }

    public Object getItem(int paramInt) {
        return this.list.get(paramInt);
    }

    public long getItemId(int paramInt) {
        return paramInt;
    }

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        ViewHolder viewholder;
        Accounts dto = list.get(paramInt);
        if (paramView == null) {
            paramView = LayoutInflater.from(ctx).inflate(R.layout.grid_cell, null);
            viewholder = new ViewHolder();
            viewholder.tvName = ((TextView) paramView.findViewById(R.id.grid_text));
            viewholder.imageTag = ((ImageView) paramView.findViewById(R.id.grid_image));
            viewholder.imageTag.setImageResource(0);
            paramView.setTag(viewholder);

            Log.e("creating new row" + dto.getAccountName(), "creating row");

        } else {
            Log.e("not creating new row" + dto.getAccountName(), " not creating row");
            viewholder = (ViewHolder) paramView.getTag();
        }


        viewholder.tvName.setText(dto.getAccountName());

        if (dto.getAccountName() != null) {

            switch (dto.getAccountName()) {


                case "Food":
                    Log.e("account name in food ", "" + dto.getAccountName().toString());
                    viewholder.imageTag.setBackgroundResource(imageId[1]);
                    break;
                case "Petrol":
                    viewholder.imageTag.setBackgroundResource(imageId[5]);
                    break;
                case "Phone bill":
                    viewholder.imageTag.setBackgroundResource(imageId[6]);
                    break;
                case "Electricity bill":
                    viewholder.imageTag.setBackgroundResource(imageId[0]);
                    break;
                case "Rent":
                    viewholder.imageTag.setBackgroundResource(imageId[7]);
                    break;
                case "Other":
                    viewholder.imageTag.setBackgroundResource(imageId[4]);
                    break;
                case "Shopping":
                    viewholder.imageTag.setBackgroundResource(imageId[8]);
                    break;
                case "Salary":
                    viewholder.imageTag.setBackgroundResource(imageId[11]);
                    break;
                case "Loan":
                    viewholder.imageTag.setBackgroundResource(imageId[2]);
                    break;
                case "Travelling":
                    viewholder.imageTag.setBackgroundResource(imageId[9]);
                    break;
                case "Cash":
                    viewholder.imageTag.setBackgroundResource(imageId[12]);
                    break;
                case "Credit card":
                    viewholder.imageTag.setBackgroundResource(imageId[13]);
                    break;
                case "Water bill":
                    viewholder.imageTag.setBackgroundResource(imageId[10]);
                    break;
                case "Movies":
                    viewholder.imageTag.setBackgroundResource(imageId[3]);
                    break;
                case "ATM withdraw":
                    viewholder.imageTag.setBackgroundResource(imageId[14]);
                    break;
                case "Books/News paper":
                    viewholder.imageTag.setBackgroundResource(imageId[15]);
                    break;
                default:
                    Log.e("account name in default", "" + dto.getAccountName().toString());
                    viewholder.imageTag.setImageBitmap(CommonUtility.drawImageGrid(dto.getAccountName(), mActivity));
                    break;

            }
        }


        return paramView;
    }


    private static class ViewHolder {

        TextView tvName;
        ImageView imageTag;
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个:

public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        ViewHolder viewholder;
        Accounts dto = list.get(paramInt);
        if (paramView == null) {
            paramView = LayoutInflater.from(ctx).inflate(R.layout.grid_cell, null);
            viewholder = new ViewHolder();
            viewholder.tvName = ((TextView) paramView.findViewById(R.id.grid_text));
            viewholder.imageTag = ((ImageView) paramView.findViewById(R.id.grid_image));

            paramView.setTag(viewholder);

            Log.e("creating new row" + dto.getAccountName(), "creating row");

        } else {
            Log.e("not creating new row" + dto.getAccountName(), " not creating row");
            viewholder = (ViewHolder) paramView.getTag();
        }

         viewholder.imageTag.setImageResource(0);


        viewholder.tvName.setText(dto.getAccountName());

        if (dto.getAccountName() != null) {

            switch (dto.getAccountName()) {


                case "Food":
                    Log.e("account name in food ", "" + dto.getAccountName().toString());
                    viewholder.imageTag.setBackgroundResource(imageId[1]);
                    break;
                case "Petrol":
                    viewholder.imageTag.setBackgroundResource(imageId[5]);
                    break;
                case "Phone bill":
                    viewholder.imageTag.setBackgroundResource(imageId[6]);
                    break;
                case "Electricity bill":
                    viewholder.imageTag.setBackgroundResource(imageId[0]);
                    break;
                case "Rent":
                    viewholder.imageTag.setBackgroundResource(imageId[7]);
                    break;
                case "Other":
                    viewholder.imageTag.setBackgroundResource(imageId[4]);
                    break;
                case "Shopping":
                    viewholder.imageTag.setBackgroundResource(imageId[8]);
                    break;
                case "Salary":
                    viewholder.imageTag.setBackgroundResource(imageId[11]);
                    break;
                case "Loan":
                    viewholder.imageTag.setBackgroundResource(imageId[2]);
                    break;
                case "Travelling":
                    viewholder.imageTag.setBackgroundResource(imageId[9]);
                    break;
                case "Cash":
                    viewholder.imageTag.setBackgroundResource(imageId[12]);
                    break;
                case "Credit card":
                    viewholder.imageTag.setBackgroundResource(imageId[13]);
                    break;
                case "Water bill":
                    viewholder.imageTag.setBackgroundResource(imageId[10]);
                    break;
                case "Movies":
                    viewholder.imageTag.setBackgroundResource(imageId[3]);
                    break;
                case "ATM withdraw":
                    viewholder.imageTag.setBackgroundResource(imageId[14]);
                    break;
                case "Books/News paper":
                    viewholder.imageTag.setBackgroundResource(imageId[15]);
                    break;
                default:
                    Log.e("account name in default", "" + dto.getAccountName().toString());
                    viewholder.imageTag.setImageBitmap(CommonUtility.drawImageGrid(dto.getAccountName(), mActivity));
                    break;

            }
        }


        return paramView;
    }

答案 1 :(得分:0)

一切顺利,我只是改变了将图像设置为imageTag.setimageResource()而不是imageTag.setBackgroundResource();的方法