0

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

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;
}
}

希望你们能帮我修复代码。对不起,我的英语不好。

4

1 回答 1

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.

于 2012-05-20T19:11:06.433 回答