2012-04-28 22 views
0

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

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

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

+0

有人帮我吗? – dane 2012-05-07 13:09:08

+0

我在哪里可以找到答案 – dane 2012-05-16 20:44:57

+0

嗨,我找到了解决方案。这可以通过先获取宽度参数,然后将其设置为进度条来解决。 – dane 2012-05-25 08:50:56

回答

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.