2016-10-26 23 views
0

複数のアイテム、つまりチェックボックス付きのスピンナを選択できるスピナーを作成するにはどうすればよいですか?同時に、私はナビゲーション・ドロワ・アクティビティの断片の中でこの複数の選択肢のスピナを使用する必要があります。複数選択肢のAndroid Spinnerの使い方ナビゲーションの引き出し引き出しアクティビティ

誰かが適切なサンプルコードを使用して疑いを明確にすることができます。

ありがとうございます!私は、私たちに複数の選択肢のスピナーをspinner2を変更する必要が

public class Doctor extends Fragment { public Doctor() { // Required empty public constructor }

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    getActivity().setTitle("Doctor"); 
    View v = inflater.inflate(R.layout.fragment_doctor, container, false); 
    String [] values = 
      {"All Town","Paris","Kodambakkam","Nungambakkam","T.Nagar","Egmore"}; 
    Spinner spinner = (Spinner) v.findViewById(R.id.town); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values); 
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
    spinner.setAdapter(adapter); 

    String [] values1 = 
      {"Select Doctor","Doctor1","Doctor2","Doctor3","Doctor4"}; 
    Spinner spinner1 = (Spinner) v.findViewById(R.id.doctor); 
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1); 
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
    spinner1.setAdapter(adapter1); 

    String [] values2 = 
      {"Worked With","Nagaraj","Muthuvel"}; 
    Spinner spinner2 = (Spinner) v.findViewById(R.id.doctor); 
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values2); 
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
    spinner2.setAdapter(adapter2); 

    return v; 
} 
public void onViewCreated(View view,@Nullable Bundle savedInstanceState){ 
    super.onViewCreated(view, savedInstanceState); 

} 

}

。私はFragmentからクラスを拡張しています。したがって、この複数の選択肢スピナー

+0

あなたのDrawerLayoutでNavigationViewを使用しています – YLS

+0

まだこれに関するコードはありません。私はこれまで何をしようと、私は断片のJavaでエラーメッセージが表示されます。 –

+0

はい私はDrawerLayoutでNavigationViewを使用しています。 –

答えて

1
How do I create spinner which allows to choose multiple items, i.e spinner with check boxes? 

を処理する方法がわからないmultiselectionspinnerを使用してみてください。 https://stackoverflow.com/a/6022474/6616489

とナビゲーション引き出しのフラグメント内部

複数選択スピナーのため

は、上記のコードhttps://stackoverflow.com/a/20017862/6616489

+0

「Class Doctor extends Fragment、SpinnerはDialogInterface.OnMultiChoiceClickListenerを実装します」という使い方を教えてください。 このエラーを取得する... –

+0

エラーを説明してください –

+0

また、達成したいことの写真をもらえますか?提案するほうが簡単でしょう –

0
public class Doctor extends Fragment{ 

MultiSelectionSpinner spinner2; 
FragmentManager fm = getFragmentManager(); 
public Doctor() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    getActivity().setTitle("Doctor"); 
    View v = inflater.inflate(R.layout.fragment_doctor, container, false); 

    String [] values = 
      {"All Town","Paris","Kodambakkam","Nungambakkam","T.Nagar","Egmore"}; 
    Spinner spinner = (Spinner) v.findViewById(R.id.town); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values); 
    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
    spinner.setAdapter(adapter); 

    String [] values1 = 
      {"Select Doctor","Doctor1","Doctor2","Doctor3","Doctor4"}; 
    Spinner spinner1 = (Spinner) v.findViewById(R.id.se_doctor); 
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1); 
    adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
    spinner1.setAdapter(adapter1); 

    String [] values2 = 
      {"Worked With","Nagaraj","Muthuvel"}; 
    // Multi spinner 
    spinner2 = (MultiSelectionSpinner)v.findViewById(R.id.work); 
    spinner2.setItems(values2);return v; 
}} 

を参照してくださいは、複数の選択肢のスピナーを保持している(注:Spinner2)

public class MultiSelectionSpinner extends Spinner implements 
    OnMultiChoiceClickListener { 
String[] _items = null; 
boolean[] mSelection = null; 

ArrayAdapter<String> simple_adapter; 

public MultiSelectionSpinner(Context context) { 
    super(context); 

    simple_adapter = new ArrayAdapter<String>(context, 
      android.R.layout.simple_spinner_item); 
    super.setAdapter(simple_adapter); 
} 

public MultiSelectionSpinner(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    simple_adapter = new ArrayAdapter<String>(context, 
      android.R.layout.simple_spinner_item); 
    super.setAdapter(simple_adapter); 
} 

public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
    if (mSelection != null && which < mSelection.length) { 
     mSelection[which] = isChecked; 

     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } else { 
     throw new IllegalArgumentException(
       "Argument 'which' is out of bounds."); 
    } 
} 

@Override 
public boolean performClick() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
    builder.setMultiChoiceItems(_items, mSelection, this); 
    builder.show(); 
    return true; 
} 

@Override 
public void setAdapter(SpinnerAdapter adapter) { 
    throw new RuntimeException(
      "setAdapter is not supported by MultiSelectSpinner."); 
} 

public void setItems(String[] items) { 
    _items = items; 
    mSelection = new boolean[_items.length]; 
    simple_adapter.clear(); 
    simple_adapter.add(_items[0]); 
    Arrays.fill(mSelection, false); 
} 

public void setItems(List<String> items) { 
    _items = items.toArray(new String[items.size()]); 
    mSelection = new boolean[_items.length]; 
    simple_adapter.clear(); 
    simple_adapter.add(_items[0]); 
    Arrays.fill(mSelection, false); 
} 

public void setSelection(String[] selection) { 
    for (String cell : selection) { 
     for (int j = 0; j < _items.length; ++j) { 
      if (_items[j].equals(cell)) { 
       mSelection[j] = true; 
      } 
     } 
    } 
} 

public void setSelection(List<String> selection) { 
    for (int i = 0; i < mSelection.length; i++) { 
     mSelection[i] = false; 
    } 
    for (String sel : selection) { 
     for (int j = 0; j < _items.length; ++j) { 
      if (_items[j].equals(sel)) { 
       mSelection[j] = true; 
      } 
     } 
    } 
    simple_adapter.clear(); 
    simple_adapter.add(buildSelectedItemString()); 
} 

public void setSelection(int index) { 
    for (int i = 0; i < mSelection.length; i++) { 
     mSelection[i] = false; 
    } 
    if (index >= 0 && index < mSelection.length) { 
     mSelection[index] = true; 
    } else { 
     throw new IllegalArgumentException("Index " + index 
       + " is out of bounds."); 
    } 
    simple_adapter.clear(); 
    simple_adapter.add(buildSelectedItemString()); 
} 

public void setSelection(int[] selectedIndicies) { 
    for (int i = 0; i < mSelection.length; i++) { 
     mSelection[i] = false; 
    } 
    for (int index : selectedIndicies) { 
     if (index >= 0 && index < mSelection.length) { 
      mSelection[index] = true; 
     } else { 
      throw new IllegalArgumentException("Index " + index 
        + " is out of bounds."); 
     } 
    } 
    simple_adapter.clear(); 
    simple_adapter.add(buildSelectedItemString()); 
} 

public List<String> getSelectedStrings() { 
    List<String> selection = new LinkedList<String>(); 
    for (int i = 0; i < _items.length; ++i) { 
     if (mSelection[i]) { 
      selection.add(_items[i]); 
     } 
    } 
    return selection; 
} 

public List<Integer> getSelectedIndicies() { 
    List<Integer> selection = new LinkedList<Integer>(); 
    for (int i = 0; i < _items.length; ++i) { 
     if (mSelection[i]) { 
      selection.add(i); 
     } 
    } 
    return selection; 
} 

private String buildSelectedItemString() { 
    StringBuilder sb = new StringBuilder(); 
    boolean foundOne = false; 

    for (int i = 0; i < _items.length; ++i) { 
     if (mSelection[i]) { 
      if (foundOne) { 
       sb.append(", "); 
      } 
      foundOne = true; 

      sb.append(_items[i]); 
     } 
    } 
    return sb.toString(); 
} 

public String getSelectedItemsAsString() { 
    StringBuilder sb = new StringBuilder(); 
    boolean foundOne = false; 

    for (int i = 0; i < _items.length; ++i) { 
     if (mSelection[i]) { 
      if (foundOne) { 
       sb.append(", "); 
      } 
      foundOne = true; 
      sb.append(_items[i]); 
     } 
    } 
    return sb.toString(); 
}} 

上記のコードは、spinner2から呼び出されているMulti Selection Spinnerを作成するためのコードです。

コードを確認してください。ご不明な点がございましたら、ここでコメントしてください。

関連する問題