ListView在滚动时混合值

时间:2017-04-21 17:38:01

标签: android listview

我在其他帖子中搜索过这个问题并试图实现一个Holder,但没有成功。

当我向上/向下滚动列表视图时,会将值混乱。在某个列表项中应为空的字段显示为属于另一个列表项的值。

这是我的getView方法:

JOIN

------------------------------ EDITED ------------- ----------------------

我使用了Vinay Garg的答案,但经过了一些修改。最后,我得到了这个:

@覆盖     public View getView(int position,View convertView,ViewGroup parent){

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


    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
    }
    // Get the {@link Record} object located at this position in the list
    Record currentRecord = getItem(position);

    if (currentRecord != null) {

        // Find the TextView in the list_item.xml layout with the ID version_name
        TextView nameTextView = (TextView) listItemView.findViewById(R.id.record_name);
        // Get the version name from the current AndroidFlavor object and
        // set this text on the name TextView

            nameTextView.setText(currentRecord.getmName());
            nameTextView.setTextSize(15);
            nameTextView.setTextColor(ContextCompat.getColor(getContext(), R.color.colorTitle));

            ImageView imageViewMiniatura = (ImageView) listItemView.findViewById(R.id.imageViewMiniatura);

            UrlImageViewHelper.setUrlDrawable(imageViewMiniatura, currentRecord.getmThumbnailUrl());

            View termometherIndicatorView = (View) listItemView.findViewById(R.id.termometherIndicator);

            switch (currentRecord.getmSellerReputation()) {
                case "1_red":
                    termometherIndicatorView.setBackgroundColor(0xFFF4111C);
                    break;
                case "2_orange":
                    termometherIndicatorView.setBackgroundColor(0xFFFF8000);
                    break;
                case "3_yellow":
                    termometherIndicatorView.setBackgroundColor(0xFFFFF059);
                    break;
                case "4_light_green":
                    termometherIndicatorView.setBackgroundColor(0xFFD6F076);
                    break;
                case "5_green":
                    termometherIndicatorView.setBackgroundColor(0xFF00B200);
                    break;
                default:
                    termometherIndicatorView.setBackgroundColor(0xFFF4F4F4);

            }

            // Find the TextView in the list_item.xml layout with the ID version_number
            TextView numberTextView = (TextView) listItemView.findViewById(R.id.record_price);
            // Get the version number from the current AndroidFlavor object and
            // set this text on the number TextView
            numberTextView.setText(converterDoubleEmReais(currentRecord.getmPrice()));
            numberTextView.setTextSize(20);
            numberTextView.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrice));

            TextView textViewPrecoComTabela = (TextView) listItemView.findViewById(R.id.preco_com_tabela);
            ImageView imageViewPrecoComSedex = (ImageView) listItemView.findViewById(R.id.preco_com_sedex);
            ImageView imageViewPrecoComPac = (ImageView) listItemView.findViewById(R.id.preco_com_pac);
            ImageView imageViewPrecoComExpresso = (ImageView) listItemView.findViewById(R.id.preco_com_menvios_expresso);
            ImageView imageViewPrecoComNormal = (ImageView) listItemView.findViewById(R.id.preco_com_menvios_normal);
            TextView textViewPrecoComSedex = (TextView) listItemView.findViewById(R.id.preco_com_sedex_text_view);
            TextView textViewPrecoComPac = (TextView) listItemView.findViewById(R.id.preco_com_pac_text_view);
            TextView textViewPrecoComExpresso = (TextView) listItemView.findViewById(R.id.preco_com_menvios_expresso_text_view);
            TextView textViewPrecoComNormal = (TextView) listItemView.findViewById(R.id.preco_com_menvios_normal_text_view);

            textViewPrecoComTabela.setText("Preço Tabelado: ");
            imageViewPrecoComSedex.setVisibility(View.VISIBLE);
            imageViewPrecoComPac.setVisibility(View.VISIBLE);
            imageViewPrecoComNormal.setVisibility(View.VISIBLE);
            imageViewPrecoComExpresso.setVisibility(View.VISIBLE);

            TextView sellerLocationTextView = (TextView) listItemView.findViewById(R.id.seller_location);
            sellerLocationTextView.setText("(" + currentRecord.getmSellerLocation().toString() + ")");

            /**TextView anuncioStartDateTextView = (TextView) listItemView.findViewById(R.id.anuncio_start_date);
             DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
             Date dataAnuncio = currentRecord.getmAnuncioId();
             String dataEmString = df.format(dataAnuncio);
             anuncioStartDateTextView.setText(dataEmString);**/

            ImageView freeShippingImageView = (ImageView) listItemView.findViewById(R.id.imageViewFreteGratis);
            if (currentRecord.ismFreeShipping()) {
                freeShippingImageView.setVisibility(View.VISIBLE);
            } else {
                freeShippingImageView.setVisibility(View.GONE);
            }

            if (currentRecord.getmShippingMode().equals("custom")) {

                textViewPrecoComTabela.setText("Tabelado: " + converterDoubleEmReais(currentRecord.getmMercadoLivreCustomPrice()) + "(" + converterDoubleEmReais(currentRecord.getmMercadoLivreCustomPrice() + currentRecord.getmPrice()) + ")");
            } else if (currentRecord.getmShippingMode().equals("not_specified")) {

                textViewPrecoComSedex.setText("Sedex: " + converterDoubleEmReais(currentRecord.getmPrecoSedex()) + "(" + converterDoubleEmReais(currentRecord.getmPrecoSedex() + currentRecord.getmPrice()) + ")");

                textViewPrecoComPac.setText("Pac: " + converterDoubleEmReais(currentRecord.getmPrecoPac()) + "(" + converterDoubleEmReais(currentRecord.getmPrecoPac() + currentRecord.getmPrice()) + ")");
                // Find the ImageView in the list_item.xml layout with the ID list_item_icon
                //ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
                // Get the image resource ID from the current AndroidFlavor object and
                // set the image to iconView
                //iconView.setImageResource(currentRecord.getImageResourceId());
            } else if (currentRecord.getmShippingMode().equals("me1") || currentRecord.getmShippingMode().equals("me2")) {

                textViewPrecoComExpresso.setText("Expresso: " + converterDoubleEmReais(currentRecord.getmMercadoEnviosExpresso()) + "(" + converterDoubleEmReais(currentRecord.getmMercadoEnviosExpresso() + currentRecord.getmPrice()) + ")");

                textViewPrecoComNormal.setText("Normal: " + converterDoubleEmReais(currentRecord.getmMercadoEnviosNormal()) + "(" + converterDoubleEmReais(currentRecord.getmMercadoEnviosNormal() + currentRecord.getmPrice()) + ")");
                // Find the ImageView in the list_item.xml layout with the ID list_item_icon
                //ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
                // Get the image resource ID from the current AndroidFlavor object and
                // set the image to iconView
                //iconView.setImageResource(currentRecord.getImageResourceId());
            }

        }
    return listItemView;
}

谢谢Vinay Garg !!

1 个答案:

答案 0 :(得分:0)

是的,您应该使用ViewHolder,但此代码也应该有效。 没有ViewHolder的getView实现似乎是正确的,也许问题出现在下面的代码中,我通过在最后添加else条件来修改: -

if (currentRecord.getmShippingMode().equals("custom")) {

            textViewPrecoComTabela.setText("Tabelado: " + converterDoubleEmReais(currentRecord.getmMercadoLivreCustomPrice()) + "(" + converterDoubleEmReais(currentRecord.getmMercadoLivreCustomPrice() + currentRecord.getmPrice()) + ")");
        } else if (currentRecord.getmShippingMode().equals("not_specified")) {

            textViewPrecoComSedex.setText("Sedex: " + converterDoubleEmReais(currentRecord.getmPrecoSedex()) + "(" + converterDoubleEmReais(currentRecord.getmPrecoSedex() + currentRecord.getmPrice()) + ")");

            textViewPrecoComPac.setText("Pac: " + converterDoubleEmReais(currentRecord.getmPrecoPac()) + "(" + converterDoubleEmReais(currentRecord.getmPrecoPac() + currentRecord.getmPrice()) + ")");
            // Find the ImageView in the list_item.xml layout with the ID list_item_icon
            //ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
            // Get the image resource ID from the current AndroidFlavor object and
            // set the image to iconView
            //iconView.setImageResource(currentRecord.getImageResourceId());
        } else if (currentRecord.getmShippingMode().equals("me1") || currentRecord.getmShippingMode().equals("me2")) {

            textViewPrecoComExpresso.setText("Expresso: " + converterDoubleEmReais(currentRecord.getmMercadoEnviosExpresso()) + "(" + converterDoubleEmReais(currentRecord.getmMercadoEnviosExpresso() + currentRecord.getmPrice()) + ")");

            textViewPrecoComNormal.setText("Normal: " + converterDoubleEmReais(currentRecord.getmMercadoEnviosNormal()) + "(" + converterDoubleEmReais(currentRecord.getmMercadoEnviosNormal() + currentRecord.getmPrice()) + ")");
            // Find the ImageView in the list_item.xml layout with the ID list_item_icon
            //ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
            // Get the image resource ID from the current AndroidFlavor object and
            // set the image to iconView
            //iconView.setImageResource(currentRecord.getImageResourceId());
        }else{
         textViewPrecoComExpresso.setText("");
         textViewPrecoComNormal.setText("");
    }

在任何情况下,您的if条件都可能不匹配,并且您最终还没有添加else条件。 (我假设问题出在textViewPrecoComExpresso and textViewPrecoComNormal textviews as you didn't mention which fields are jumbled) You should add the其他condition at the end and set the textViewPrecoComExpresso and textViewPrecoComNormal`为空。