2015-09-19 63 views
0

我有一個listview佈局,每行包含一個複選框。 當活動開始時,它顯示覆選框,其中一些默認選中。 最初,我得到正確選中的複選框。但是當我想取消選擇一些並檢查其他人時, 他們都會在我收到消息'你不能超過5個選擇!'後取消選中!來自自定義適配器。 我希望能夠檢查新盒子或取消選中一些已經從開始檢查的盒子。 的複選框的總數應小於5listview組成的複選框

佈局:single_row_item

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkBox" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cinteretidtxt" 
     android:visibility="gone"/> 
</LinearLayout> 

定義適配器:ModCinteretsAdapter

public class ModCinteretsAdapter extends ArrayAdapter<ModCinteret>{ 
    private ArrayList<String> selectedStrings = new ArrayList<String>(); 
    private ArrayList<String> selectedids = new ArrayList<String>(); 

    public ModCinteretsAdapter(Context context, ArrayList<ModCinteret> cinterets) { 
     super(context, 0, cinterets); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     ModCinteret cinteret = getItem(position); 
     // Check if an existing view is being reused, otherwise inflate the view 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_row_item, parent, false); 
     } 
     // Lookup view for data population 
     final TextView cinteretidtxt = (TextView) convertView.findViewById(R.id.cinteretidtxt); 
     cinteretidtxt.setText(cinteret.getId()); 
     final CheckBox cinterettxt = (CheckBox) convertView.findViewById(R.id.checkBox); 
     cinterettxt.setText(cinteret.getText()); 

     if((cinteret.getFlag()).equals("1")) { 
      cinterettxt.setChecked(true); 
      selectedStrings.add(cinterettxt.getText().toString()); 
      selectedids.add(cinteretidtxt.getText().toString()); 
     }else { 
      cinterettxt.setChecked(false); 
      selectedStrings.remove(cinterettxt.getText().toString()); 
      selectedids.remove(cinteretidtxt.getText().toString()); 
     } 
     cinterettxt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        if(selectedStrings.size()<5) { 
         selectedStrings.add(cinterettxt.getText().toString()); 
         selectedids.add(cinteretidtxt.getText().toString()); 
        }else{ 
         cinterettxt.setChecked(false); 
         Toast.makeText(getContext(),"You cannot have more than 5 choices!",Toast.LENGTH_LONG).show(); 
        } 
       }else{ 
        selectedStrings.remove(cinterettxt.getText().toString()); 
        selectedids.remove(cinteretidtxt.getText().toString()); 
       } 

      } 
     }); 


     return convertView; 
    } 

    public ArrayList<String> getSelectedString(){ 
     return selectedStrings; 
    } 

    public ArrayList<String> getSelectedId(){ 
     return selectedids; 
    } 
} 
+0

嗨,你不應該在getView中添加點擊監聽器。意見得到回收,一切都會混淆。 –

+0

嗨Jawad。我認爲getView裏面的點擊監聽器是完全可以的。我從網上讀了一些例子 –

+0

也許我不知道。你什麼時候更新cinteret的旗幟? –

回答

0

默認爲你的複選框添加一個全局靜態變量在您的適配器。

static int nbChecked = 5; 

在你的ModCinteret中添加一個布爾值。

boolean isChecked; 

而不是使用您的標誌,添加。

if(cinteret.getIsChecked()){ 
    cinterettxt.setChecked(true); 
} 
else{ 
    cinterettxt.setChecked(false); 
} 

最後,在你的聽衆變化onCheckedChanged到:

if(!isChecked){ 
    cinteret.setIsChecked(false); 
    nbChecked--; 
    notifyDataSetChanged(); 
} 
else{ 
    if(nbChecked == 5){ 
     Toast.makeText(getContext(),"You cannot have more than 5 choices!",Toast.LENGTH_LONG).show(); 
    } 
    else{ 
     cinteret.setIsCheck(true); 
     nbChecked++; 
     notifyDataSetChanged(); 
    } 
} 

我覺得應該這樣做。

+0

而當你創建你的列表視圖時,除了你的默認設置,你的cinteret.checked狀態設置爲false。 –

+0

我仍然有同樣的問題。我希望能夠從列表視圖被聲明的活動中獲得已選框的列表 –

0

商店的每個複選框的狀態an Array

Declare一個布爾值數組,並使用索引存儲的特定Chekcbox

值,並設置基於該

SparseBooleanArray sba=new SparseBooleanArray();