0

When I try to add RecyclerView in the sample application, I get an error E/RecyclerView: No adapter attached; skipping layout.

Because of this, the data which is successfully retrieved from my server does not appear on the screen (empty)

This in my code

MainActivity

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import page.DefaultFragment;
import page.HomeFragment;
import page.ProfileFragment;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    private final static String API_KEY = "7e8f60e325cd06e164799af1e317d7a7";

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        Fragment fragment = null;
        fragment = new DefaultFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.root, fragment);
        fragmentTransaction.commit();

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        Fragment fragment = null;
        String title = getString(R.string.app_name);

        if (id == R.id.nav_camera) {
            fragment = new HomeFragment();
            title = "Home";
        } else if (id == R.id.nav_gallery) {
            fragment = new ProfileFragment();
            title = "Profile";
        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.root, fragment);
            fragmentTransaction.commit();

            getSupportActionBar().setTitle(title);
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    public void showToast(String output){
        Toast.makeText(this.getBaseContext(), output, Toast.LENGTH_SHORT).show();
    }

}

My Fragment code

package page;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.transvision.bertho.transvisiondashboardapp.R;

import java.util.List;

import adapter.ChannelAdapter;
import model.Channel;
import model.ChannelResponse;
import rest.ApiClient;
import rest.ApiInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class HomeFragment extends Fragment {

    private RecyclerView recyclerView;

    private static final String TAG = HomeFragment.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        getChannelData();

        // Inflate the layout for this fragment
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public void getChannelData() {
        ApiInterface apiService = ApiClient.getChannel().create(ApiInterface.class);
        Call<ChannelResponse> call = apiService.getItems();

        call.enqueue(new Callback<ChannelResponse>() {
            @Override
            public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
                int statusCode = response.code();
                List<Channel> channel = response.body().getItems();
                recyclerView.setAdapter(new ChannelAdapter(channel, R.layout.list_channel, getActivity()));
            }

            @Override
            public void onFailure(Call<ChannelResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
                showToast("CONNECTION ERROR");
            }
        });
    }

    public void showToast(String output){
        Toast.makeText(getActivity(), output, Toast.LENGTH_SHORT).show();
    }
}

My adapter

package adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.transvision.bertho.transvisiondashboardapp.R;

import java.util.List;

import model.Channel;


public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> {

    private List<Channel> channels;
    private int rowLayout;
    private Context context;

    public static class ChannelViewHolder extends RecyclerView.ViewHolder {

        LinearLayout moviesLayout;
        TextView movieTitle;
        TextView data;
        TextView movieDescription;
        TextView rating;

        TextView name;
        TextView code;
        TextView description;
        TextView number;
        TextView definition;
        TextView paket;
        ImageView logo;

        public ChannelViewHolder(View v) {
            super(v);
            moviesLayout = (LinearLayout) v.findViewById(R.id.movies_layout);
            name = (TextView) v.findViewById(R.id.title);
            definition = (TextView) v.findViewById(R.id.subtitle);
            description = (TextView) v.findViewById(R.id.description);
//            rating = (TextView) v.findViewById(R.id.rating);
        }
    }

    public ChannelAdapter(List<Channel> channels, int rowLayout, Context context) {
        this.channels = channels;
        this.rowLayout = rowLayout;
        this.context = context;
    }

    @Override
    public ChannelAdapter.ChannelViewHolder onCreateViewHolder(ViewGroup parent,
                                                            int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
        return new ChannelViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ChannelViewHolder holder, final int position) {
        holder.name.setText(channels.get(position).getName());
        holder.definition.setText(channels.get(position).getDefinition());
        holder.description.setText(channels.get(position).getDescription());
//        holder.rating.setText(channels.get(position).getVoteAverage().toString());
    }

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

}

Is there anything wrong in my code? Or wrong in code placement?

Thanks

Bertho Joris
  • 1,243
  • 3
  • 22
  • 53
  • code of your adapter will help finding the issue – Saeed Jassani Jun 23 '16 at 17:16
  • Hi @SaeedJassani I already add my adapter above – Bertho Joris Jun 23 '16 at 17:18
  • your adapter code is fine... are you sure that the list "channels" is not empty? try logging.... No adapter attached; skipping layout has nothing to do with this AFAIK – Saeed Jassani Jun 23 '16 at 17:23
  • @SaeedJassani My code like this is working fine when I run in Activity Level. And the data is show. but when I try to insert inside Fragment, become like this (not show to screen). – Bertho Joris Jun 23 '16 at 17:25
  • are you able to see your fragment?? try a log statement in the onCreateView method of fragment to ensure that you are attaching it properly – Saeed Jassani Jun 23 '16 at 17:29
  • Possible duplicate of [recyclerview No adapter attached; skipping layout](http://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – blizzard Jun 23 '16 at 17:31
  • In `onCreateView` you should call `recyclerView.setAdapter` with an empty list – Juan Cruz Soler Jun 23 '16 at 17:35
  • Hey, the recyclerview gets its adapter just when you get a respond from the server, It's a better practice to create an adapter (even an empty one) and bind it to recyclerview, then update it in the response listener. – Farshad Tahmasbi Jun 23 '16 at 17:38
  • Hi @FarShaD can you give me some code? – Bertho Joris Jun 23 '16 at 17:40
  • @BerthoJoris Create an empty adapter in `onCreateView` (pass an empty list to it) and set it to the `recyclerview`, Then in the listener body update it (in case you get a success response), check this: http://stackoverflow.com/questions/30053610/best-way-to-update-data-with-a-recyclerview-adapter?answertab=oldest#tab-top – Farshad Tahmasbi Jun 23 '16 at 17:47
  • @FarShaD How about using my adapter above? `ChannelAdapter` – Bertho Joris Jun 23 '16 at 17:56
  • The approach is the same my friend, You're getting async data from server and won't setAdapter until the response arrives (if doesn't fail!) and this is not a good idea, So create and set your adapter before calling getChannelData(), the in response listener body update it ! – Farshad Tahmasbi Jun 23 '16 at 18:07
  • @FarShaD Is an adapter that will be new create, empty the contents of which must be the same structure with the existing ones? I mean same like `ChannelAdapter` ? Sorry for newbie – Bertho Joris Jun 23 '16 at 18:14
  • Hi @FarShaD do you mean like this? https://s31.postimg.org/p0r7y4xzf/contoh.png – Bertho Joris Jun 23 '16 at 18:35

1 Answers1

0

Getting data from internet is an async task and may takes too long, here you don't bind any adapter to the recyclerView until you get a response and this is not the usual right way, If you don't get any response for some reasons (e.g no internet connection), then recyclerView.setAdapter won't run, So I suggest you to create an empty adapter first, Then whenever you get response just update the adapter :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    //Pass empty list to (predefined) adapter
    adapter = new ChannelAdapter (new ArrayList<>(), rowLayout, getActivity());
    //Bind it to recyclerView
    recyclerView.setAdapter(adapter);

    getChannelData();
    // Inflate the layout for this fragment
    return rootView;
}

Add some method to your adapter in case you want add Channel :

public void addChannel(Channel channel) {
channels.add(channel);
notifyDatasetChanged();//even better notifyItemInserted();
}

Now in your response listener add the response body that you got to the adapter:

@Override
        public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {

            //Use a loop to add channels you got
            adapter.addChannel(responsebodyItems);
        }
Farshad Tahmasbi
  • 2,755
  • 2
  • 25
  • 42
  • Hi @FarShaD I tried to follow the steps you give above but I get some error : https://s31.postimg.org/e3kzzalbv/contoh.png – Bertho Joris Jun 23 '16 at 18:58