0

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.

Alex Ardavin
  • 345
  • 1
  • 4
  • 20
  • in item click listener `TextView name= (TextView) v.findViewById(R.id.grid_item_name);` and then display it in toast – Raghunandan Apr 16 '16 at 07:12
  • Thanks for the suggestion but that is not working sir. It appears "This is android.support.v7.widget.AppCompatTextView{41545654d8 V.ED........ 0,48-117,117#7f00c7a app:id/grid_item_name}" – Alex Ardavin Apr 16 '16 at 07:32
  • Not working though, "Cannot resolve method getItem(int)" :( but thank you sir. – Alex Ardavin Apr 16 '16 at 07:38
  • Now Check it. Toast.makeText(getActivity(), "This is " +parent. getItem(position).name, Toast.LENGTH_SHORT).show(); – Ankita Shah Apr 16 '16 at 07:40
  • Still not working, cannot resolve that method. What about trying with parent.getItemAtPosition(position).name? It resolves the method but it can´t find the symbol name... – Alex Ardavin Apr 16 '16 at 07:48
  • 1
    @AlejandroArdavín you set a click listener to View v in getView then have a interface as a callback to the fragment. You can get the name based on posiiton using the list in getview. use set tag an get tag. – Raghunandan Apr 16 '16 at 08:28
  • Something like this @Raghunandan? http://stackoverflow.com/questions/12734793/android-get-position-of-clicked-item-in-gridview – Alex Ardavin Apr 16 '16 at 21:06
  • You just answered my question in another answer you gave here: http://stackoverflow.com/questions/24703257/how-to-get-string-from-an-arraylist-from-a-gridview-onitemclick-in-a-fragment?rq=1, amazing! Thank you very much sir @Raghynandan, I will update my answer. – Alex Ardavin Apr 17 '16 at 00:00

2 Answers2

0

Another approach for doing that is to perform click event of your parent layout inside your Adapter.

first you need to initialize your parent layout of layout.grid_item.xml inside your adapter. (I assume you can do that.)

and then just perform click event of that particular layout.

For Example

layout= (RelativeLayout) v.getTag(R.id.main_layout);

layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "This is " + name.getText().toString, Toast.LENGTH_SHORT).show();
            }
        });

And you are good to go..

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

Well, thanks to this excellent answer: https://stackoverflow.com/a/24703304/5051506 by @Raghunandan I figured it out how to do it.

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

With that, I just had to add the "name" variable to my toast and it displayed it. Hope it may help someone with the same problem in the future.

Community
  • 1
  • 1
Alex Ardavin
  • 345
  • 1
  • 4
  • 20