列表视图在滚动时丢失值,并在项目单击时获取worg位置

时间:2016-06-13 19:58:41

标签: java android

我在下面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;
    }
}

1 个答案:

答案 0 :(得分:0)

适配器代码中存在一些错误。

  1. 删除位置检查!= 0
  2. 在为每个视图设置空文本的位置添加else检查。
  3. 例如。 : 改变这个

      if(p.getHistorico() != null){
                    simpleDescriptionTextView.setText(p.getHistorico());
                }
    

      if(p.getHistorico() != null){
                    simpleDescriptionTextView.setText(p.getHistorico());
                } 
     else {
          simpleDescriptionTextView.setText("");
     }
    
      

    这些更改可以解决您的问题。