列表视图显示不稳定的数据



我正在使用光标适配器来填充列表视图,该列表视图包含文本,图像和每行的水平进度条。 一切都很好,除了在行中上下滚动时所有信息都会混淆。 我很确定这与光标如何回收信息有关,当它重新加载信息时,它会混淆。 我似乎找不到解决方案,谢谢你的帮助。

public class MyDisplayAdapter extends CursorAdapter{
    private final Context context;
    private final Cursor c;
    public MyDisplayAdapter(Context context, Cursor c) {
        super(context,c);
        this.context=context;
        this.c=c;
    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        long Time = new Date().getTime();
        String savedodom=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_ODOM));
        String savedmiles=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_MILES));
        String savedtime=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_Q));
        long timer = Long.valueOf(savedtime);
        long realtime = Time-timer;
        int w = Integer.parseInt(savedmiles);
        int g = Integer.parseInt(savedodom);
        long m = realtime;
        int l = (int) m;
        int frealtime = l/10000;
        int ght = 8640/w;
        int k = frealtime/ght;
        int frd = g+k;
        long bn = (long) frd;
        ImageView imageicon = (ImageView) view.findViewById(R.id.button1);

        String eid=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_ID));
        TextView t4 = (TextView) view.findViewById(R.id.id);

        String ename=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_NAME));
        TextView t1 = (TextView) view.findViewById(R.id.text1);
        t1.setText(ename);
        if(t1.getText().toString().equals("Brake Pads")){
            imageicon.setBackgroundResource(R.drawable.mtire);
        }
        if(t1.getText().toString().equals("Air Filter")){
            imageicon.setBackgroundResource(R.drawable.maiffilter);
        }
         String edept=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_DEPT));
         TextView t2 = (TextView) view.findViewById(R.id.text3);
        String eage=cursor.getString(cursor.getColumnIndex(EmployeeDatabase.EMP_AGE));
        int x = Integer.valueOf(eage);
        int x2 = Integer.valueOf(edept);
        int ww = x2+x;
        int trb = ww-frd;
        TextView t3 = (TextView) view.findViewById(R.id.textView7);
        ProgressBar pg = (ProgressBar)view.findViewById(R.id.progressBar1);
        pg.setProgress(trb);
        pg.setMax(x);
        int pet = pg.getMax();
        int prog = pg.getProgress();
        t2.setText(""+x);
        t4.setText(""+pet);
        t3.setText(""+prog);
        if(trb < 10){
            t3.setTextColor(Color.RED);
        }
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.item, null, false);
        return rowView;
    }

在我的主要活动中

db=new EmployeeDatabase(this);
c=db.query(EmployeeDatabase.EMP_TABLE, null,
        null,null,null,null,null);
MyDisplayAdapter adapter = new MyDisplayAdapter(this, c);// OWN ADAPTER
ListContent.setAdapter(adapter);

每当使用适配器时,都应始终使用 if-else 而不是 if。你有你的绑定视图方法在做:

if(t1.getText().toString().equals("Brake Pads")){
    imageicon.setBackgroundResource(R.drawable.mtire);
}

这意味着,如果它是真的,但现在不是,则可绘制对象不会被重置。 执行以下操作:

if(t1.getText().toString().equals("Brake Pads")){
    imageicon.setBackgroundResource(R.drawable.mtire);
}
else {
    imageicon.setBackgroundResource(defaultResourceId)
}

另外,只是一个随机建议,请考虑使用 ViewHolder,因为它将帮助您的表现。您还可以将光标行冷转储到 ContentValues 对象,这样您就不必调用 cursor.getString(cursor.getColumnIndex(key))

最新更新