Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. ArrayList<ItemData> list = ItemData.createFromMaterialArray(materials);
  2. MaterialsAdapter adapter = new MaterialsAdapter(this,
  3. R.layout.custom_material_item, R.id.text, list);
  4.  
  5.  
  6. AlertDialog.Builder materialTypesDialog = new AlertDialog.Builder(this);
  7. materialTypesDialog.setTitle(R.string.material);
  8. materialTypesDialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
  9. @Override
  10. public void onClick(DialogInterface dialog, int which) {
  11. // Nothing happens, why???
  12. Toast.makeText(context, "WORK", Toast.LENGTH_LONG).show();
  13. }
  14. });
  15.  
  16. materialTypesDialog.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  17. @Override
  18. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  19. // Same problem
  20. Toast.makeText(context, "WORK", Toast.LENGTH_LONG).show();
  21. }
  22.  
  23. @Override
  24. public void onNothingSelected(AdapterView<?> parent) {
  25.  
  26. }
  27. });
  28.  
  29. materialTypesDialog.show();
  30.  
  31. class MaterialsAdapter extends ArrayAdapter<ItemData> {
  32.  
  33. private int groupId;
  34.  
  35. Activity context;
  36. ArrayList<ItemData> list;
  37. private LayoutInflater inflater;
  38.  
  39. /**
  40. * @param context
  41. * @param _groupId
  42. * @param _id
  43. * @param list
  44. */
  45. MaterialsAdapter(Activity context, int _groupId, int _id, ArrayList<ItemData>
  46. list) {
  47. super(context, _id, list);
  48. this.list = list;
  49. inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  50. this.groupId = _groupId;
  51. }
  52.  
  53. /**
  54. * @param position
  55. * @param convertView
  56. * @param parent
  57. * @return
  58. */
  59. public View getView(int position, View convertView, ViewGroup parent) {
  60.  
  61. View itemView = inflater.inflate(groupId, parent, false);
  62. ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
  63.  
  64. imageView.setImageResource(list.get(position).getImageId());
  65. TextView textView = (TextView) itemView.findViewById(R.id.text);
  66. textView.setText(list.get(position).getTextId());
  67.  
  68. return itemView;
  69.  
  70. }
  71.  
  72. /**
  73. * @param position
  74. * @param convertView
  75. * @param parent
  76. * @return
  77. */
  78. public View getDropDownView(int position, View convertView, ViewGroup
  79. parent) {
  80. return getView(position, convertView, parent);
  81.  
  82. }
  83. }
  84.  
  85. class ItemData {
  86.  
  87. private String id;
  88. private int textId;
  89. private int imageId;
  90.  
  91. /**
  92. * @param id
  93. * @param textId
  94. * @param imageId
  95. */
  96. private ItemData(String id, int textId, int imageId) {
  97. this.id = id;
  98. this.textId = textId;
  99. this.imageId = imageId;
  100. }
  101.  
  102. /**
  103. * @return
  104. */
  105. int getTextId() {
  106. return textId;
  107. }
  108.  
  109. /**
  110. * @return
  111. */
  112. int getImageId() {
  113. return imageId;
  114. }
  115.  
  116. /**
  117. * @return
  118. */
  119. String getId() {
  120. return id;
  121. }
  122.  
  123.  
  124. /**
  125. * @return
  126. */
  127. static ArrayList<ItemData> createFromMaterialArray(Material[] materials) {
  128. ArrayList<ItemData> itemDataList = new ArrayList<>();
  129.  
  130. for (Material material : materials) {
  131.  
  132. itemDataList.add(
  133. new ItemData(material.getName(),
  134. material.textID,
  135. material.imageID);
  136. }
  137.  
  138. return itemDataList;
  139. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement