• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps creating intents in gridview

rchamp

Lurker
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:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class Dashboard extends Activity {
       
       


    String[] main_list = {
            "first", 
            "second", 
            "third"
            };
    
    /** Called when the activity is first created. */

       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                CharSequence text = "Now Loading " + main_list[position] + "...";
                
                Toast.makeText(Dashboard.this, text, Toast.LENGTH_SHORT).show();
                Intent myIntent = null;
                if(position == 0){
                    myIntent = new Intent(v.getContext(), browser.class);
                }
                if(position == 1){
                    myIntent = new Intent(v.getContext(), storage_calc.class);
                }
                if(position ==2){
                    myIntent = new Intent(v.getContext(), vendor_list.class);
                }
            }
        });

        
    }

    
    
        
        
        
        
        
        
    
    // Create custom class ImageAdapter
    // this is the source for all images to be displayed on the grid.
    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        
               
        public ImageAdapter(Context c) {
            mContext = c;
        }
       
        public int getCount() {
            // TODO Auto-generated method stub
            return mThumbIds.length;

        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            
            ImageView imageView;
            
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                                
                imageView = new ImageView(mContext);
                
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        // references to our images
        private Integer[] mThumbIds = {
                R.drawable.browser, R.drawable.calculator,
                R.drawable.contacts
        };
    }
    
        
    
    
    
    
    
}

Any ideas? Any and all remarks are greatly appreciated.

Thanks,

Ryan
 
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
 
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();
 
Back
Top Bottom