2017-11-01 7 views
0

MainActivity私はListViewにカスタム単一の選択Checkboxesを持っています。各アイテムにはチェックボックスが1つあり、カスタムアダプタクラスでリストに設定されています。ユーザーが1つを選択してダイアログボックスに選択肢の確認をポップアップした後、すべてのチェックボックスを無効にする必要がありますが、現在は選択された行の1つだけが無効になっています。どのように私はすべてを無効にして、選択肢を変更するユーザーの可能性をブロックできますか?リストビューからすべてのチェックボックスを無効にした後

Adapter

public class CustomAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener { 


    SparseBooleanArray mCheckStates; 
    private List<CustomClass> customList; 
    private Context context; 
    private LayoutInflater inflater; 
    private int selectedPosition = -1; 
    private Dialog dialog; 


    public CustomAdapter(Context _context, List<CustomClass> _customList) { 
     inflater = LayoutInflater.from(_context); 
     this.customList = _CustomList; 
     this.context = _context; 
     mCheckStates = new SparseBooleanArray(customList.size()); 
    } 


    @Override 
    public int getCount() { 
     return customList.size(); 
    } 

    @Override 
    public Object getItem(int i) { 
     return i; 
    } 

    @Override 
    public long getItemId(int i) { 
     return i; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 

     CustomClass customClass = customList.get(i); 

     if (view == null) 
      view = inflater.inflate(R.layout.custom_list, null); 


     TextView name = (TextView) view.findViewById(R.id.tv_name); 
     CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox); 

     checkBox.setTag(i); 
     checkBox.setChecked(mCheckStates.get(i, false)); 
     checkBox.setOnCheckedChangeListener(this); 
     checkBox.setChecked(false); 

     if (i == selectedPosition) { 
      checkBox.setChecked(true); 
     } else { 
      checkBox.setChecked(false); 
     } 

     checkBox.setOnClickListener(onStateChangedListener(checkBox, i)); 


     name.setText(customClass.getName()); 


     return view; 

    } 

    private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) { 
     return new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (checkBox.isChecked()) { 
        selectedPosition = position; 

        dialog = new Dialog(context); 
        dialog.setTitle("Accept"); 
        dialog.setContentView(R.layout.dialog_accept); 
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { 
         @Override 
         public void onCancel(DialogInterface dialogInterface) { 

         } 
        }); 
        dialog.show(); 

        Button btnAccept = (Button) dialog.findViewById(R.id.btnVote); 

        btnAccept.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          checkBox.setEnabled(false); 
          dialog.dismiss(); 
         } 
        }); 


       } else { 
        selectedPosition = -1; 

       } 
       notifyDataSetChanged(); 
      } 
     }; 
    } 


    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 


     mCheckStates.put((Integer) buttonView.getTag(), isChecked); 
     notifyDataSetChanged(); 


    } 

CustomClass:

public class CustomClass{ 

    String name; 

    public CustomClass(){} 

    public CustomClass(String name){ 
    this.name = name; 
    } 

    public String getName(){return name;} 
    public void setName(String name){this.name = name;} 

} 

MainActivityListViewAdapter

答えて

0
ListView list = (ListView) findViewById("R.id.list"); 
for(int x = 0; x < list.getItems.length(); x++){ list.items.get(x).setEnabled(false);} 

わからないが、おそらくこのようなものを設定しています。試すだけの価値があります? Getview()

if(customClass.getSelected()){ 
     checkBox.setEnable(true); 
    }else{ 
     checkBox.setEnable(false); 
    } 
+0

いいえ、それは仕事量:( – droho

0
チェックボックス変更リスナーで

private int selectedPosition = 0; 

    private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) { 
      return new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (checkBox.isChecked()) { 
         customClass.setSelected(true); 
         customList.get(selectedPosition).setSelected(false); 
         selectedPosition = position; 

        } 
        notifyDataSetChanged(); 
       } 
      }; 

のみ1つのチェックボックスを選択することができるように、グループ内のすべてのチェックボックスを含めます。

2

関連する問題