Android ListView在getView()中返回不正确的位置

Android ListView在getView()中返回不正确的位置,android,Android,我有一个列表视图,其中checkedTextView和一个textView作为每个textView中的项。checkedTextView值将从数据库表中填充。对于每个值,数据库中有一列的计时器值为10,15,30。选中checkedTextView后,我会检查计时器列中是否有值,如果存在,我会设置textView,并每秒更新计时器值。问题在于,如果第一个元素具有计时器值,则textView将填充计时器mm:ss值。当我向下滚动时,会有一些其他listView项填充了相同的值 这是因为位置返回了错

我有一个列表视图,其中checkedTextView和一个textView作为每个textView中的项。checkedTextView值将从数据库表中填充。对于每个值,数据库中有一列的计时器值为10,15,30。选中checkedTextView后,我会检查计时器列中是否有值,如果存在,我会设置textView,并每秒更新计时器值。问题在于,如果第一个元素具有计时器值,则textView将填充计时器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;
}
公共类方法LazyAdapter扩展了BaseAdapter{
活动;
私有字符串[]准备步骤;
私有字符串[]timeToPrepare;
私有字符串[]箭头值;
私有int[]noOfStepsFlag;
字符串recipeID;
列表视图列表;
意图;
语境;
整数分钟;
int noOfSteps;
private HashMap mIsChecked=new HashMap();
private ArrayList itemChecked=新建ArrayList();
专用静态充气机=空;
媒体播放器;
字符串振动器服务;
振动器;
public MethodLazyAdapter(活动a,字符串[]准备步骤,字符串[]时间准备,字符串[]箭头值,int noOfSteps,列表视图列表){
本活动=a;
this.preparationSteps=preparationSteps;
this.timeToPrepare=timeToPrepare;
此.arrowValue=arrowValue;
this.list=list;
this.noOfSteps=noOfSteps;
noOfStepsFlag=newint[noOfSteps];

对于(inti=0;iAndroid,请尽可能多地重用视图

您的
checkedTextView
设置正确,因为您设置它与条件无关


但是,如果单击
checkedTextView
,您只能更改
textView
文本,因此您的
textView
可能已经设置了一个文本(因为Android正在重用一个视图)

xml中的textView最初没有设置为任何值。我在单击checkedTextView后设置了该值。当我滚动时,下面的一些列表元素中的textView设置不正确。是的,适配器为ListView中的某个项目设置了文本,比如说“Obiwan”,然后重新使用此视图:如果不更改文本值,它仍然是“欧比旺”: