0

我正在使用 cursoradapter 填充列表视图,其中包含文本、图像和每行中的水平进度条。一切都很好,除了在上下滚动行时所有信息都混淆了。我很确定它与光标如何回收信息以及重新加载它时混淆有关。我似乎无法找到解决此问题的方法,谢谢您的帮助。

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);
4

1 回答 1

2

任何时候使用适配器时,都应该使用 if-else 而不是 just if。你有你的 bindView 方法在做:

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

这意味着如果它是真的,但不再是,drawable 不会被重置。做类似的事情:

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

此外,只是一个随机建议,请考虑使用 ViewHolder,因为它会帮助您提高性能。您还可以将光标行转储到ContentValues对象,因此您不必调用 cursor.getString(cursor.getColumnIndex(key))

于 2012-10-16T22:36:54.323 回答