0

我有一个 listView 有 checkTextView 和一个 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;
}

}

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

4

1 回答 1

0

Android尽量重用View

checkedTextView的设置是正确的,因为你设置它不考虑条件

textView但是您只有在单击时才会更改文本,checkedTextView因此您textView可能会附带已经设置的文本(因为 Android 正在重用视图)

于 2013-02-20T17:29:27.120 回答