2

I am developing a simple app for videos by using android camera. In my app I have a button having name "Make Video " and a list view which is for to show the video name recorded by my app. Now as I click the button "Make Video " it opens my mobile camera for recording but when I complete my recording the camera gives me two options. "Save" and "Discard". Now by clicking the "Save" option, I want to add the name of the recorded video to my list view. I have developed some code in this respect and it works fine,but I am facing issue that how to add the name of recorded video in my listview in onActivityResult method and update my list view. Please help me I would be very thankful to you.

You can check my code below.

public class MainActivity extends ListActivity 
{

    private ArrayList<String> cameraVideoList = new ArrayList<String>();

    Context ctx;
//  Resources res;

     int REQUEST_VIDEO_CAPTURED =1;
     Uri uriVideo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ctx = getApplicationContext();


        Button makeVideo = (Button) findViewById(R.id.button1);
        makeVideo.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                //Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);

            }
        });

        ListView videoList = getListView();

        videoList.setOnItemClickListener(new OnItemClickListener()
        {
           @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
           {
                Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            }
        });

        setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList));


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK)
        {
            if (requestCode == REQUEST_VIDEO_CAPTURED) 
            {
                uriVideo = data.getData();
//                Toast.makeText(MainActivity.this, uriVideo.getPath(),
//                        Toast.LENGTH_LONG).show();
//                
//                Toast.makeText(MainActivity.this, uriVideo.toString(),
//                        Toast.LENGTH_LONG).show();

                cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

            }
        }
    }

    public String getFileNameFromUrl(String path) 
    {
        String[] pathArray = path.split("/");
        return pathArray[pathArray.length - 1];
    }

    public class ImageAndTextAdapter extends ArrayAdapter<String>
    {
        private LayoutInflater mInflater;
        private ArrayList<String> mStrings;
        private int mViewResourceId;

        public ImageAndTextAdapter(Context context, int textViewResourceId,ArrayList<String> objects) 
        {
            super(context, textViewResourceId, objects);
            mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            mStrings = objects;
            mViewResourceId = textViewResourceId;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            convertView = mInflater.inflate(mViewResourceId, null);

            ImageView iv = (ImageView)convertView.findViewById(R.id.icon);
            iv.setImageResource(R.drawable.video_icon);

            TextView tv = (TextView)convertView.findViewById(R.id.text);
            tv.setText(mStrings.get(position));

            return convertView;
        }
    }

}
user1703737
  • 533
  • 1
  • 11
  • 25

2 Answers2

2

As you going right way but here some change are requirement.

  1. As you create your Custom adapter class object that not work because when you change your data in list object you have to notify by adapter for list view content.

Declare ImageAndTextAdapter adapter; as global and private object

onCreate(){

    adapter = new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList);
    videoList.setAdapter(adapter);

}

Ok now in onActivityResult after adding new record into your list object just call the notifyDataSetChange() of your adapter class

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK)
    {
        if (requestCode == REQUEST_VIDEO_CAPTURED) 
        {
            uriVideo = data.getData();

            cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

            adapter.notifyDatasetChanged(); // here
        }
    }
}
Pratik
  • 30,639
  • 18
  • 84
  • 159
2

First way.

On this line. You created ImageAndTextAdapter object which is unreachable.

setListAdapter(new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList));

Extract your ImageAndTextAdapter object in a variable

adapter = new ImageAndTextAdapter(ctx, R.layout.list_item_icon_text, cameraVideoList);
  setListAdapter(adapter);

From this statement of onActivityResult() call notifyDataSetChanged() of ImageAndTextAdapter

if (requestCode == REQUEST_VIDEO_CAPTURED) 
{
     uriVideo = data.getData();
//   Toast.makeText(MainActivity.this, uriVideo.getPath(),
//    Toast.LENGTH_LONG).show();
//                
//   Toast.makeText(MainActivity.this, uriVideo.toString(),
//   Toast.LENGTH_LONG).show();

     cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

     adapter.notifyDataSetChanged();
}

Second way. You can call ((ImageAndTextAdapter) getAdapter()).notifyDataSetChanged()

In your onActivityResult() implementation you can do it like this

if (requestCode == REQUEST_VIDEO_CAPTURED) 
{
     uriVideo = data.getData();

     cameraVideoList.add(getFileNameFromUrl(uriVideo.getPath().toString()));

     ((ImageAndTextAdapter) getAdapter()).notifyDataSetChanged(); // 
}
Glenn
  • 12,741
  • 6
  • 47
  • 48