Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<ItemData> list = ItemData.createFromMaterialArray(materials);
- MaterialsAdapter adapter = new MaterialsAdapter(this,
- R.layout.custom_material_item, R.id.text, list);
- AlertDialog.Builder materialTypesDialog = new AlertDialog.Builder(this);
- materialTypesDialog.setTitle(R.string.material);
- materialTypesDialog.setAdapter(adapter, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Nothing happens, why???
- Toast.makeText(context, "WORK", Toast.LENGTH_LONG).show();
- }
- });
- materialTypesDialog.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
- @Override
- public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- // Same problem
- Toast.makeText(context, "WORK", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onNothingSelected(AdapterView<?> parent) {
- }
- });
- materialTypesDialog.show();
- class MaterialsAdapter extends ArrayAdapter<ItemData> {
- private int groupId;
- Activity context;
- ArrayList<ItemData> list;
- private LayoutInflater inflater;
- /**
- * @param context
- * @param _groupId
- * @param _id
- * @param list
- */
- MaterialsAdapter(Activity context, int _groupId, int _id, ArrayList<ItemData>
- list) {
- super(context, _id, list);
- this.list = list;
- inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- this.groupId = _groupId;
- }
- /**
- * @param position
- * @param convertView
- * @param parent
- * @return
- */
- public View getView(int position, View convertView, ViewGroup parent) {
- View itemView = inflater.inflate(groupId, parent, false);
- ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
- imageView.setImageResource(list.get(position).getImageId());
- TextView textView = (TextView) itemView.findViewById(R.id.text);
- textView.setText(list.get(position).getTextId());
- return itemView;
- }
- /**
- * @param position
- * @param convertView
- * @param parent
- * @return
- */
- public View getDropDownView(int position, View convertView, ViewGroup
- parent) {
- return getView(position, convertView, parent);
- }
- }
- class ItemData {
- private String id;
- private int textId;
- private int imageId;
- /**
- * @param id
- * @param textId
- * @param imageId
- */
- private ItemData(String id, int textId, int imageId) {
- this.id = id;
- this.textId = textId;
- this.imageId = imageId;
- }
- /**
- * @return
- */
- int getTextId() {
- return textId;
- }
- /**
- * @return
- */
- int getImageId() {
- return imageId;
- }
- /**
- * @return
- */
- String getId() {
- return id;
- }
- /**
- * @return
- */
- static ArrayList<ItemData> createFromMaterialArray(Material[] materials) {
- ArrayList<ItemData> itemDataList = new ArrayList<>();
- for (Material material : materials) {
- itemDataList.add(
- new ItemData(material.getName(),
- material.textID,
- material.imageID);
- }
- return itemDataList;
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement