1. Are you ready for the Galaxy S20? Here is everything we know so far!

creating intents in gridview

Discussion in 'Android Development' started by rchamp, Feb 8, 2011.

  1. rchamp

    rchamp Lurker
    Thread Starter

    hey there, I'm new to the android development scene.

    I have a project where I've created a gridview. I got the images to display properly and I even have a toast displaying custom messages when each particular item is clicked.

    What I am trying to figure out now is how do I create an Intent to fire and open a new acitivity (class) when I click the icon?

    here is what I have so far...

    Code (Text):
    1.  
    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.content.Intent;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.view.ViewGroup;
    8. import android.widget.AdapterView;
    9. import android.widget.AdapterView.OnItemClickListener;
    10. import android.widget.BaseAdapter;
    11. import android.widget.GridView;
    12. import android.widget.ImageView;
    13. import android.widget.Toast;
    14.  
    15. public class Dashboard extends Activity {
    16.        
    17.        
    18.  
    19.  
    20.     String[] main_list = {
    21.             "first",
    22.             "second",
    23.             "third"
    24.             };
    25.    
    26.     /** Called when the activity is first created. */
    27.  
    28.        @Override
    29.     public void onCreate(Bundle savedInstanceState) {
    30.         super.onCreate(savedInstanceState);
    31.         setContentView(R.layout.main);
    32.  
    33.         GridView gridview = (GridView) findViewById(R.id.gridview);
    34.         gridview.setAdapter(new ImageAdapter(this));
    35.  
    36.         gridview.setOnItemClickListener(new OnItemClickListener() {
    37.            
    38.             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    39.                 CharSequence text = "Now Loading " + main_list[position] + "...";
    40.                
    41.                 Toast.makeText(Dashboard.this, text, Toast.LENGTH_SHORT).show();
    42.                 Intent myIntent = null;
    43.                 if(position == 0){
    44.                     myIntent = new Intent(v.getContext(), browser.class);
    45.                 }
    46.                 if(position == 1){
    47.                     myIntent = new Intent(v.getContext(), storage_calc.class);
    48.                 }
    49.                 if(position ==2){
    50.                     myIntent = new Intent(v.getContext(), vendor_list.class);
    51.                 }
    52.             }
    53.         });
    54.  
    55.        
    56.     }
    57.  
    58.    
    59.    
    60.        
    61.        
    62.        
    63.        
    64.        
    65.        
    66.    
    67.     // Create custom class ImageAdapter
    68.     // this is the source for all images to be displayed on the grid.
    69.     public class ImageAdapter extends BaseAdapter {
    70.         private Context mContext;
    71.        
    72.                
    73.         public ImageAdapter(Context c) {
    74.             mContext = c;
    75.         }
    76.        
    77.         public int getCount() {
    78.             // TODO Auto-generated method stub
    79.             return mThumbIds.length;
    80.  
    81.         }
    82.  
    83.         public Object getItem(int position) {
    84.             return null;
    85.         }
    86.  
    87.         public long getItemId(int position) {
    88.             return 0;
    89.         }
    90.  
    91.         // create a new ImageView for each item referenced by the Adapter
    92.         public View getView(int position, View convertView, ViewGroup parent) {
    93.            
    94.             ImageView imageView;
    95.            
    96.             if (convertView == null) {  // if it's not recycled, initialize some attributes
    97.                                
    98.                 imageView = new ImageView(mContext);
    99.                
    100.                 imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    101.                 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    102.                 imageView.setPadding(8, 8, 8, 8);
    103.             } else {
    104.                 imageView = (ImageView) convertView;
    105.             }
    106.             imageView.setImageResource(mThumbIds[position]);
    107.             return imageView;
    108.         }
    109.  
    110.         // references to our images
    111.         private Integer[] mThumbIds = {
    112.                 R.drawable.browser, R.drawable.calculator,
    113.                 R.drawable.contacts
    114.         };
    115.     }
    116.    
    117.        
    118.    
    119.    
    120.    
    121.    
    122.    
    123. }
    124.  
    Any ideas? Any and all remarks are greatly appreciated.

    Thanks,

    Ryan
     



    1. Download the Forums for Android™ app!


      Download

       
  2. manojkm

    manojkm Lurker

    I think you are quite there already. After assigning the class to myIntent, you need to call startActivity(myIntent); to start the corresponding activity. You might be set some flags to to your intent before starting the activity.
    Thanks
    Manoj
     
  3. asgar.ali

    asgar.ali Lurker

    Manoj and Rayan, I have a question I am trying same example. I have pictures in the grid. Now what I want to do is play some sond on when we click on the picture. How can I do that. Any pointers will really help

    MediaPlayer soudn= MediaPlayer.create(​
    this,R.raw.alif);

    soudn.start();
     
Loading...
Similar Threads - creating intents gridview
  1. cwg01
    Replies:
    3
    Views:
    1,702
  2. kumaranil13k
    Replies:
    4
    Views:
    634
  3. 5g Moto Android 11
    Replies:
    2
    Views:
    1,082
  4. Muhammad Faizan Ali Khan
    Replies:
    2
    Views:
    436
  5. App Update
    Replies:
    0
    Views:
    322
  6. Mehak Gul
    Replies:
    1
    Views:
    328
  7. ghostanime2001
    Replies:
    16
    Views:
    2,777
  8. Muhammad Faizan Ali Khan
    Replies:
    1
    Views:
    605
  9. Muhammad Faizan Ali Khan
    Replies:
    1
    Views:
    373
  10. wseng92
    Replies:
    7
    Views:
    1,791

Share This Page

Loading...