0

I am trying to get the image property when there is an onclick event happens on the images displayed in the Grid View inside a Fragment. I am trying to fetch values from the parameters passed to onItemClick method. When I try to access any parameter, inside the toaster the app stops working.

Inside the toaster I am replacing the text "test" with v.getId()/position/id. Then the app stops.

Here is the code.

    GridView gridView = (GridView) view.findViewById(R.id.imageGrid);
    gridView.setAdapter(new ImageAdapter(this));    
    gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
         Toast.makeText(getActivity(), "test"  ,Toast.LENGTH_SHORT).show();
        }});

Do I need to provide any other details? or am I making any silly mistakes?

Update: I am adding my error log here.

09-28 16:03:53.327: W/dalvikvm(15316): threadid=1: thread exiting with uncaught exception (group=0x410f6300)
09-28 16:03:53.337: E/AndroidRuntime(15316): FATAL EXCEPTION: main
09-28 16:03:53.337: E/AndroidRuntime(15316): android.content.res.Resources$NotFoundException: String resource ID #0x10
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.content.res.Resources.getText(Resources.java:229)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.Toast.makeText(Toast.java:265)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at com.example.store.fragments.AllStoresFragment$1.onItemClick(AllStoresFragment.java:111)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.widget.AbsListView$1.run(AbsListView.java:3529)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.os.Handler.handleCallback(Handler.java:615)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.os.Looper.loop(Looper.java:137)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at android.app.ActivityThread.main(ActivityThread.java:4745)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at java.lang.reflect.Method.invokeNative(Native Method)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at java.lang.reflect.Method.invoke(Method.java:511)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 16:03:53.337: E/AndroidRuntime(15316):    at dalvik.system.NativeStart.main(Native Method)
intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75

1 Answers1

1

If you pass v.getId()/position/id to the Toast it will think that it represents the id of a String resources from strings.xml, which it will not find and throw that exception. Try this instead:

Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();

or

Toast.makeText(getActivity(), "" + position + " " + id, Toast.LENGTH_SHORT).show();
user
  • 86,916
  • 18
  • 197
  • 190