2012-04-28 11 views
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; 
} 
} 

あなたがコードを修正するのを助けてくれることを願っています。私の悪い英語を申し訳ありません。

+0

誰か助けてください? – dane

+0

ここで私は答えを見つけることができます – dane

+0

こんにちは、私は解決策を見つけました。これは、まずwidthパラメータを取得してから、プログレスバーに設定することで解決できます。 – dane

答えて

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.

関連する問題