Search code examples
androidlistviewandroid-camera-intentonactivityresult

How to change listview row icon after capturing image?


I am new in android, am able to implement normal image capture through camera intent procedure on button click. Now what i want suppose i have a custom listview in which listview row contains textview and an imageview which is on right side ,if i click on imageview then default camera of device should open after capturing the image then the path of the image should save in array and this goes on and most importantly what i want that after returning to listview activity image of imageview(through which camera intent is fired ) of particular row item should change according to position to notify the user that image successfully captured and its path stored in array.

Any help will be appriciated. Thanks in advance.


Solution

  • Save the clicked position in the source activity. Use the source activity's onActivityResult to update your data with a flag indicating that the image has been captured for the given position, and call notifyDataSetChanged() on the adapter. In your getView() of adapter, check for the flag and update view accordingly.

    Edit: Added code to save clicked position

    MyAdapter.java

    @Override
    public View getView(final int position, View convertView, ViewGroup   parent) {
        if(mData.get(position).isImageCaptured(){
            // handle image captured case
        } else  {
            // handle normal case
        }    
    }
    
    public void updateData(int position,String path){
        MyObject obj = mData.get(position);
        obj.setPath(path);
        obj.setImageCaptured(true);
        mData.set(position,obj);
        notifyDataSetChanged();
    }
    

    Source Activity

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_CODE_CAMERA){
            mAdapter.updateData(mSelectedPosition,imagePath);    
        }
    }
    
    public void onInitiateImageCapture(int position){
    
        //rest of the boilerplate code to create camera intent
    
        mSelectedPosition = position;
        startActivityForResult(cameraIntent, REQUEST_CODE_CAMERA);
    }