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;
}
}
}
}