ListView在getView()中返回错误的位置

时间:2013-02-20 17:22:15

标签: android

我有一个listView,在每个textView中都有checkedTextView和textView作为项目。 checkedTextView值将从数据库表中填充。对于每个值,在数据库中有一个具有计时器值10,15,30的列。检查checkedTextView后,检查timer列中是否有值,如果存在,我将textView设置为每秒更新一次计时器值。问题是如果第一个元素有计时器值,则textView get填充计时器mm:ss值。当我向下滚动时,会有一些其他listView项填充相同的值不正确。

这是因为该位置返回了错误的值?当我们滚动时,我已经读过某个地方视图刷新了。例如:如果项目1填充了值,当我滚动时,我发现元素6也具有此值

以下是适配器类

public class MethodLazyAdapter extends BaseAdapter {

Activity activity;
private String[] preparationSteps;
private String[] timeToPrepare;
private String[] arrowValue;
private int [] noOfStepsFlag;
String recipeID;
ListView list;
Intent intent;
Context context;
int minutes;
int noOfSteps;
private HashMap<Integer, Boolean> mIsChecked = new HashMap<Integer, Boolean>();
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

private static LayoutInflater inflater=null; 
MediaPlayer player;
String vibratorService;
Vibrator vibrator;



public MethodLazyAdapter(Activity a,String[] preparationSteps,String [] timeToPrepare,String [] arrowValue,int noOfSteps,ListView list) {
    this.activity = a;
    this.preparationSteps = preparationSteps;
    this.timeToPrepare= timeToPrepare;
    this.arrowValue=arrowValue;
    this.list=list;
    this.noOfSteps = noOfSteps;

    noOfStepsFlag = new int[noOfSteps];
    for(int i=0;i<noOfSteps;i++){
        noOfStepsFlag[i]=0;
        itemChecked.add(i, false);
    }

    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.method_item, null);

    final CheckedTextView checkedTextView = (CheckedTextView)vi.findViewById(R.id.checkedTextView);
    checkedTextView.setText(preparationSteps[position]);

    final TextView textView = (TextView)vi.findViewById(R.id.textView1);

    final AlertDialog.Builder alert = new AlertDialog.Builder(activity);


    checkedTextView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            ((CheckedTextView) v).toggle();

            if(checkedTextView.isChecked())
                itemChecked.set(position, true);
            else if(!checkedTextView.isChecked())
                itemChecked.set(position,false);

            if(!timeToPrepare[position].equals("NA")  && checkedTextView.isChecked() && noOfStepsFlag[position]==0){
                noOfStepsFlag[position]=1;
                playAudio();
                new CountDownTimer(Integer.parseInt(timeToPrepare[position])*60*1000, 500) { //the timer runs for 30 seconds in this case
                     public void onTick(long leftTimeInMilliseconds) {
                         long seconds = leftTimeInMilliseconds / 1000;
                         textView.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));

                     }

                     public void onFinish() {
                         textView.setText("Done");
                         alert.setTitle("Done!!");
                         alert.setMessage(arrowValue[position]);
                         alert.setIcon(R.drawable.savai_upma);
                         playAudio();
                         vibratorService = Context.VIBRATOR_SERVICE;
                         vibrator = (Vibrator)activity.getSystemService(vibratorService);
                         vibrator.vibrate(1000); //vibrate for 1sec.

                         alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                             public void onClick(DialogInterface dialog, int which) {
                                // Some code
                             }

                          });
                         alert.show();
                     }
                  }.start();

            }
        }

        private void playAudio() {
            // TODO Auto-generated method stub
            player = MediaPlayer.create(activity, R.raw.facebook_ringtone_pop);
            player.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer player) {
                    // TODO Auto-generated method stub
                    player.release();                           
                }
            });
            player.start();
        }
    });

    /*
     * The checkedTextView click state is saved in array itemChecked when the row is clicked and by default its set 
     * to false.
     * Outside onClick the checkTextView row will be checked or unchecked from the 
     * value stored in array itemChecked.
     */
    checkedTextView.setChecked(itemChecked.get(position));

    return vi;
}

public int getCount() {
    return preparationSteps.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

}

checkedTextView正在正确切换,但textView值填充不正确。请帮忙。

1 个答案:

答案 0 :(得分:0)

Android尝试尽可能多地重复使用

您的checkedTextView设置正确,因为您设置的不是条件

但是,如果您点击textView,则只会更改checkedTextView文字,以便textView可能附带已设置的文字(因为Android正在重新使用视图)