Android列表视图在滚动入适配器上显示重复值

Android列表视图在滚动入适配器上显示重复值,android,android-listview,Android,Android Listview,我正在将数据绑定到Android中的列表视图控件。数据存储在ArrayList中。在适配器的getView()函数中,我得到了由23项组成的完整列表。如果“selectedval”为“1”,我会将值设置为行中的TextView 然而,尽管列表包含23个唯一的值,但我在视图中看到重复的值。在调试时,我看到值在scroll上被复制 下面是getView()函数的代码 @SuppressLint("InflateParams") @Override public View getView(int po

我正在将数据绑定到Android中的列表视图控件。数据存储在ArrayList中。在适配器的getView()函数中,我得到了由23项组成的完整列表。如果“selectedval”为“1”,我会将值设置为行中的TextView

然而,尽管列表包含23个唯一的值,但我在视图中看到重复的值。在调试时,我看到值在scroll上被复制

下面是getView()函数的代码

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View mView = convertView;

    if (mView == null){
        mView = mInflator.inflate(R.layout.custom_rate_card, null);
        FancyTextview mTxtName = (FancyTextview)mView.findViewById(R.id.txt_list_name);
        FancyTextview mTxtPrice = (FancyTextview)mView.findViewById(R.id.txt_list_price);

        if(selectedVal == 0){
            String laundryPrice = mRateCardData.get(position).getLaundryPrice();
            if(Integer.parseInt(laundryPrice) != 0 && laundryPrice != null)
            {   
                mTxtName.setText(mRateCardData.get(position).getName());
                mTxtPrice.setText(laundryPrice);
            }
        }
        if(selectedVal == 1){
            String IPrice = mRateCardData.get(position).getIroningPrice();
            String Iname = mRateCardData.get(position).getName();
            if(Integer.parseInt(IPrice) != 0 && IPrice != null)
            {
                mTxtPrice.setText(IPrice);
                mTxtName.setText(Iname);
            }
        }
        return mView;
    }
    else{
        return mView;
    }
}
我搜索了一下,认为这和景观的循环利用有关。但我一直无法解决它


任何帮助都将不胜感激。谢谢。

您创建视图的方式不正确。只有当convertView为空时,才需要膨胀新视图,但每次调用getView时都需要设置数据

我建议考虑使用以下模式:

您的代码应该如下所示:

@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View mView = convertView;

    if (mView == null){
        mView = mInflator.inflate(R.layout.custom_rate_card, null);
        FancyTextview mTxtName = (FancyTextview)mView.findViewById(R.id.txt_list_name);
        FancyTextview mTxtPrice = (FancyTextview)mView.findViewById(R.id.txt_list_price);

     }
     if(selectedVal == 0){
        String laundryPrice = mRateCardData.get(position).getLaundryPrice();
        if(Integer.parseInt(laundryPrice) != 0 && laundryPrice != null)
        {   
            mTxtName.setText(mRateCardData.get(position).getName());
            mTxtPrice.setText(laundryPrice);
        }
    } else if (selectedVal == 1) {
        String IPrice = mRateCardData.get(position).getIroningPrice();
        String Iname = mRateCardData.get(position).getName();
        if(Integer.parseInt(IPrice) != 0 && IPrice != null)
        {
            mTxtPrice.setText(IPrice);
            mTxtName.setText(Iname);
        }
   }



   return mView;

}

请理解。。。那就应该是obvious@Selvin,我正在经历一个简单的事情没有意义的时代。只是想寻求帮助。对不起,我是在浪费时间。这个问题被人们否决是有原因的。OP甚至还没有执行一个基本级别的搜索,而这个搜索会为listview的usageOP提供编译和运行的代码,并且显然已经对其进行了调试。我看到了努力。他只是需要一些指导。@humblerookie,谢谢你这么理解并考虑到无数可能导致开发人员在不进行基本搜索的情况下提出如此“愚蠢”的问题的情况。谢谢@GaryBak。我在ListView上没有做很多工作。我是StackOverflow的一部分,因为像您这样无私的开发人员随时准备提供帮助。