滚动视图而不丢失数据

时间:2012-05-31 14:22:37

标签: android

我的应用程序中有一个Scrollview,它有很多要加载的数据,所有这些都是加载对象属性的TextView,但是当我滚动视图时,它有时会加载要显示的对象内容,有时候不。那么我能做些什么来实现这一目标呢?

这是ViewHolder类(它有点难看): 我忘了提到的东西,这个类扩展了BaseAdapter,所以,没有活动

private class ViewHolder {
    TextView txtName;
    TextView txtFaults;
    TextView txtA;
    TextView txtB;
    TextView txtC;
    TextView txtD;
    TextView txtE;
    TextView txtF;
    TextView txtG;
    TextView txtH;
    TextView txtI;
    TextView txtPFinal;
    TextView txtPart;
    TextView txtMF;
    TextView txtMP;
    TextView txtForm;
    TextView txtMN;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.row, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView
                .findViewById(R.id.txtMateria);
        holder.txtFaults = (TextView) convertView
                .findViewById(R.id.txtFaults);
        holder.txtA = (TextView) convertView.findViewById(R.id.txtA);
        holder.txtB = (TextView) convertView.findViewById(R.id.txtB);
        holder.txtC = (TextView) convertView.findViewById(R.id.txtC);
        holder.txtD = (TextView) convertView.findViewById(R.id.txtD);
        holder.txtE = (TextView) convertView.findViewById(R.id.txtE);
        holder.txtF = (TextView) convertView.findViewById(R.id.txtF);
        holder.txtG = (TextView) convertView.findViewById(R.id.txtG);
        holder.txtH = (TextView) convertView.findViewById(R.id.txtH);
        holder.txtI = (TextView) convertView.findViewById(R.id.txtI);
        holder.txtPFinal = (TextView) convertView.findViewById(R.id.txtPFinal);
        holder.txtPart = (TextView) convertView
                .findViewById(R.id.txtPart);
        holder.txtMF = (TextView) convertView.findViewById(R.id.txtMF);
        holder.txtMP = (TextView) convertView.findViewById(R.id.txtMP);
        holder.txtForm = (TextView) convertView
                .findViewById(R.id.txtForm);
        holder.txtMN = (TextView) convertView
                .findViewById(R.id.txtMN);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Materia materia = list.get(position);
    holder.txtName.setText(materia.getMateria());
    holder.txtName.setTypeface(null, Typeface.BOLD);
    holder.txtFaults.setText("Faults:"
            + Double.toString(materia.getFaltas()) + "%");

    holder.txtA.setText("A:" + Double.toString(materia.getA()));
    if (materia.getA() == 98.9)
        holder.txtA.setVisibility(View.GONE);

    holder.txtB.setText("B:" + Double.toString(materia.getB()));
    if (materia.getB() == 98.9)
        holder.txtB.setVisibility(View.GONE);

    holder.txtC.setText("C:" + Double.toString(materia.getC()));
    if (materia.getC() == 98.9)
        holder.txtC.setVisibility(View.GONE);

    holder.txtD.setText("D:" + Double.toString(materia.getD()));
    if (materia.getD() == 98.9)
        holder.txtD.setVisibility(View.GONE);

    holder.txtE.setText("E:" + Double.toString(materia.getE()));
    if (materia.getE() == 98.9)
        holder.txtE.setVisibility(View.GONE);

    holder.txtF.setText("F:" + Double.toString(materia.getF()));
    if (materia.getF() == 98.9)
        holder.txtF.setVisibility(View.GONE);

    holder.txtG.setText("G:" + Double.toString(materia.getG()));
    if (materia.getG() == 98.9)
        holder.txtG.setVisibility(View.GONE);

    holder.txtH.setText("H:" + Double.toString(materia.getH()));
    if (materia.getH() == 98.9)
        holder.txtH.setVisibility(View.GONE);

    holder.txtI.setText("I:" + Double.toString(materia.getI()));
    if (materia.getI() == 98.9)
        holder.txtI.setVisibility(View.GONE);

    holder.txtPFinal.setText("PF:" + Double.toString(materia.getPf()));
    if (materia.getPfinal() == 98.9)
        holder.txtPFinal.setVisibility(View.GONE);

    holder.txtPart.setText("Partic:"
            + Double.toString(materia.getPartic()));
    if (materia.getPart() == 98.9)
        holder.txtPart.setVisibility(View.GONE);

    holder.txtMF.setText("MF:" + Double.toString(materia.getMf()));
    if (materia.getMf() == 98.9)
        holder.txtMF.setVisibility(View.GONE);

    holder.txtMP.setVisibility(View.GONE);
    if (!materia.getForm().toLowerCase().contains("inform")) {
        holder.txtMP.setText("MP:"
                + String.format("%.2f", materia.getMP()));
        holder.txtMP.setVisibility(View.VISIBLE);
    }
    holder.txtMN.setText("MN:LongClick to simulate");
    holder.txtMN.setTextColor(Color.GRAY);
    if (materia.getFormula().toLowerCase().contains("inform"))
        holder.txtMN
                .setText("Form not avaiable");

    holder.txtForm.setText("Form not informed");
    if (!materia.getFormula().toLowerCase().contains("inform"))
        holder.txtForm.setText("Form:" + materia.getForm());

    return convertView;
}

韩国社交协会

2 个答案:

答案 0 :(得分:2)

只要意识到逻辑上缺少其他东西就会造成麻烦,谢谢所有试图提供帮助的人!

答案 1 :(得分:0)

实施

    onSavedInstanceState(Bundle bundle);

方法应该解决这个问题。您可以在此处找到有关它的信息 http://developer.android.com/reference/android/app/Activity.html