滚动ListView会填充虚拟值

时间:2013-03-25 10:39:44

标签: android listview scroll

我的列表视图包含一个图像和一些textview;有两个textiview包含价格和特价,如果特价是0比仅设置价格textview和特价让它null,但特价是> 0比设定价格值以及特价,fisrt时间一切顺利,但当我滚动列表视图时,空白textview设置为dumyy值enter code here

这是我的getView方法代码。

@Override
public     View     getView(final int position, View convertView, ViewGroup parent){
//public View getView(int position, View convertView, ViewGroup parent) {
    /*View vi = convertView;
    if (convertView == null)
    {
        vi = inflater.inflate(R.layout.list_row, null);
    }
    */
    int pos=position;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_row, null);
        viewHolder=new ViewHolder();
        viewHolder.txt_id = (TextView) convertView.findViewById(R.id.id); // title
        viewHolder.txt_product_name = (TextView) convertView.findViewById(R.id.title); // title
        viewHolder.artist = (TextView) convertView.findViewById(R.id.artist); // artist
                                                                    // name
        viewHolder.txt_mspecialprice_withouttax = (TextView) convertView.findViewById(R.id.duration); // duration
        viewHolder.stock = (TextView) convertView.findViewById(R.id.stck);
        viewHolder.txt_mprice_withouttax = (TextView) convertView.findViewById(R.id.txtmpricewithouttax);

        viewHolder.thumb_image = (ImageView) convertView.findViewById(R.id.list_image); // thumb
                                                                                // image


        convertView.setTag(viewHolder);

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


    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    String mspecialprice_str=song.get(CustomizedListView.KEY_PRODUCT_MSPECIAL_WITHOUT_TAX);
    //String substr_mspecialprice_str=mspecialprice_str.substring(1,mspecialprice_str.indexOf("."));
    //String substr_mspecialprice_str_replaced=substr_mspecialprice_str.replace(",", "");

    String msaleprice_str=song.get(CustomizedListView.KEY_PRODUCT_MPRICE_WITHOUT_TAX);
    //String substr_msaleprice_str=msaleprice_str.substring(0,msaleprice_str.indexOf("."));
    //String substr_msaleprice_str_replaced=substr_msaleprice_str.replace(",", "");

    viewHolder.txt_id.setText(song.get(CustomizedListView.KEY_PRODUCT_ID));
    viewHolder.txt_product_name.setText(song.get(CustomizedListView.KEY_PRODUCT_NAME));
    viewHolder.artist.setText(song.get(CustomizedListView.KEY_PRODUCT_DESCRIPTION));
    viewHolder.stock.setText(song.get(CustomizedListView.KEY_STOCK));
    if(mspecialprice_str.equals("0"))
    {
        //txt_mspecialprice_withouttax.setText(song.get(CustomizedListView.KEY_PRODUCT_MSPECIAL_WITHOUT_TAX));
        viewHolder.txt_mprice_withouttax.setText("$"+(song.get(CustomizedListView.KEY_PRODUCT_MPRICE_WITHOUT_TAX)));

        viewHolder.txt_mprice_withouttax.setTextColor(Color.parseColor("#64aef9"));

    }
    //if(!(mspecialprice_str.equals("0")))
    //{
else
{
    viewHolder.txt_mspecialprice_withouttax.setText("$"+(song.get(CustomizedListView.KEY_PRODUCT_MSPECIAL_WITHOUT_TAX)));
    viewHolder.txt_mprice_withouttax.setText("$"+(song.get(CustomizedListView.KEY_PRODUCT_MPRICE_WITHOUT_TAX)));
    viewHolder.txt_mprice_withouttax.setPaintFlags(viewHolder.txt_mprice_withouttax.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    viewHolder.txt_mprice_withouttax.setTextColor(Color.parseColor("#F01616"));
}

    imageLoader.DisplayImage(
            song.get(CustomizedListView.KEY_PRODUCT_IMAGEURL), viewHolder.thumb_image);

    return convertView;
}
}

class ViewHolder {
TextView txt_id ; // title
TextView txt_product_name; // title
TextView artist ; // artist
                                                            // name
TextView txt_mspecialprice_withouttax; // duration
TextView stock ;
TextView txt_mprice_withouttax;

ImageView thumb_image ; // thumb


}

2 个答案:

答案 0 :(得分:1)

ListView,GridView重用视图用作列表项/网格元素。每当android尝试在滚动视图时绘制下一个元素时,都会调用getView()。没有必要阻止它!

修改 - Atrix1987

来自开发者docs

  
    

Adapter对象充当AdapterView与该视图的基础数据之间的桥梁。适配器提供对数据项的访问。适配器还负责为数据集中的每个项目创建视图。

  

假设您要使用GridView / ListView显示10个元素,并且最大可见项为5,则滚动时可以重复使用相同的5个视图来显示5个元素中的其余元素。这是预期的行为,是正确的做事方式[将视图数量保持在最低限度]。

您无法控制getView方法,框架会为您执行此操作。

答案 1 :(得分:0)

GridView小部件扩展AdapterView。它使用适配器来重用View并提高性能。没有办法避免调用getView() - 这对整个AdapterView想法至关重要。如果你想要一个静态布局,也许你应该使用别的东西。