切换按钮不会保持其状态

时间:2014-05-02 05:21:24

标签: android alarmmanager togglebutton

my list view has some toggle button and dates which is to be checked and uncheck on the basis of user choice 
the functionality is to set alarm for those days when button is checked

when i am clicking the toggle button it sets the alarm for the day 
but when i click next button it overwrite it
 **for ex ... let the user has choice to set alarm for  1, 2 3, 4 or all dates and individual toggle button for this if the user has selected 1, 4,5 then it will work for 5 not for all
one more issue which i am unable to figure out is when i scroll the list view and the checked button get disappear it automatically sets it unchecked**
  • 由于某种原因我不能把代码

    公共类DDListAdapter扩展了BaseAdapter {

        ArrayList<DataModelDD> listArray;
        int curIndex=1000, pIndex;
    
        public DDListAdapter(String[] str, String[] str1) {
            listArray = new ArrayList<DataModelDD>();
    
            for (int i=0; i < str.length; i++) {
                listArray.add(new DataModelDD(str[i],str1[i], "Dry Day Alert on " + str[i]));   
            }   
    
    
        }
    
        public void DDListUpdate(String[] str, String[] str1){
            listArray = new ArrayList<DataModelDD>();
            for (int i=0; i < str.length; i++) {
                listArray.add(new DataModelDD(str[i],str1[i], "Dry Day Alert on " + str[i]));   
            }   
            this.notifyDataSetChanged();
        }
    
        @Override
        public int getCount() {
            return listArray.size(); // total number of elements in the list
        }
    
        @Override
        public Object getItem(int i) {
            return listArray.get(i); // single item in the list
        }
    
        @Override
        public long getItemId(int i) {
    
            return i; // index number
    
        }
    
        @Override
        public View getView(final int index, View view, final ViewGroup parent) {
    
            pIndex = index;
    
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.lstdd, parent, false);
    
            final DataModelDD dmFl = listArray.get(index);
    
            final TextView lbl1 = (TextView) view.findViewById(R.id.txtDDate);
            final TextView lbl2 = (TextView) view.findViewById(R.id.txtDres);
    
            lbl1.setText(dmFl.getDDate());
            lbl2.setText(dmFl.getDres());
    
            final ToggleButton btnDShare = (ToggleButton) view.findViewById(R.id.btnDShare);
            final ToggleButton btnlock = (ToggleButton) view.findViewById(R.id.btnDAlarm);
    
    
    
            btnDShare.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                     if(btnDShare.isChecked()){
                    // TODO Auto-generated method stub
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
                    sharingIntent.setType("text/plain");
                    String shareBody = "Dry Day Alert on " + lstMonth.getSelectedItem().toString() + dmFl.getDDate() + " for " + dmFl.getDres();
                    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "DryDayApp");
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    parent.getContext().startActivity(Intent.createChooser(sharingIntent, "Share via"));
                }
                }
            });
    
            btnlock.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                    if(btnlock.isChecked()){
                        final String alarmTime = dmFl.getDDate();
                        final String reason = dmFl.getDres();
                        enableAlarm(buttonView,alarmTime,reason);
                        buttonView.setChecked(true);    
                        astatus = true;
                    }
                    else{
                        final String alarmTime = dmFl.getDDate();
                        disableAlarm(buttonView,alarmTime);
                        buttonView.setChecked(false); 
                        astatus = false;
                    }
    
    
                }
    
    
            });
    
    
            return view;
        }
    }
    

2 个答案:

答案 0 :(得分:0)

这可能对你有帮助......

ArrayList<Boolean> toggleOnArr;
public DDListAdapter(String[] str, String[] str1,ArrayList<Boolean> toggleOnArr) {
    listArray = new ArrayList<DataModelDD>();
   this.toggleOnArr = toggleOnArr;
    for (int i=0; i < str.length; i++) {
        listArray.add(new DataModelDD(str[i],str1[i], "Dry Day Alert on " + str[i]));   
    }   


}

在public View中使用getView(final int position,View convertView,ViewGroup parent)方法使用下面给出的逻辑..

toggleButton.setChecked(toggleOnArr.get(position));
toggleButton.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
     if(toggleOnArr.get(position))
      {
         toggleOnArr.set(postion,false); //means first this togglebutton was on but you clicked to make it off

      }else
         toggleOnArr.set(postion,true);

    notifyDataSetChanged();
 }
});

答案 1 :(得分:0)

警报可能会被覆盖,因为您可能对所有警报使用相同的ID,您必须为每个日期保留单独的ID。

选中的按钮会消失,因为列表视图会刷新。您必须将按钮的状态保持在某个数组中或通过其他方式来避免这种情况。