滚动时ListView中的进度条问题

时间:2012-04-28 17:13:48

标签: android listview scroll progress-bar

我正在实施列表视图,以显示由其团队分隔的成员。有进度条显示每个成员的工作重量以及他们到目前为止完成了多少。列表似乎显示正确,但当我向上和向下滚动时,进度条意外更改。这是代码。

public class MyCursorAdapter extends CursorAdapter{

private static final int STATE_UNKNOWN = 0;

private int requiredJob;
private static final int STATE_SECTIONED_CELL = 1;
private static final int STATE_REGULAR_CELL = 2;
private int[] mCellStates;

public MyCursorAdapter(Context context, Cursor cursor, int requiredJob) {
    super(context, cursor);
    mCellStates = cursor == null ? null : new int[cursor.getCount()];
    this.requiredJob=requiredJob;
}

@Override
public void changeCursor(Cursor cursor) {
    super.changeCursor(cursor);
    mCellStates = cursor == null ? null : new int[cursor.getCount()];
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    int memJob, memDone;
    final  ViewHolder holder= (ViewHolder)view.getTag();

    boolean needSeparator = false;
    final int position = cursor.getPosition();

    String team= cursor.getString(cursor.getColumnIndex(DBAdapter.COL_TEAM));

    switch (mCellStates[position]) {
        case STATE_SECTIONED_CELL:
            needSeparator = true;
            break;

        case STATE_REGULAR_CELL:
            needSeparator = false;
            break;

        case STATE_UNKNOWN:
        default:
            if (position == 0) {
                needSeparator = true;
            } else {

                cursor.moveToPosition(position-1);
                String str=cursor.getString(cursor.getColumnIndex(DBAdapter.COL_TEAM));
                if(!str.equalsIgnoreCase(String.valueOf(team))) {
                    needSeparator=true;
                }

                cursor.moveToPosition(position);
            }

            mCellStates[position] = needSeparator ? STATE_SECTIONED_CELL : STATE_REGULAR_CELL;
            break;
    }

    if (needSeparator) {
        holder.separator.setText(team);
        holder.separator.setVisibility(View.VISIBLE);
    } else{
        holder.separator.setVisibility(View.GONE);
    }

    holder.member.setText(cursor.getString(cursor.getColumnIndex(DBAdapter.COL_MEMBER)));

    memJob=cursor.getInt(cursor.getColumnIndex(DBAdapter.COL_MEMBER_JOB));
    memDone=(int)cursor.getInt(cursor.getColumnIndex(DBAdapter.COL_MEMBER_JOB_DONE));
    holder.pBar.getLayoutParams().width=(int)(400*memJob/requiredJob);
    holder.pBar.setMax(memJob);
    holder.pBar.setProgress(memDone);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View v=LayoutInflater.from(context).inflate(R.layout.row, parent, false);
    ViewHolder holder= new ViewHolder();
    holder.separator=(TextView)v.findViewById(R.id.sep_team);
    holder.member=(TextView)v.findViewById(R.id.member);
    holder.pBar=(MyProgressBar)v.findViewById(R.id.pBar);

    v.setTag(holder);
    return v;
}
}

希望你们能帮助我修复代码。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

尝试更改:

memJob=cursor.getInt(cursor.getColumnIndex(DBAdapter.COL_MEMBER_JOB));
memDone=(int)cursor.getInt(cursor.getColumnIndex(DBAdapter.COL_MEMBER_JOB_DONE));
holder.pBar.getLayoutParams().width=(int)(400*memJob/requiredJob);
holder.pBar.setMax(memJob);
holder.pBar.setProgress(memDone);

要:

memJob=cursor.getInt(cursor.getColumnIndex(DBAdapter.COL_MEMBER_JOB));
memDone=(int)cursor.getInt(cursor.getColumnIndex(DBAdapter.COL_MEMBER_JOB_DONE));
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.pBar);
progressBar.getLayoutParams().width=(int)(400*memJob/requiredJob);
progressBar.setMax(memJob);
progressBar.setProgress(memDone);

这将直接从bindView中获取实际进度条,并希望能够解决您的问题。您可以删除holder.pBar=(MyProgressBar)v.findViewById(R.id.pBar); from newView.