I have an app with a GridView inside a Fragment and each item on it has a name and a picture. I want to get and show the name of each picture as a toast when I click on an item. How can I do it?
This is my fragment:
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
// Get layout GridViewSi and load images from ImageAdapter.
ExpandableHeightGridView gridViewSi = (ExpandableHeightGridView) view.findViewById(R.id.gridview_si);
gridViewSi.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewSi.setExpanded(true);
gridViewSi.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// This toast should show the picture's name like "This is Buddy" instead of position
Toast.makeText(getActivity(), "This is " + position, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
And this is my ImageAdapter where the data is:
public final class ImageAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;
public ImageAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mItems.add(new Item("Moyo", R.drawable.sample_0));
mItems.add(new Item("Rex", R.drawable.sample_1));
mItems.add(new Item("Buddy", R.drawable.sample_2));
mItems.add(new Item("Alejandro", R.drawable.sample_3));
mItems.add(new Item("Mía", R.drawable.sample_4));
mItems.add(new Item("Bobby", R.drawable.sample_5));
mItems.add(new Item("José Antonio", R.drawable.sample_6));
mItems.add(new Item("Skipper", R.drawable.sample_7));
mItems.add(new Item("Moyo", R.drawable.sample_0));
mItems.add(new Item("Rex", R.drawable.sample_1));
mItems.add(new Item("Buddy", R.drawable.sample_2));
mItems.add(new Item("Alejandro", R.drawable.sample_3));
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Item getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return mItems.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
v.setTag(R.id.grid_item_picture, v.findViewById(R.id.grid_item_picture));
v.setTag(R.id.grid_item_name, v.findViewById(R.id.grid_item_name));
}
picture = (ImageView) v.getTag(R.id.grid_item_picture);
name = (TextView) v.getTag(R.id.grid_item_name);
Item item = getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private static class Item {
public final String name;
public final int drawableId;
Item(String name, int drawableId) {
this.name = name;
this.drawableId = drawableId;
}
}
}
I will appreciate any kind of help as I've been stuck on it all day! Thank you very much in advance.