2013-09-28 92 views
1

我面臨着一個問題,它是關於在列表視圖中檢查檢查項目時我sc the着列表,我不知道該怎麼辦。帶有複選框的Android ListView在檢查時鬆動檢查的項目

這裏是我的自定義接口的代碼:

public class SPCKontrola extends Activity { 
ArrayList<Integer> yourSelectedItems= new ArrayList<Integer>(); 

@Override 
     public void onResume() { 
      super.onResume(); 

      yourSelectedItems.clear(); 

     } 
} 

public class SPCMjereAdapter extends BaseAdapter 
    { 
     private Context context; 

     public SPCMjereAdapter(Context c) 
     {   
      context = c;      
     } 

     public int getCount() { 
      return MyArrList.size(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 
     public View getView(final int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_list_row, null); 

      } 

      TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
      txtOpis.setText(MyArrList.get(position).get("OpisMjere")); 

      TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); 
      txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); 

      CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 
      Chk.setTag(MyArrList.get(position).get("RbMjere")); 



Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {    
        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         if(isChecked){ 
          yourSelectedItems.add(position); 
         }else{ 
          if(yourSelectedItems.contains(position)){            
           yourSelectedItems.remove((position));         
          } 
         } 
        } 
        }); 


      return convertView; 

     } 

}

我應該改變或添加代碼?我找到了一些例子,但我不能重複使用它們並在我的代碼中實現:/

在此先感謝。

+0

https://groups.google.com/forum/#!topic/android-developers/No0LrgJ6q2M。查看Romain Guy的評論。 – Raghunandan

+0

hm ...似乎是一個很好的解決方案,但它擴展了ListActivity。我試圖避免這一點,因爲它會影響幾乎完成的應用程序中的一些事情。 – Josef

回答

0

我覺得我得到的東西。 ..取而代之的Chk.setOnCheckedChangeListener()我改成Chk.setOnClickListener(new OnClickListener()

public class SPCMjereAdapter extends BaseAdapter 
    { 
     private Context context; 


     public SPCMjereAdapter(Context c) 
     {   
      context = c;      
     } 

     public int getCount() { 
      return MyArrList.size(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 
     public View getView(final int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_list_row, null); 

      } 

      TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
      txtOpis.setText(MyArrList.get(position).get("OpisMjere")); 

      TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); 
      txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); 

      final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 


      if(yourSelectedItems.contains((position))){ 
       Chk.setChecked(true); 
      }else{ 
       Chk.setChecked(false); 
      } 

      Chk.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        if (Chk.isChecked()) 
        { 
         yourSelectedItems.add(position); 
        } else 
        { 
         if(yourSelectedItems.contains(position)){            
          yourSelectedItems.remove((position));         
         } 
        } 

       } 
      }); 


      return convertView; 
     } 

} 

現在,滾動向上/向下時工作正常。但還有一個問題,我有點困惑。當滾動和隨機選中/取消項目的程序崩潰,這些錯誤我得到:

FATAL EXCEPTION: main 
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
at java.util.ArrayList.remove(ArrayList.java:399) 
at com.dem.spckontrola.SPCKontrola$SPCMjereAdapter$1.onClick(SPCKontrola.java:830) 
at android.view.View.performClick(View.java:4084) 
at android.widget.CompoundButton.performClick(CompoundButton.java:100) 
at android.view.View$PerformClick.run(View.java:16966) 
at android.os.Handler.handleCallback(Handler.java:615) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method 

確定。我解決了這個加yourSelectedItems.remove(yourSelectedItems.indexOf(position));

所以現在正確的解決辦法是:

public class SPCMjereAdapter extends BaseAdapter 
    { 
     private Context context; 


     public SPCMjereAdapter(Context c) 
     {   
      context = c;      
     } 

     public int getCount() { 
      return MyArrList.size(); 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 
     public View getView(final int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_list_row, null); 

      } 

      TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
      txtOpis.setText(MyArrList.get(position).get("OpisMjere")); 

      TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); 
      txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); 

      final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 


      if(yourSelectedItems.contains((position))){ 
       Chk.setChecked(true); 
      }else{ 
       Chk.setChecked(false); 
      } 

      Chk.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        if (Chk.isChecked()) 
        { 
         yourSelectedItems.add(position); 
        } else 
        { 
         if(yourSelectedItems.contains(position)){            
          yourSelectedItems.remove(yourSelectedItems.indexOf(position));         
         } 
        } 

       } 
      }); 


      return convertView; 
     } 

} 

但現在我有新的問題面對:如何讓所有的檢查,未經檢查的項目列表,其中一些複選框(項目)由於列表視圖中的許多項目不可見,只有向下滾動時纔可見?

5

爲此,你必須保持一個數組變量yourSelectedItems[]用於存儲你有checked..and把這樣的情況在你getView()方法的視圖位置..

​​

你可以管理你的陣列在getView()編寫這個方法..

check.setOnCheckedChangeListener(new OnCheckedChangeListener() {    
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if(isChecked){ 
      yourSelectedItems.add(position); 
     }else{ 
      if(yourSelectedItems.contains(position)){            
       yourSelectedItems.remove((position));         
      } 
     } 
    }); 
+0

不幸的是,這不起作用。如果我選擇第一個項目而不是向下滾動列表,它會自動檢查其他項目。如果我回到列表的首位,那麼該項目不再被檢查。 – Josef

+0

它不應該像那樣......你可以發佈你改變後的代碼......? – Vikram

+0

我剛剛添加ArrayList yourSelectedItems = new ArrayList (); 並按照您的建議對getView()進行更改。 – Josef

0

如果你在你的listView.onItemSelectedListner中使用了getView,你將會面臨這個問題 listView並沒有把每個對象放在不同的視圖中,這就是爲什麼當你調用getView(2)它會返回列表的第二個對象的視圖屏幕沒有列表的第二個對象的視圖上...你必須使用你的類

1

一些布爾值或複選框爲了避免在ListView的CheckBox 的問題,你可以採取一個布爾 實現它自己數組初始化爲假 ,然後在數組 中確定 對應的位置,其中複選框被選中在ListView中。這不會引起麻煩的複選框的選中狀態,當你向前和向後移動的應用程序或滾動ListView.You可以查看這裏的示例代碼:

Android checkbox multiselected issue

1

這發生在我身上,我解決這個wtih ,

1.創建一個全局布爾型數組列表並在構造函數中用MyArrList.size()填充它。

2.當您更改複選框值設置視圖索引arratlist。

3.Change這樣的:

CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 
Chk.setTag(MyArrList.get(position).get("RbMjere")); 

與此:

CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 
Chk.setTag(MyArrList.get(position).get("RbMjere")); 
Chk.setChecked(commentTexts.get(position)); 
1

改變你的getview()這一個...

 public View getView(final int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_list_row, null); 

      } 

      TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
      txtOpis.setText(MyArrList.get(position).get("OpisMjere")); 

      TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode); 
      txtRbMjere.setText(MyArrList.get(position).get("RbMjere")); 

      CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk); 


      if(yourSelectedItems.contains((position))){ 
       Chk.setChecked(true); 
      }else{ 
       Chk.setChecked(false); 
      } 

      Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {    
        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         if(isChecked){ 
          yourSelectedItems.add(position); 
         }else{ 
          if(yourSelectedItems.contains(position)){            
           yourSelectedItems.remove((position));         
          } 
         } 
        } 
        }); 


      return convertView; 

     } 
+0

不幸的是,這不會工作... :(再次有時丟失檢查項目的狀態,有時斷行yourSelectedItems.remove( (位置))setOnCheckedChangeListener() – Josef