在适配器中刷新ListView行,其中包含微调器

时间:2013-04-26 09:22:18

标签: android listview refresh spinner

我有一个从基础扩展的自定义适配器,我在每一行内都有一个微调器(与每个产品的数量相关的东西 - > listview中的项目)。 当用户选择微调器的不同值时,我必须刷新显示总价格的TextView(该行的TextView - productQuantity * productPrice)。问题是,在spinner.setOnItemClickListener中调用notifyDataSetChanged完全混淆了我的listview,大大降低了帧数。 是否有任何(其他可能)解决方案来解决这个问题..(我在寻找适配器getView中的一行刷新,但找不到合适的解决方案)。

适配器

public class CartAdapter extends BaseAdapter {

private List<ProductDetailedHolder> itemsList = new ArrayList<ProductDetailedHolder>();
private ProductDetailedHolder item = null;
private Activity activity;
private final String[] quantityValues = new String[] { "1", "2", "3", "4",
        "5", "6", "7", "8", "9", "10" };
private DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.bulina)
        .displayer(new RoundedBitmapDisplayer(15))
        .bitmapConfig(Bitmap.Config.RGB_565)
        .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2).build();

private ViewHolder holder = null;
private ImageLoader imageLoader;

public CartAdapter(List<ProductDetailedHolder> array, Activity activity,
        ImageLoader imageLoader) {
    this.activity = activity;
    this.itemsList = array;
    this.imageLoader = imageLoader;

}

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

@Override
public Object getItem(int position) {
    return itemsList.get(position);
}

@Override
public long getItemId(int position) {
    return itemsList.get(position).getProductId();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    item = itemsList.get(position);

    if (convertView == null) {
        convertView = activity.getLayoutInflater().inflate(
                R.layout.row_cart_item, null);

        holder = new ViewHolder(convertView);

        ArrayAdapter<String> quantityAdapter = new ArrayAdapter<String>(
                activity, android.R.layout.simple_spinner_item,
                quantityValues);

        quantityAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        holder.spin_row_cart_quantity.setAdapter(quantityAdapter);

        holder.spin_row_cart_quantity
                .setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0,
                            View arg1, int arg2, long arg3) {

                        int getPosition = (Integer) arg0.getTag();

                        Log.d(ServiceManager.TagOSDShop, "getPosition: "
                                + getPosition);

                        itemsList.get(getPosition).setProductQuantity(
                                Integer.valueOf(
                                        arg0.getSelectedItem().toString())
                                        .intValue());

// here I need to refresh the row with holder.tv_row_cart_price.setText ( productQuantity * productPrice), but I can't use notifyDataSetChanged on all adapter, because this would refresh the entire list (and spinner.setOnItemSelected fires when the list is initialised, causing my listview to run very slow and ugly.



                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> arg0) {
                        // do nothing
                    }
                });

        holder.ib_row_cart_delete
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        int getPosition = (Integer) v.getTag();
                        itemsList.remove(getPosition);

                    }
                });

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

    holder.tv_row_cart_title.setText(item.getProductName());

    holder.tv_row_cart_color.setText(Html
            .fromHtml("<font color=\"#999999\"> Color </font> <br />"
                    + item.getProductColor()));
    holder.tv_row_cart_size.setText(Html
            .fromHtml("<font color=\"#999999\"> Size </font> <br />"
                    + item.getProductSize()));
    holder.tv_row_cart_details.setText(item.getProductDescription());
    holder.tv_row_cart_price.setText(String.valueOf(item.getProductPrice()
            * item.getProductQuantity()));

    holder.ib_row_cart_delete.setTag(position);

    holder.spin_row_cart_quantity.setSelection(findSelection(item));

    holder.spin_row_cart_quantity.setTag(position);

    imageLoader.displayImage(item.getProductImageUrl(),
            holder.iv_row_cart_content, options);

    return convertView;
}

private int findSelection(ProductDetailedHolder item) {

    for (int i = 0; i < quantityValues.length; i++) {
        if (Integer.valueOf(quantityValues[i]).intValue() == item
                .getProductQuantity()) {
            return i;
        }
    }

    Log.e(ServiceManager.TagOSDShop,
            "findSelection returned invalid position!!!");
    return -1;
}

static class ViewHolder {

    public TextView tv_row_cart_title;
    public TextView tv_row_cart_color;
    public TextView tv_row_cart_size;
    public TextView tv_row_cart_price;
    public TextView tv_row_cart_details;
    public ImageButton ib_row_cart_delete;
    public ImageView iv_row_cart_content;
    public Spinner spin_row_cart_quantity;

    public ViewHolder(View convertView) {

        tv_row_cart_title = (TextView) convertView
                .findViewById(R.id.tv_row_cart_title);
        tv_row_cart_color = (TextView) convertView
                .findViewById(R.id.tv_row_cart_color);
        tv_row_cart_size = (TextView) convertView
                .findViewById(R.id.tv_row_cart_size);
        tv_row_cart_price = (TextView) convertView
                .findViewById(R.id.tv_row_cart_price);
        tv_row_cart_details = (TextView) convertView
                .findViewById(R.id.tv_row_cart_details);
        ib_row_cart_delete = (ImageButton) convertView
                .findViewById(R.id.ib_row_cart_delete);
        iv_row_cart_content = (ImageView) convertView
                .findViewById(R.id.iv_row_cart_content);
        spin_row_cart_quantity = (Spinner) convertView
                .findViewById(R.id.spin_row_cart_quantity);

    }

}
顺便说一下,我的微调器有一些常数值,比如1,2,... 20。

2 个答案:

答案 0 :(得分:0)

请参阅此link
它可能对您有所帮助

答案 1 :(得分:0)

您需要在适配器中创建一个侦听器,然后在您的活动中实现它,然后从您的活动中调用listview.getChildAt(position)方法并根据需要刷新视图。

您可以使用接口实现侦听器,并且您需要实现接口。