batuman
batuman

Reputation: 7304

onActivityResult in not called in the class extended from ArrayAdapter

I did search but can't find useful answer in the internet. That is why I raise the query. I like to load the camera and capture image in the getView method of a custom ArrayAdapter class. Camera is loaded and capture image but onActivityResult() is never called. My code is as shown below.

       public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder viewHolder=new ViewHolder();
        LayoutInflater inflater = (LayoutInflater) this.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(vi==null){
            vi = inflater.inflate(R.layout.list_row, parent, false);
            viewHolder.id=(TextView)vi.findViewById(R.id.title);
            viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
            viewHolder.arrow=(ImageView)vi.findViewById(R.id.list_arrow);
            vi.setTag(viewHolder);
        }
        else
            viewHolder=(ViewHolder) vi.getTag();



        // Setting all values in listview
        viewHolder.id.setText(listIDs.get(position));
        viewHolder.thumbnailImage.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                ((Activity)context).startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }

        });
        return vi;
    }

   protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
//            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
//            imageView.setImageBitmap(photo);
        }  

    } 

Then the custom adapter class is activated from the ListFragment class as

customList_Adaptor adapter = new customList_Adaptor(
                getActivity(), R.layout.list_row, listIDs);

How can I modify so that onActivityResult() method is called. Thanks

Upvotes: 1

Views: 4910

Answers (3)

Zubair Ahmed
Zubair Ahmed

Reputation: 2897

First create the object of your CustomArrayAdapter class in the class which must extends with Activity or ListActivity because onActivityForResult is an overrided method of Activity class. In your case you can follow these steps:

1) Make a class

import android.graphics.Bitmap;

public class Items { // You can give any name to this class

    private Bitmap bitmap;

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

}

2) In your class (which extends with Activity or ListActivity) you should create the object of your CustomArrayAdapter Class like:

public class YourClassName extends Activity {\

         public static final int CAMERA_REQUEST = 1; // here integer value 1 must be same                
                                                     // as you gave in CustomAdapterClass

        //First make an array list of the class created above
     ArrayList<Items> camera_pic_array = new ArrayList<Items>();

       // make object of CustomArrayAdapter Class
     CustomArrayAdapter adapter = new  CustomArrayAdapter(this, camera_pic_array);


      /* It is assumed by me that you should have a constructor of your CustomArrayAdapter 
       with two parameters like 
       public CustomArrayAdapter(Context ctx, ArrayList<Items> list){
               this.context = context;
        this.list = list;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
        */

       // now here, write onActivityResult code 


     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case CAMERA_REQUEST: {
            if (resultCode == RESULT_OK) {
                Bitmap bitmap = (Bitmap) data.getExtras().get("data");              
                Items item = new Items();
                item.setBitmap(bitmap);
                camera_pic_array.add(item);
                adapter.notifyDataSetChanged(); //you must notify this 
                                                                // line is very important

            }

            break;
        }
           }
        }

Hope This will work for you, I did same and works fine for me....

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157487

onActivityResult belongs to the Activity. You can override only in the Actvity/ListActivity context. What you can do is to keep a reference to your ArrayAdapter's subclass inside the Actvity/ListActivity, create a method inside it to invoke when onActivityResult is fired and condition are met

Upvotes: 0

Sam
Sam

Reputation: 3413

The onActivityResult will be called on your activity. Not on your array adapter.

Upvotes: 0

Related Questions