2013-11-22 101 views
0

Iam新增至android。我有一個在列表視圖中的切換按鈕,默認情況下它應該顯示狀態!但它顯示和單擊按鈕狀態更改但滾動視圖狀態不保留。滾動列表視圖上的Android切換按鈕狀態更改

我的列表視圖中的樣子如下: 比爾沒有1234 預定日期2013年6月13日 發票號碼加載的123 港口DF 目的港YS的 警報切換按鈕選中即關閉狀態

我listviewadapter類

private class listviewAdapter extends BaseAdapter 
    { 
     public ArrayList<HashMap<String,String>> list; 
     Activity activity; 
     SparseBooleanArray mSparseBooleanArray; 

     public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) 
     { 
      // TODO Auto-generated constructor stub 
      super(); 
      this.activity = activity; 
      this.list = list; 
      mSparseBooleanArray = new SparseBooleanArray(list.size()); 
     } 

     public int getCount() { 
      // TODO Auto-generated method stub 
      return list.size(); 
     } 

     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return list.get(position); 
     } 

     public long getItemId(int arg0) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 



     public View getView(int position, View convertView, ViewGroup parent) 
     { 
      // TODO Auto-generated method stub 
      ListViewHolder viewHolder; 
      LayoutInflater inflater = activity.getLayoutInflater(); 
      final int pos = position; 
      if (convertView == null) 
      { 
       convertView=inflater.inflate(R.layout.listview_row_list1,null); 
       txtFirst=(TextView) convertView.findViewById(R.id.FirstText); 
       txtSecond=(TextView) convertView.findViewById(R.id.SecondText); 
       togglbtn=(ToggleButton) convertView.findViewById(R.id.tglbtn1); 
       togglbtn.setTag(position); 
       togglbtn.setChecked(mSparseBooleanArray.get(position)); 
       togglbtn.setOnCheckedChangeListener(mCheckedChangeListener); 
       convertView.setTag(new ListViewHolder(txtFirst,txtSecond,togglbtn)); 

      } 
      else 
      { 
       viewHolder = (ListViewHolder) convertView.getTag(); 
       txtFirst = viewHolder.getText1(); 
       txtSecond = viewHolder.getText2(); 
       togglbtn = viewHolder.getButton(); 



      } 

      HashMap<String, String> map = list.get(position); 
     // count=count+6; 
      txtFirst.setTypeface(font1); 
      txtFirst.setText(map.get(FIRST_COLUMN)); 
      txtSecond.setTypeface(font2); 
      txtSecond.setText(map.get(SECOND_COLUMN)); 
      String data= map.get(FIRST_COLUMN); 
      String value_data=map.get(SECOND_COLUMN); 

       if(data.trim().equals("HBL NO")) 
       { 
        convertView.setBackgroundResource(R.color.LightBlue); 
       } 
       else 
       { 
        convertView.setBackgroundColor(Color.WHITE); 
       } 

       if(data.trim().equals("ALERT")) 
       { 
        togglbtn.setVisibility(View.VISIBLE); 

        /*if(value_data.trim().equals("YES")) 
        { 

         togglbtn.setChecked(true); 
        } 
        else 
        { 

         togglbtn.setChecked(false); 
        }*/ 
       } 
       else 
       { 
        togglbtn.setVisibility(View.GONE); 
       } 





      return convertView; 

     } 

    public boolean isChecked(int position) { 
      return mSparseBooleanArray.get(position, false); 
     } 

     public void setChecked(int position, boolean isChecked) { 
      mSparseBooleanArray.put(position, isChecked); 

     } 

     public void toggle(int position) { 
      setChecked(position, !isChecked(position)); 

     } 

     OnCheckedChangeListener mCheckedChangeListener=new OnCheckedChangeListener() { 

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       // TODO Auto-generated method stub 
       System.out.println("Tag: "+buttonView.getTag()); 
       mSparseBooleanArray.put((Integer) buttonView.getTag(), 
         isChecked); 
       System.out.println("Status: "+isChecked); 

      } 
     }; 


    } 

    private static class ListViewHolder 
    { 
     private TextView txt1 ; 
     private TextView txt2 ; 
     private ToggleButton btn; 
     int ref; 

     //public ListViewHolder() {} 
     public ListViewHolder(TextView tx1,TextView tx2,ToggleButton btn) { 
      this.txt1=tx1 ; 
      this.txt2=tx2 ; 
      this.btn=btn; 
     } 
     public TextView getText1() { 
      return txt1; 
     } 
     public TextView getText2() { 
      return txt2; 
     } 

     public ToggleButton getButton() 
     { 
      return btn; 
     } 
    } 
+0

這是很多代碼。請縮小到您認爲問題發生的地方。 – PoweredByOrange

+0

看起來像你在Sparse中的boolean值...將狀態改變爲** true ** –

回答

1

只需添加在您的適配器類這兩種方法它會正常工作

 @Override 
     public int getViewTypeCount() {     
      return yourlist.size(); 
     } 
     @Override 
     public int getItemViewType(int pos) 
      return pos; 
     } 
相關問題