Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Issue about obtaining item's position of gridview and list content related with it

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I sent the position of each item of gridview to next activity. When I pressed first one, content realted with it was listed but when I pressed other positions such as 2nd 3th and son on , app was crushed.When I run the app the debug mode, I show that the position is got from Adapter but I couldn't solve the issue. How do I solve it out? Thanks.

Here are the codes belows.

MainActivity

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
              switch (position){

              case 0:
                   Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_SHORT).show();
                   Intent intent = new Intent(MainActivity.this,AttactivePlacesActivity.class);
                   intent.putExtra("position",position);
                   startActivity(intent);
                   break;

               case 1:
                   Toast.makeText(getApplicationContext(),position+"",Toast.LENGTH_SHORT).show();
                   Intent intent1 = new Intent(MainActivity.this,AttactivePlacesActivity.class);
                   intent1.putExtra("position",position);
                   startActivity(intent1);
                   break;
AttactivePlacesActivity

Intent mIntent = getIntent();
       Bundle bundle = mIntent.getExtras();

       int position = bundle.getInt("position");



       AttractivePlacesAdapter attractivePlacesAdapter = new AttractivePlacesAdapter(this,cityAttactivePlaces,R.color.mainBackground,position);
AttractivePlacesAdapter

public class AttractivePlacesAdapter extends ArrayAdapter<City> {

   private int mBackgroundColor; // BackGround Color For each Activity

   private int gridviewPosition;

   public AttractivePlacesAdapter(Context context, ArrayList<City> resources, int color,int position) {
       // Here, we initialize the ArrayAdapter's internal storage for the context and the list.
       // the second argument is used when the ArrayAdapter is populating a single TextView.
       // Because this is a custom adapter for two TextViews , the adapter is not
       // going to use this second argument, so it can be any value. Here, we used 0.
       // 0 -> list item layout resource ID
       super(context,0 ,resources);
       mBackgroundColor = color;
       gridviewPosition = position;
   }


   // get list item from getView

   /**
    * Provides a view for an AdapterView (ListView, GridView, etc.)
    *
    * @param position The position in the list of data that should be displayed in the
    *                 list item view.
    * @param convertView The recycled view to populate.
    * @param parent The parent ViewGroup that is used for inflation.
    * @return The View for the position in the AdapterView.
    */
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {

       // Check if the existing view is being reused, otherwise inflate the view
       View listview = convertView;
       if (listview == null) {
           // parent -> listView   false -> we don't want to attach list item to parent
           listview = LayoutInflater.from(getContext()).inflate(R.layout.layout, parent, false);
       }

       // get position of item to city
       City city = getItem(gridviewPosition);


           // City Information
           TextView name = (TextView) listview.findViewById(R.id.bilgi_text_view);
           name.setText(city.getAttactivePlaces().get(position).getPlaceName());


           // Picture of City
           ImageView image = (ImageView) listview.findViewById(R.id.image_image_view);


           // Check whether the picture is or not.
           if (city.hasImage()) {
               //
              image.setImageResource(city.getAttactivePlaces().get(position).getmAttaticePlaceImageResourceId());
           } else {
               image.setVisibility(View.GONE);
           }

       //}

       // Set theme color for the list item
       View textContainer = listview.findViewById(R.id.text_container);

       // Find the color that the resource ID maps to
       int color = ContextCompat.getColor(getContext(), mBackgroundColor);

       // Set Background color to view
       textContainer.setBackgroundColor(color);

       return listview;

   }

}


Error

03-12 09:52:35.417 5056-5056/com.example.android.turkeytourguide E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.android.turkeytourguide, PID: 5056
                                                                                  java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
                                                                                      at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                      at java.util.ArrayList.get(ArrayList.java:308)
                                                                                      at com.example.android.turkeytourguide.AttractivePlacesAdapter.getView(AttractivePlacesAdapter.java:63)
                                                                                      at android.widget.AbsListView.obtainView(AbsListView.java:2344)
                                                                                      at android.widget.ListView.makeAndAddView(ListView.java:1864)
                                                                                      at android.widget.ListView.fillDown(ListView.java:698)
                                                                                      at android.widget.ListView.fillFromTop(ListView.java:759)
                                                                                      at android.widget.ListView.layoutChildren(ListView.java:1673)
                                                                                      at android.widget.AbsListView.onLayout(AbsListView.java:2148)
                                                                                      at android.view.View.layout(View.java:15596)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4966)
                                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                                      at android.view.View.layout(View.java:15596)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4966)
                                                                                      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                                      at android.view.View.layout(View.java:15596)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4966)
                                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                                      at android.view.View.layout(View.java:15596)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4966)
                                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                                      at android.view.View.layout(View.java:15596)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4966)
                                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                                                      at android.view.View.layout(View.java:15596)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:4966)
                                                                                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
                                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
                                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
                                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
                                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                                      at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5223)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


Error line --> name.setText(city.getAttactivePlaces().get(position).getPlaceName());

 
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

  java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1  


That says the code used an index of 1 in an array that one element which means the max index is 0.

Check the logic to see why it used an index past the end of the array.
 
Kevin Rapter
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm Radder

I already knew that there is an arrayoutofboundsException because of the size of array before I post it here. My Question is how to troubleshoot it .
 
Norm Radder
Rancher
Posts: 4936
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how to troubleshoot it .


Why did the code assume there were at least 2 elements in the list?
Where is the list loaded with values?
Does it load more than 1 item into the list?
If there is only one item in the list, what is it?

Add lots of print statements to show the values of variables used in the code to see how they are changed.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic