1

I'm trying to set up an onclick listener for a gridview. The gridview shows text and an image. When an item is clicked, I want it to get the text from that item and check it against some if statements. Here is my class. Due to that fact that it in a fragment, and I'm new to fragments, I am having difficulty figuring it out. Thanks

    IMPORTS HERE...

    public class Items extends SherlockFragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.activity_main, container, false);


            GridView gridView = (GridView)view.findViewById(R.id.gridview);
            gridView.setAdapter(new MyAdapter(getActivity()));
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    if(SOMETHING.equals("Furniture")){
                        Toast.makeText(getActivity(), "Furny", Toast.LENGTH_SHORT).show();
                    //THIS IS WHERE I'M STUCK. I DON'T KNOW WHAT TO REPLACE "SOMETHING" WITH

}
             }
            });


            return view;
        }



        public class MyAdapter extends BaseAdapter
        {
            private List<Item> items = new ArrayList<Item>();
            private LayoutInflater inflater;

            public MyAdapter(Context context)
            {
                inflater = LayoutInflater.from(context);

                items.add(new Item("Weapons", R.drawable.BOMB));
                items.add(new Item("Tools", R.drawable.TOOL));
                items.add(new Item("Furniture", R.drawable.CHAIR));


            }

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

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

            @Override
            public long getItemId(int i)
            {
                return items.get(i).drawableId;
            }



            @Override
            public View getView(int i, View view, ViewGroup viewGroup)
            {
                View v = view;
                ImageView picture;
                TextView name;

                if(v == null)
                {
                   v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
                   v.setTag(R.id.picture, v.findViewById(R.id.picture));
                   v.setTag(R.id.text, v.findViewById(R.id.text));
                }

                picture = (ImageView)v.getTag(R.id.picture);
                name = (TextView)v.getTag(R.id.text);

                Item item = (Item)getItem(i);
                Options options = new BitmapFactory.Options();
                options.inDither = false;
                options.inScaled = false;
                Bitmap source = BitmapFactory.decodeResource(getResources(), item.drawableId, options);

                final int maxSize = 120;
                int outWidth;
                int outHeight;
                int inWidth = source.getWidth();
                int inHeight = source.getHeight();
                if(inWidth > inHeight){
                    outWidth = maxSize;
                    outHeight = (inHeight * maxSize) / inWidth; 
                } else {
                    outHeight = maxSize;
                    outWidth = (inWidth * maxSize) / inHeight; 
                }

                Bitmap resizedBitmap = Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
                picture.setImageBitmap(resizedBitmap);
                name.setText(item.name);

                return v;


            }

            private class Item
            {
                final String name;
                final int drawableId;

                Item(String name, int drawableId)
                {
                    this.name = name;
                    this.drawableId = drawableId;
                }
            }
        }


    }
cnfw
  • 770
  • 2
  • 11
  • 28

1 Answers1

0

Use the position param and get the item at that position

 Item item= (Item) parent.getItemAtPosition(position);
 String name = item.name;

and you can move the below to separate .java file

  public class Item 
  {
             String name;
             int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
   }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Great! Thank you very much! This worked perfectly except since my class for "Item" was internal i had to do `CLASS.MyAdapter.Item item= (CLASS.MyAdapter.Item) parent.....` – cnfw Jul 11 '14 at 17:32
  • @cw1998 you can move that class to separate `.java` File as suggested in the edited post. its also private in your case – Raghunandan Jul 11 '14 at 17:33
  • In efforts to keep everything together, I put it internally, but I guess it would be easier to move it to its own .java file. But I will go with your suggestion! Thanks! – cnfw Jul 11 '14 at 17:34