当我在Listview项目android

时间:2016-05-04 05:00:02

标签: android listview

public class CustomList extends ArrayAdapter<String> {
    private String[] names;

    private Integer[] imageid;
    private Activity context;
    LayoutInflater inflater;
    List<String> categories;
    boolean firstimagevisible = true;
    boolean secondimagevisible = false;

    public CustomList(Activity context, String[] names, Integer[] imageid) {
        super(context, R.layout.list_layout, names);
        this.context = context;
        this.names = names;

        this.imageid = imageid;

        inflater = LayoutInflater.from(context);

    }


    private class ViewHolder {
        TextView textView;
        ImageView imageView;
        TextView price;
        Button previousimage;
        Button nextimage;
        Spinner sppiner;

    }



    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }


    @Override
    public int getCount() {
        return super.getCount();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public String getItem(int position) {
        return super.getItem(position);
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.list_layout, null);
            //
            holder.textView = (TextView) view.findViewById(R.id.editText);
            holder.imageView = (ImageView) view.findViewById(R.id.list_avatar);
            holder.price = (TextView) view.findViewById(R.id.price);
            holder.previousimage = (Button) view.findViewById(R.id.previousimage);
            holder.nextimage = (Button) view.findViewById(R.id.nextimage);
            holder.sppiner = (Spinner) view.findViewById(R.id.sppiner);


            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.textView.setText(names[position]);
        holder.imageView.setImageResource(imageid[position]);
        categories = new ArrayList<String>();
        for (int i = 1; i <= 20; i++) {

            categories.add(String.valueOf(i));
        }
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_item, categories);
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        holder.sppiner.setAdapter(dataAdapter);


        holder.previousimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.imageView.setImageResource(R.drawable.pro1);
                holder.previousimage.setVisibility(View.GONE);
                holder.nextimage.setVisibility(View.VISIBLE);

                secondimagevisible = false;
                firstimagevisible = true;
                holder.price.setText(String.valueOf(1 * 22));
            }
        });

        holder.nextimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.imageView.setImageResource(R.drawable.pro2);
                holder.nextimage.setVisibility(View.GONE);
                holder.previousimage.setVisibility(View.VISIBLE);
                secondimagevisible = true;
                firstimagevisible = false;
                holder.price.setText(String.valueOf(1 * 16));
            }
        });

        holder.sppiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String item = adapterView.getItemAtPosition(i).toString();

                int Quantity = Integer.parseInt(item);
                holder.previousimage.setVisibility(View.GONE);
                if (firstimagevisible == false && secondimagevisible == true) {
                    holder.price.setText(String.valueOf(Quantity * 16));
                } else {
                    holder.price.setText(String.valueOf(Quantity * 22));
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        return view;
    }
}

这是我的代码。我在Listview中显示10个项目。每个在imageview上都有文字和图像。我已经推出了下一个和之前的图像和图像更改,就像我们设置第一个项目pic1和第二个项目我点击下一个按钮和图像更改为pic2现在我已向下滚动到第10个项目现在再次返回到第一个项目和第二个项目它显示pic一个和下一个和上一个按钮也消失,而它应该保存第一项pic1和第二项pic2请告诉我我在哪里做错了

1 个答案:

答案 0 :(得分:2)

由于缓存问题正在发生,所以我建议在将图像分配到ImageView对象之前使用以下代码

            holder.imageView.setDrawingCacheEnabled(false);
            holder.imageView.destroyDrawingCache();
            holder.imageView.setImageDrawable(null);
            holder.imageView.setImageResource(imageid[position]);