Android 滚动列表视图填充虚拟值

Android 滚动列表视图填充虚拟值,android,listview,scroll,Android,Listview,Scroll,我的列表视图包含一个图像和一些文本视图;有两个包含价格和特价的textiview,如果特价为0比仅设置价格textview和特价,则允许其为空,但特价大于设置价格值以及特价,第一次一切都很顺利,但是当我滚动listview时,然后是空白的textview,设置了dumyy值在这里输入code 这是我的getView方法代码 @Override public View getView(final int position, View convertView, ViewGroup p

我的列表视图包含一个图像和一些文本视图;有两个包含价格和特价的textiview,如果特价为0比仅设置价格textview和特价,则允许其为空,但特价大于设置价格值以及特价,第一次一切都很顺利,但是当我滚动listview时,然后是空白的textview,设置了dumyy值
在这里输入code

这是我的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


}
@覆盖
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
//公共视图getView(int位置、视图转换视图、视图组父视图){
/*视图vi=转换视图;
if(convertView==null)
{
vi=充气机充气(R.layout.list_行,空);
}
*/
int pos=位置;
if(convertView==null){
convertView=充气机。充气(R.layout.list_行,空);
viewHolder=新的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
//名字
viewHolder.txt_mspecialprice_without tax=(TextView)convertView.findViewById(R.id.duration);//duration
viewHolder.stock=(TextView)convertView.findViewById(R.id.stck);
viewHolder.txt\u mprice\u without tax=(TextView)convertView.findViewById(R.id.txtmpricewithouttax);
viewHolder.thumb_image=(ImageView)convertView.findViewById(R.id.list_image);//thumb
//形象
convertView.setTag(viewHolder);
}
否则{
viewHolder=(viewHolder)convertView.getTag();
}
HashMap宋=新HashMap();
宋=数据。获取(位置);
//在listview中设置所有值
字符串mspecialprice\u str=song.get(CustomizedListView.KEY\u PRODUCT\u MSPECIAL\u无税);
//字符串substr\u msspecialprice\u str=msspecialprice\u str.substring(1,msspecialprice\u str.indexOf(“.”);
//字符串substr_mspecialprice_str_replaced=substr_mspecialprice_str.replace(“,”,”);
字符串msaleprice\u str=song.get(CustomizedListView.KEY\u PRODUCT\u MPRICE\u,不含税);
//字符串substr_msaleprice_str=msaleprice_str.substring(0,msaleprice_str.indexOf(“.”));
//字符串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\u PRODUCT\u DESCRIPTION));
viewHolder.stock.setText(song.get(CustomizedListView.KEY_stock));
如果(mspecialprice_str.等于(“0”))
{
//txt_mspecialprice_WITHOUT TAX.setText(song.get(CustomizedListView.KEY_PRODUCT_MSPECIAL_WITHOUT TAX));
viewHolder.txt_mprice_WITHOUT TAX.setText(“$”+(song.get(CustomizedListView.KEY_PRODUCT_mprice_WITHOUT TAX));
viewHolder.txt_mprice_without tax.setTextColor(Color.parseColor(“#64aef9”);
}
//如果(!(MSSpecialPrice_.str.等于(“0”))
//{
其他的
{
viewHolder.txt_mspecialprice_WITHOUT TAX.setText(“$”+(song.get(CustomizedListView.KEY_PRODUCT_MSPECIAL_WITHOUT TAX));
viewHolder.txt_mprice_WITHOUT TAX.setText(“$”+(song.get(CustomizedListView.KEY_PRODUCT_mprice_WITHOUT TAX));
viewHolder.txt_mprice_without tax.setPaintFlags(viewHolder.txt_mprice_without tax.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
viewHolder.txt_mprice_without tax.setTextColor(Color.parseColor(“#F01616”);
}
imageLoader.DisplayImage(
song.get(CustomizedListView.KEY\u PRODUCT\u IMAGEURL)、viewHolder.thumb\u image);
返回视图;
}
}
类视图持有者{
TextView txt_id;//标题
TextView txt_产品_名称;//标题
TextView艺术家;//艺术家
//名字
TextView txt\u mspecialprice\u不含税;//持续时间
TextView股票;
TextView txt_mprice_不含税;
ImageView thumb_image;//thumb
}

ListView,GridView重用用作列表项/网格元素的视图。每次android在滚动视图时尝试绘制下一个元素时,都会调用getView()。不需要阻止这种情况

编辑-

发展商

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

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


您没有对
getView
方法的控制权,框架会为您这样做。

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

谢谢你的回复,但我只想在数据加载到listview/gridview后执行一次getview方法,那么我不想每次更改listview项目位置时都再次执行getview方法,因此我不想更改listview项的位置。加载后也不会调用listview(),而是在listview要绘制某些内容时调用它。假设您的数据适配器中有100个元素,但listview一次只显示10个元素,您将得到10个getView()调用。只需要10个,因为显示listview只需要10个。这就是方法