Skip to content Skip to sidebar Skip to footer

How To Add Item In Custom Listview In Onactivityresult Method?

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

Solution 1:

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 = newImageAndTextAdapter(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

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) 
{
    // TODO Auto-generated method stubsuper.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
        }
    }
}

Solution 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 = newImageAndTextAdapter(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(); // 
}

Post a Comment for "How To Add Item In Custom Listview In Onactivityresult Method?"