Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. final String[] select_qualification = {
  2. "Todos", "1", "2", "3", "4",
  3. "5", "6"};
  4. Spinner spinner = (Spinner) findViewById(R.id.spinner);
  5.  
  6. ArrayList<StateVO> listVOs = new ArrayList<>();
  7.  
  8. for (int i = 0; i < select_qualification.length; i++) {
  9. StateVO stateVO = new StateVO();
  10. stateVO.setTitle(select_qualification[i]);
  11. stateVO.setSelected(false);
  12. listVOs.add(stateVO);
  13. }
  14. MyAdapter myAdapter = new MyAdapter(AdmListagem.this, 0,
  15. listVOs);
  16. spinner.setAdapter(myAdapter);
  17.  
  18. <?xml version="1.0" encoding="utf-8"?>
  19. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content">
  22.  
  23. <TextView
  24. android:id="@+id/text"
  25. xmlns:android="http://schemas.android.com/apk/res/android"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:paddingLeft="25dp"
  29. android:paddingTop="10dp"
  30. android:paddingRight="10dp"
  31. android:paddingBottom="10dp"
  32. android:textSize="16dp"
  33. android:textColor="#000000"
  34. android:background="@drawable/spinner_item_border" />
  35.  
  36. <CheckBox
  37. android:id="@+id/checkbox"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_alignParentEnd="true"
  41. android:layout_alignParentRight="true" />
  42. </RelativeLayout>
  43.  
  44. public class MyAdapter extends ArrayAdapter<StateVO> {
  45. private Context mContext;
  46. private ArrayList<StateVO> listState;
  47. private MyAdapter myAdapter;
  48. private boolean isFromView = false;
  49. String[] values;
  50. Boolean[] checkedStatus;
  51.  
  52. public MyAdapter(Context context, int resource, List<StateVO> objects) {
  53. super(context, resource, objects);
  54. this.mContext = context;
  55. this.listState = (ArrayList<StateVO>) objects;
  56. this.myAdapter = this;
  57. }
  58.  
  59. @Override
  60. public View getDropDownView(int position, View convertView,
  61. ViewGroup parent) {
  62. return getCustomView(position, convertView, parent);
  63. }
  64.  
  65. @Override
  66. public View getView(int position, View convertView, ViewGroup parent) {
  67. return getCustomView(position, convertView, parent);
  68. }
  69.  
  70. public View getCustomView(final int position, View convertView,
  71. ViewGroup parent) {
  72.  
  73. final ViewHolder holder;
  74. if (convertView == null) {
  75. LayoutInflater layoutInflator = LayoutInflater.from(mContext);
  76. convertView = layoutInflator.inflate(R.layout.spinner_item2, null);
  77. holder = new ViewHolder();
  78. holder.mTextView = (TextView) convertView
  79. .findViewById(R.id.text);
  80. holder.mCheckBox = (CheckBox) convertView
  81. .findViewById(R.id.checkbox);
  82. convertView.setTag(holder);
  83. } else {
  84. holder = (ViewHolder) convertView.getTag();
  85. }
  86.  
  87. holder.mTextView.setText(listState.get(position).getTitle());
  88.  
  89. // To check weather checked event fire from getview() or user input
  90. isFromView = true;
  91. holder.mCheckBox.setChecked(listState.get(position).isSelected());
  92. isFromView = false;
  93.  
  94. if ((position == 0)) {
  95. holder.mCheckBox.setVisibility(View.INVISIBLE);
  96. } else {
  97. holder.mCheckBox.setVisibility(View.VISIBLE);
  98. }
  99. holder.mCheckBox.setTag(position);
  100. holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  101.  
  102. @Override
  103. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  104. int getPosition = (Integer) buttonView.getTag();
  105. String t = String.valueOf(getPosition);
  106. Toast.makeText(mContext, t, Toast.LENGTH_SHORT).show();
  107.  
  108. }
  109. });
  110. return convertView;
  111. }
  112.  
  113. private class ViewHolder {
  114. private TextView mTextView;
  115. private CheckBox mCheckBox;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement