Mike Mike - 22 days ago 7
Android Question

How to pass the button intent given this android tutorial?

I based my gridview of buttons based on this tutorial from the android developers page. I'm really new to the idea of intents and android development in general.
I have a Java file full of variables that I want to use to populate a "detailed view" of about 16 "flash cards" but I'm not sure how to use the button ID as an intent to get the Java Class

For example the first button id is

btn_c1
and the data for it reads as

final String[] c1Data = {
"Left", "Right", "+", "Left", "A", "PS", "+", "Flatter", "AI", "Inferior", "Anterior",
"Contralateral", "Typical", "Typical", "Typical", "C1", "No"
};


(don't ask me what each one is, it's what my friend/client wanted in it)

This is supposed to populate into a list view that has labels, and this information on each line. I'm not sure where to even start on how to pass the data through.

The gridview adapter is pretty much exactly what the tutorial has, just different button id's.

and this is the area where I want to pass the intent. Right now it just opens a generic test activity and that works fine

final ImageAdapter CardsAdapter = new ImageAdapter(getActivity());

final GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
gridview.setAdapter(CardsAdapter);

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){

Intent intent = new Intent(getActivity(), DetailActivity.class);
startActivity(intent);

}
});


As you can see, there's multiple types of buttons and the data for each one is different than the other, if that changes anything

screenshot

Answer

Hope this can help

In the adapter ImageAdapter , in the following method.

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

        //In here a intent to your retail activity
        imageView.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
           Intent intent = new Intent(getActivity(), DetailActivity.class);
           i.putExtra("button", c1Data [position]);

        startActivity(intent);
         }
        }
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

And in the DetailActivity

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String name = intent.getStringExtra("button");
}
Comments