0

I'm working on a page in which I have a button which takes you to the video gallery which I have created. From this you select some videos(thumbnails) and then hit the arrow to come back to the same place from where I hit the button to come to the Custom gallery with the selected videos.

Basically the map goes like this :

Button(from AddFragment)->VideoGallery->Select videos-> AddFragment with selected videos which is visible inside the recycler view

Now when I hit the button I come to my videogallery but when I select some videos from the gallery and come back to the AddFragment it shows nothing and one error comes up that is E/RecyclerView: No adapter attached; skipping layout

Now I have used Bundle to extract the sent data and populate it in my AddFragment.

I'm giving out the code for you :

1. AddFragment.java

public class AddFragment extends Fragment {

private ImageButton nextActivity;
private RecyclerView recyclerView;
private ProgressDialog videoProgressDialog;
ArrayList<File> checkedList = new ArrayList<>();
ImageAdapter imageAdapter;
Button button;

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);

    nextActivity = (ImageButton) view.findViewById(R.id.gotoButton);

    recyclerView = (RecyclerView) view.findViewById(R.id.grid_add_view);

    button = (Button) view.findViewById(R.id.buttonToGallery);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(getContext(),VideoGalleryActivity.class));
        }
    });

    return view;
}

//making adapter for RecyclerView which loads the desired files
class ImageAdapter extends RecyclerView.Adapter<ViewHolder>{

    private LayoutInflater mInflater;

    private Bitmap bitmap;

    private ArrayList<File> fileName;

    public ImageAdapter(ArrayList<File> checkedList) {

        fileName = checkedList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Log.e("ADAPTER SETTING","DOING");
        //getting the passed value from videogallery
        Bundle bundle = new Bundle();
        ArrayList<String> getValue = bundle.getStringArrayList("sendData");
        Log.e("RECEIVED_DATA======",getValue.toString());

        //adding the files to the list
        for(String pathName : getValue){
            File filePath = new File(pathName);
            checkedList.add(filePath);
        }

        //setting the adapter
        imageAdapter = new ImageAdapter(checkedList);
        recyclerView.setAdapter(imageAdapter);
        recyclerView.setVisibility(View.VISIBLE);

        View items = mInflater.from(parent.getContext()).inflate(R.layout.custom_added_video,
                parent,false);
        items.setLayoutParams(new AbsListView.LayoutParams(215,215));
        return new ViewHolder(items);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if(fileName != null){
            bitmap = ThumbnailUtils.createVideoThumbnail(fileName.get(position).toString(),1);
            holder.imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public int getItemCount() {
        return fileName.size();
    }
}

class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    //public ImageButton imageButton;

    public ViewHolder(View itemView) {
        super(itemView);

        //imageButton = (ImageButton) itemView.findViewById(R.id.addVideos);
        imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
    }
} }

2. fragment_add.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/colorWhite"
tools:context="in.pinelane.myhovi.AddFragment">

<!-- TODO: Update blank fragment layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/linearLayout"
    android:padding="19dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17dp"
        android:textStyle="bold"
        android:text="Choose Videos"
        android:textColor="@color/colorBackground"/>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/gotoButton"
        android:layout_width="25sp"
        android:layout_height="25sp"
        android:background="#00ffffff"
        android:src="@mipmap/ic_arrow_forward_black_24dp"/>
</LinearLayout>

<Button
    android:id="@+id/buttonToGallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Gallery"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/grid_add_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" />

Inflates this layout custom_added_video.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorWhite"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp">

<!--<ImageButton-->
    <!--android:id="@+id/addVideos"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:src="@mipmap/ic_add_black_24dp"-->
    <!--android:layout_margin="3dp"-->
    <!--android:background="@drawable/edittext_border"-->
    <!--android:layout_centerInParent="true"/>-->

<ImageView
    android:id="@+id/galleryImageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:layout_margin="3dp"
    android:layout_centerInParent="true"/>

Any idea would be of great help! Thanks

I've tried defining the below code in onCreate but I got a NullPointerException so I defined the same inside the onCreateViewHolder but no result just No Adapter attached; skipping layout

Log.e("ADAPTER SETTING","DOING");
        //getting the passed value from videogallery
        Bundle bundle = new Bundle();
        ArrayList<String> getValue = bundle.getStringArrayList("sendData");
        Log.e("RECEIVED_DATA======",getValue.toString());

        //adding the files to the list
        for(String pathName : getValue){
            File filePath = new File(pathName);
            checkedList.add(filePath);
        }

        //setting the adapter
        imageAdapter = new ImageAdapter(checkedList);
        recyclerView.setAdapter(imageAdapter);
        recyclerView.setVisibility(View.VISIBLE);

