0

我的应用程序中有一个 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;
}

Tks

4

2 回答 2

2

只是意识到逻辑上缺少 else 造成了麻烦,感谢所有试图帮助的人!

于 2012-05-31T16:46:55.547 回答
0

实施

    onSavedInstanceState(Bundle bundle);

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

于 2012-05-31T14:28:52.480 回答