0

I am searching for the coding for get the image from another java file with xml layout return back to a button. And then the button will show the image which I selected. Anyone can help me about this? What I need is I want the button 'A' get the image from IconGridView.class ?

Button A = (Button) findViewById (R.id.a);
A.setOnClickListener(this);

public void onClick(View v) {
    // TODO Auto-generated method stub
startActivity(new Intent(this, IconGridView.class));    

}

IconGridView.java

icon = (GridView) findViewById(R.id.icon);

    // Instance of ImageAdapter Class
    icon.setAdapter(new IconAdapter(this));

IconAdapter.java

import android.content.Context;
import android.view.*;
import android.widget.*;


public class IconAdapter extends BaseAdapter {
private Context mContext;

// saving all images in array
public Integer[] mThumbIds = {
        R.drawable.android,R.drawable.w7,
        R.drawable.blogger,R.drawable.google,R.drawable.fb,R.drawable.youtube,R.drawable.msn,R.drawable.gmail,R.drawable.twitter,
        R.drawable.chrome,R.drawable.firefox,R.drawable.instagram,
        R.drawable.emoji1,R.drawable.emoji2,R.drawable.emoji3,R.drawable.emoji4,
        R.drawable.emoji5,R.drawable.emoji6,R.drawable.emoji7,
        R.drawable.love,R.drawable.love1,R.drawable.love2,R.drawable.rose,
        R.drawable.punch,R.drawable.good,R.drawable.peace,
        R.drawable.beer,R.drawable.book,R.drawable.calendar,R.drawable.chat,
        R.drawable.clip,R.drawable.cone,R.drawable.contact,
        R.drawable.cpleset,R.drawable.cross,R.drawable.home,
        R.drawable.laptop,R.drawable.lock,R.drawable.lock1,R.drawable.key,R.drawable.music,R.drawable.nosmoke,R.drawable.note,
        R.drawable.pills,R.drawable.printer,R.drawable.rain,R.drawable.sun,
        R.drawable.recycle,R.drawable.search,R.drawable.shopping,R.drawable.survey,
        R.drawable.sx,R.drawable.teacup,R.drawable.text,R.drawable.toilet,R.drawable.tree,
        R.drawable.up,R.drawable.down,R.drawable.check,R.drawable.qm,

};

// Constructor
public IconAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(40, 40));
    return imageView;
}

}
karan
  • 8,637
  • 3
  • 41
  • 78
user2274349
  • 415
  • 2
  • 5
  • 12
  • http://stackoverflow.com/questions/11519691/passing-image-from-one-activity-another-activity. 1st solution in the answer is better one. Pass the image from one activity to another and use the same to set button background – Raghunandan Apr 21 '13 at 07:46
  • are you extending ListActivity? Are you using a ListView? – Blackbelt Apr 21 '13 at 08:15
  • @blackbelt, no. I am using gridView for the image on another xml layout. – user2274349 Apr 21 '13 at 08:23

1 Answers1

0

on your GridView instance you can set:

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

        }
    });

the callback is triggered when you click on a element of your GridView. The position parameter refers to the element inside the adapter you clicked on:

So you can do:

 gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
           IconAdapter adapter = (IconAdapter) gridView.getAdapter() ;
           Integer drawableId = (Integer)yourAdapter.getItem(position);
           Intent intent = new Intent();
           intent.putExtra("drawableId", drawabledId); 
            setResult(Activity.RESULT_OK, intent);
    finish();   
        }
    });

you have to keep a reference to your IconAdapter, or you can try retrieving with:

IconAdapter adapter = (IconAdapter) gridView.getAdapter() ;

EDIT: Use startActivityForResult in order to start IconGridView, and when the onItemClick of the gridview is called, finish IconGridView and set as extras for the result an intent with an extra parameter, the id of resource you want to use as background for your button.

public void onClick(View v) {
    // TODO Auto-generated method stub
startActivityForResult(new Intent(this, IconGridView.class), 100);    

}



  @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode ==  Activity.RESULT_OK) {
         int drawableId = data.getIntExtra("drawableId", -1);
         if (drawableId != -1) {
             A.setBackgroundResource(drawableId);
         }
      }
   }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Then where is the coding can set my Button 'A' with an image? – user2274349 Apr 21 '13 at 09:10
  • Actually I think I misunderstood what you need. Could you rephrase your question? – Blackbelt Apr 21 '13 at 09:15
  • I have a Button 'A', when I click the 'A' then it will comes out an iterface which is my IconGridView.java and there has an Adapter display the images with gridView, all the image display on that interface, and then I had click or choose the image from the interface, there will return the image which I had clicked in the Button A and the Button A will becum a button with the image. – user2274349 Apr 21 '13 at 09:30
  • so you want one of mThumbIds to be the background for your button? – Blackbelt Apr 21 '13 at 10:08
  • yes I can . So when you click on a item of the gridview you want to came back to the previous activity and change button A background? – Blackbelt Apr 21 '13 at 10:30
  • see my editi... See the snippets of code in the WHOLE post. I made some modifications – Blackbelt Apr 22 '13 at 12:05
  • What I confuse is `IconAdapter adapter = (IconAdapter) gridView.getAdapter() ;` where can put it? It is in my adapter class or GridView class? Another 1 error is the `REQUEST_CODE`, how can fix it? And then `(Integer)yourAdapter.getItem(position)` my `IconAdapter.getItem()` there dun has static, it is also error... so how can solve these? – user2274349 Apr 22 '13 at 12:36
  • alright... Thanks god... Lucky have you... Thank you very much what you advise me... ^^ – user2274349 Apr 22 '13 at 12:53