我在下面ListView
有一个自定义适配器。我从服务中收到ArrayAdapter
,但滚动时发生了错误,值丢失了。
public class AccountStatementArrayAdapter extends ArrayAdapter<ListaExtratos> {
public AccountStatementArrayAdapter(Context context, int textViewResourceId, List<ListaExtratos> listaExtratos) {
super(context, textViewResourceId, listaExtratos);
this.listaExtratos = listaExtratos;
}
@Override
public int getItemViewType(int position) {
return listaExtratos.get(position).getData() == null ? SECTION : ACCOUNT_STATEMENT_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return listaExtratos.size();
}
@Override
public ListaExtratos getItem(int position) {
return listaExtratos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_account_statement, parent, false);
}
ListaExtratos p = getItem(position);
if (p != null && position != 0) {
TextView simpleDescriptionTextView = (TextView) v.findViewById(R.id.list_item_account_statement_simple_description_field);
TextView txtDataExtrato = (TextView) v.findViewById(R.id.txtDataExtrato);
TextView simpleValueTextView = (TextView) v.findViewById(R.id.list_item_account_statement_simple_value_field);
TextView completeDescriptionTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_description_field);
TextView completeDateTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_date_field);
TextView completeValueTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_value_field);
TextView completeDocumentTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_document_field);
TextView completeBalanceTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_balance_field);
if(p.getHistorico() != null){
simpleDescriptionTextView.setText(p.getHistorico());
}
if(p.getValor() != null){
if ((p.getValor() != null) && (Double.parseDouble(p.getValor()) < 0)) {
simpleValueTextView.setTextColor(Color.RED);
completeValueTextView.setTextColor(Color.RED);
completeValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
simpleValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
} else {
simpleValueTextView.setTextColor(Color.BLACK);
completeValueTextView.setTextColor(Color.BLACK);
completeValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
simpleValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
}
}
if(p.getHistorico() != null)
completeDescriptionTextView.setText(p.getHistorico());
if(p.getData() != null)
completeDateTextView.setText(p.getData());
if(p.getDocto() != null)
completeDocumentTextView.setText(p.getDocto());
completeBalanceTextView.setVisibility(View.GONE);
if (!datas.contains(p.getData())) {
txtDataExtrato.setVisibility(View.VISIBLE);
txtDataExtrato.setText(DateUtil.getDataPorExtenso(DateUtil.dateFromString(p.getData(), "dd/MM/yyyy")));
datas += p.getData() + ";";
} else {
txtDataExtrato.setVisibility(View.GONE);
}
}
return v;
}
}
答案 0 :(得分:0)
适配器代码中存在一些错误。
例如。 : 改变这个
if(p.getHistorico() != null){
simpleDescriptionTextView.setText(p.getHistorico());
}
到
if(p.getHistorico() != null){
simpleDescriptionTextView.setText(p.getHistorico());
}
else {
simpleDescriptionTextView.setText("");
}
这些更改可以解决您的问题。