【问题标题】:ListView displays erratic dataListView 显示不稳定的数据
【发布时间】:2012-10-16 22:31:04
【问题描述】:

我正在使用 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);

【问题讨论】:

  • 考虑使用Cursor#getInt(),而不是要求将整数作为字符串,然后将其解析回整数...

标签: android android-listview android-cursoradapter android-progressbar android-cursor


【解决方案1】:

任何时候使用适配器,都应该使用 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))

【讨论】:

  • "只是一个随机的建议,考虑使用 ViewHolder" 这不是随机的,这是一个很好的建议!此外,适配器应存储getColumnIndex() 的每个值,无需在任何列上多次调用它。 (虽然我可能不会使用 ContentValues,但我认为更直接地访问光标。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-11
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多