开发者

Regarding the Checked state of a checkbox inside a list view

开发者 https://www.devze.com 2023-02-11 10:07 出处:网络
I am developing a simple application,in which i am displaying a check Box and a text in each line of a list,but while i am selecting or checking one check box some other check boxes inside my list vie

I am developing a simple application,in which i am displaying a check Box and a text in each line of a list,but while i am selecting or checking one check box some other check boxes inside my list view are checked automatically. I have done a lot Google on it and unable to find a way to fix this... Please help me....

Thanx in advance....

 public void checkevent(View v) throws ParseException
{
    lv=(ListView)v.getParent().getParent();

    int i开发者_开发技巧=lv.getPositionForView(v);

    Object o = lv.getItemAtPosition(i);

    String item= o.toString();

    int q=item.indexOf("-",0);

    String alarm_date=new_date+" "+item.substring(0,q);

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    Date rdate = (Date)formatter.parse(alarm_date); 

    set_prog=item.substring(q+1,item.length())+" on "+item.substring(0,q);

    if(((CompoundButton) v).isChecked())
    {
    if(rdate.getTime()>=new Date().getTime())
    {
        if(program_reminder_main.contains((alarm_date+"--"+item.substring(q+1,item.length()))))
        {
            Toast.makeText(this,"You have already selected this one", Toast.LENGTH_LONG).show();
            ((CompoundButton) v).setChecked(false);

        }
        else {
            program_reminder_main+=("~"+alarm_date+"--"+item.substring(q+1,item.length()));

        }
        //To call the Alarm service Class at respective time
    Toast.makeText(this, "Alarm Set For"+" "+item.substring(q+1,item.length())+" on "+item.substring(0,q), Toast.LENGTH_LONG).show();
    Intent myIntent = new Intent(MainClass.this, MyAlarmService_Movie.class);

    pendingIntent = PendingIntent.getService(MainClass.this, 0, myIntent, 0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

         alarmManager.set(AlarmManager.RTC_WAKEUP,rdate.getTime()-30000,pendingIntent);

}
else {
 ((CompoundButton) v).setChecked(false);

        Toast.makeText(MainClass.this, "Alarm can't be set Before to crrent time", Toast.LENGTH_LONG).show();

    }
}
    else if(!((CompoundButton) v).isChecked()){
        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        Toast.makeText(MainClass.this, "Reset Alarm", Toast.LENGTH_LONG).show();
    }

}

ignore the extra things... just concentrate on the code for check box...and for your info. i have set the above method in the check Box's properties...


The problem is that list reuses your view .

Let's say view1 is first associated with id 1 and you check it. Then, you scroll and view1 is converted to display id 10. But since the view isn't recreated, it will still have the checkbox state from the item with id 1.

So basically the solution is to store somewhere which items are selected and in our getView method, you would do

 ckeckbox.setChecked(isItemChecked(position));

And you have to implement isItemChecked(int position);

An inefficient but working implementation would be to use an array of boolean in your activity, let's say

 boolean[] itemChecked;

And then in getView, you set a listener on checkbox checked . You also set the checked state using the array.

  checkbox .setOnCheckedChangeListener (new OnCheckedChangeListener () {
  public void onCheckedChanged (CompoundButton btn, boolean isChecked) {
  itemChecked[position] = isChecked;
}
});

checkbox .setChecked(itemChecked[position]);


checkbox state should be saved

Declare a boolean itemChecked and save the check box state in it

boolean[] itemChecked =  new boolean[100];  
public View getView(int pos, View inView, ViewGroup parent) {     

 final CheckBox checkBox = (CheckBox) v.findViewById(R.id.bcheck); 

 checkBox.setTag(pos);  checkBox.setOnClickListener(new OnClickListener()
 {     
 @Override    
    public void onClick(View arg0)
{     
         String position = (String) checkBox.getTag();      

 if (checkBox.isChecked() == true) {       

 itemChecked[Integer.valueOf(position)] = checkBox.isChecked();     

  } else {  

    itemChecked[Integer.valueOf(position)] = checkBox.isChecked();       
 }          }  }); 

checkBox.setChecked(itemChecked[pos]);  

  return (v);  } 

REFER : Automatic selection of checkboxes inside listview - android


in my opinion,

You should look carefull in :

method: getView(..) and onClick()

Good Luck

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号