Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

get clicked item of gridview to access it from getView in Adapter

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just created a city object with other essential things like tourist places. I used arraylist in the Arraylist .

There is any object in the arrayslist .Here is the code below.

City city = new City();

       city.setCityName("London");
       city.setmCityImageResourceId(R.drawable.london);

       AttactivePlaces londonAttactivePlaces_one =new AttactivePlaces();
       londonAttactivePlaces_one.setPlaceName("Big Ben");
       londonAttactivePlaces_one.setmAttaticePlaceImageResourceId(R.drawable.bigben);
       londonAttactivePlaces_one.setPlaceDetailInformation("Big Ben");
       city.getAttactivePlaces().add(londonAttactivePlaces_one);


MainActicity

CityAdapter cityAdapter = new CityAdapter(this,cityArraylist,R.color.mainBackground);

       GridView gridView = (GridView) findViewById(R.id.gridlist);
       gridView.setAdapter(cityAdapter);

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

               switch (position){

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

AttractivePalcesActivity

ArrayList<City> cityAttactivePlaces = new ArrayList<City>();

     
       cityAttactivePlaces = MainActivity.cityArraylist;

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

       ListView listView = (ListView) findViewById(R.id.listviewAttactivePlaces);

       listView.setAdapter(attractivePlacesAdapter);


AttractivePlacesAdapter

public class AttractivePlacesAdapter extends ArrayAdapter<City> {

   private int mBackgroundColor; // BackGround Color For each Activity


   public AttractivePlacesAdapter(Context context, ArrayList<City> resources, int color) {
       // 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;
   }


   // 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(position);

       ArrayList<City> cityArrayList = MainActivity.cityArraylist;

       City city = cityArrayList.get(1);

       for(int i=0;i<city.getAttactivePlaces().size();i++){

           // City Information
           TextView name = (TextView) listview.findViewById(R.id.bilgi_text_view);
           name.setText(city.getAttactivePlaces().get(i).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(i).getmAttaticePlaceImageResourceId());
           } else {
               image.setVisibility(View.GONE);
           }

       }

       // City Information
       //TextView name = (TextView) listview.findViewById(R.id.name);

       //name.setText(city.getAttactivePlaces().get(0).getPlaceName());

       //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(0).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;

   }

}

When I clicked any item of gridview, list informaiton of this item.  I ask it to many platform but not required response is occured.

How do I access clicked item' position  of gridview form AttractivePlacesAdapter.

I will apprecate you if you answer my quesiton and show the solution step by step.
 
Saloon Keeper
Posts: 28054
198
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks like an Android question. Moving to a more appropriate forum.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic