首页 文章

ListView在滚动时混合值

提问于
浏览
-1

我已经搜索了关于这个问题的其他帖子,并试图实现一个Holder,但没有成功 .

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

这是我的getView方法:

@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;
}

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

I've used Vinay Garg answer, but with some modifications. In the end, i got this:

@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 AndroidFlavor} object located at this position in the list
    Record currentRecord = getItem(position);

    //listItemView.setBackgroundResource(R.drawable.customshape);

    // 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()){ // ta dando problema de nullPointer aleatóriamente
        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);

    LinearLayout linearLayoutPrecoComSedex = (LinearLayout) listItemView.findViewById(R.id.preco_sedex_layout);
    LinearLayout linearLayoutPrecoComPac = (LinearLayout) listItemView.findViewById(R.id.preco_pac_layout);
    LinearLayout linearLayoutPrecoComExpresso = (LinearLayout) listItemView.findViewById(R.id.preco_menvios_expresso_layout);
    LinearLayout linearLayoutPrecoComNormal = (LinearLayout) listItemView.findViewById(R.id.preco_menvios_normal_layout);


               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()) + ")");
        textViewPrecoComExpresso.setText("");
        textViewPrecoComNormal.setText("");
        textViewPrecoComPac.setText("");
        textViewPrecoComSedex.setText("");
        linearLayoutPrecoComSedex.setVisibility(View.GONE);
        linearLayoutPrecoComPac.setVisibility(View.GONE);
        linearLayoutPrecoComExpresso.setVisibility(View.GONE);
        linearLayoutPrecoComNormal.setVisibility(View.GONE);
    } else if(currentRecord.getmShippingMode().equals("not_specified")) {

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

        textViewPrecoComPac.setText(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());

        textViewPrecoComExpresso.setText("");
        textViewPrecoComNormal.setText("");
        textViewPrecoComTabela.setText("");
        linearLayoutPrecoComSedex.setVisibility(View.VISIBLE);
        linearLayoutPrecoComPac.setVisibility(View.VISIBLE);
        linearLayoutPrecoComExpresso.setVisibility(View.GONE);
        linearLayoutPrecoComNormal.setVisibility(View.GONE);
    } else if(currentRecord.getmShippingMode().equals("me1") || currentRecord.getmShippingMode().equals("me2")) {

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

        textViewPrecoComNormal.setText(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());
        textViewPrecoComPac.setText("");
        textViewPrecoComSedex.setText("");
        textViewPrecoComTabela.setText("");
        linearLayoutPrecoComExpresso.setVisibility(View.VISIBLE);
        linearLayoutPrecoComNormal.setVisibility(View.VISIBLE);
        linearLayoutPrecoComSedex.setVisibility(View.GONE);
        linearLayoutPrecoComPac.setVisibility(View.GONE);
    } else {
        textViewPrecoComExpresso.setText("");
        textViewPrecoComNormal.setText("");
        textViewPrecoComPac.setText("");
        textViewPrecoComSedex.setText("");
        textViewPrecoComTabela.setText("");
        linearLayoutPrecoComSedex.setVisibility(View.GONE);
        linearLayoutPrecoComPac.setVisibility(View.GONE);
        linearLayoutPrecoComExpresso.setVisibility(View.GONE);
        linearLayoutPrecoComNormal.setVisibility(View.GONE);
    }



    // Return the whole list item layout (containing 2 TextViews and an ImageView)
    // so that it can be shown in the ListView
    return listItemView;
}

谢谢Vinay Garg !!

1 回答

  • 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 else condition at the end and set the textViewPrecoComExpresso and textViewPrecoComNormal`清空 .

相关问题