EDITS

I've made some changes inside my code and user onActivityResult and startActivityForResult in this fragment but still no result. My AddFragment is still blank and not showing any result

Edited AddFragment.java

public class AddFragment extends Fragment {

private ImageButton nextActivity;
private RecyclerView recyclerView;
ArrayList<File> checkedList = new ArrayList<>();
ImageAdapter imageAdapter;
Button button;
private static final int CustomGallerySelectId = 1;//Set Intent Id

public AddFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add, container, false);

    nextActivity = (ImageButton) view.findViewById(R.id.gotoButton);

    recyclerView = (RecyclerView) view.findViewById(R.id.grid_add_view);

    button = (Button) view.findViewById(R.id.buttonToGallery);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(new Intent(getContext(),VideoGalleryActivity.class),CustomGallerySelectId);
        }
    });

    //setting the adapter
    imageAdapter = new ImageAdapter(checkedList);
    GridLayoutManager videoGrid = new GridLayoutManager(getContext(),3);
    recyclerView.setLayoutManager(videoGrid);
    recyclerView.setAdapter(imageAdapter);
    recyclerView.setVisibility(View.VISIBLE);

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode){
        case CustomGallerySelectId :
            if(requestCode == RESULT_OK){
                Log.e("ADAPTER SETTING","DOING");
                //getting the passed value from videogallery
                Bundle bundle = new Bundle();
                ArrayList<String> getValue = bundle.getStringArrayList("sendData");
                Log.e("RECEIVED_DATA======",getValue.toString());

                //adding the files to the list
                for(String pathName : getValue) {
                    File filePath = new File(pathName);
                    checkedList.add(filePath);
                }

            }
    }
}

//making adapter for RecyclerView which loads the desired files
class ImageAdapter extends RecyclerView.Adapter<ViewHolder>{

    private LayoutInflater mInflater;

    private Bitmap bitmap;

    private ArrayList<File> fileName;

    public ImageAdapter(ArrayList<File> checkedList) {

        fileName = checkedList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View items = mInflater.from(parent.getContext()).inflate(R.layout.custom_added_video,
                parent,false);
        items.setLayoutParams(new AbsListView.LayoutParams(215,215));
        return new ViewHolder(items);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if(fileName != null){
            bitmap = ThumbnailUtils.createVideoThumbnail(fileName.get(position).toString(),1);
            holder.imageView.setImageBitmap(bitmap);
        }
    }

    @Override
    public int getItemCount() {
        return fileName.size();
    }
}

class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    //public ImageButton imageButton;

    public ViewHolder(View itemView) {
        super(itemView);

        //imageButton = (ImageButton) itemView.findViewById(R.id.addVideos);
        imageView = (ImageView) itemView.findViewById(R.id.galleryImageView);
    }
} }
Alok
  • 6,066
  • 5
  • 33
  • 69

1 Answers1

0

Move this code:

    //setting the adapter
    imageAdapter = new ImageAdapter(checkedList);
    recyclerView.setAdapter(imageAdapter);
    recyclerView.setVisibility(View.VISIBLE);

to your createView() method.

In createView you are inflating the layout, you are finding the RecyclerView but you are not setting an adapter to it. The RecyclerView doesn't have any data to show, so it's skipped from the layout for a performance boost. You should also set a LayoutManager. If you need a simple list, you should use LinearLayoutManager.

The createViewHolder() method is called once for each visible element of your RecyclerView. It's not the right place to perform the initial setup of your RecyclerView.

Danail Alexiev
  • 6,536
  • 1
  • 16
  • 27
  • I've done some the changes like this but still no result : //setting the adapter imageAdapter = new ImageAdapter(checkedList); GridLayoutManager videoGrid = new GridLayoutManager(getContext(),3); recyclerView.setLayoutManager(videoGrid); recyclerView.setAdapter(imageAdapter); recyclerView.setVisibility(View.VISIBLE); – Alok Aug 17 '17 at 10:44
  • Are you actually adding any items to your adapter? – Danail Alexiev Aug 17 '17 at 10:48
  • I've made some changes in my code and yes in my ImageAdapter all the things are defined which is the data that came from the VideoGallery – Alok Aug 17 '17 at 10:55
  • You are not updating your adapter items in the correct way in `onActivityResult`. Check this thread https://stackoverflow.com/questions/30053610/best-way-to-update-data-with-a-recyclerview-adapter for details – Danail Alexiev Aug 17 '17 at 11:02
  • I've done this. There was a problem inside the putExtra, I was not sending the data in putArrayListExtra() – Alok Aug 17 '17 at 12:04