我已經搜索了關於這個問題的其他帖子,並試圖實現一個持有人,但沒有成功。ListView滾動時混合值
當我向上/向下滾動列表視圖亂七八糟的值。在某個列表項中應該爲空的字段會顯示一個屬於另一個列表項的值。
這裏是我的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- ----------------------------------
我已經使用了Vinay Garg的答案,但與一些修改。最終,我得到這個:
@覆蓋 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司){
// 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;
}
謝謝維奈加爾格!