0

my app working well but android studio give me 1message then after accept that seems RecyclerView not work! this is my error : RecyclerView: No adapter attached; skipping layout

my fragment :

    public class news extends Fragment {

    private RecyclerView recyclerView;
    private ArrayList<Deatails> data;
    private DataAdapter adapter;
    private View myFragmentView;

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

        myFragmentView = inflater.inflate(R.layout.news, container, false);

        initViews();
        return myFragmentView;
    }

    private void initViews() {
        recyclerView = (RecyclerView) myFragmentView.findViewById(R.id.card_recycler_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);

        data = new ArrayList<Deatails>();
        adapter = new DataAdapter(getActivity(), data);
        recyclerView.setAdapter(adapter);



        new Thread()
        {
            public void run()
            {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        loadJSON();
                    }
                });

            }
        }
        .start();

    }
//        Thread t = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                loadJSON();
//            }
//        });
//        t.start();



    private void loadJSON() {
        if (isNetworkConnected()){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://memaraneha.ir")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RequestInterface request = retrofit.create(RequestInterface.class);
            Call<JSONResponse> call = request.getJSON();
            final ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.show();
            call.enqueue(new Callback<JSONResponse>() {
                @Override
                public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                    progressDialog.dismiss();
                    JSONResponse jsonResponse = response.body();
                    data.addAll(Arrays.asList(jsonResponse.getAndroid()));
                    adapter.notifyDataSetChanged();
                }
                @Override
                public void onFailure(Call<JSONResponse> call, Throwable t) {
                    progressDialog.dismiss();
                    Log.d("Error", t.getMessage());
                }
            });
        }
        else {
            Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show();}
    }
    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
            return false;
        } else
            return true;
    }
}

my Adapter :

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
    private Context context;
    private ArrayList<Deatails> android;


    public DataAdapter(Context context,ArrayList<Deatails> android) {
        this.context = context;
        this.android = android;
    }

    @Override
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, final int i) {

        viewHolder.tv_name.setText(android.get(i).getName());
        viewHolder.tv_version.setText(android.get(i).getVer());
        viewHolder.tv_api_level.setText(android.get(i).getApi());

        // load image into imageview using glide
        Picasso.with(context).load("http://memaraneha.ir/Erfan/images/"+android.get(i).getPic()).resize(500,500)
//                .placeholder(R.drawable.truiton)
//                .error(R.drawable.truiton)
                .into(viewHolder.tv_image);

        viewHolder.tv_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context,"this is my pic",Toast.LENGTH_SHORT).show();

            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_name,tv_version,tv_api_level;
        private ImageView tv_image;


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

            tv_name = (TextView)view.findViewById(R.id.tv_name);
            tv_version = (TextView)view.findViewById(R.id.tv_version);
            tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
            tv_image= (ImageView) view.findViewById(R.id.img);
        }
    }
}

RequestInterface :

public interface RequestInterface {

    @GET("Erfan/news.php")
    Call<JSONResponse> getJSON();
}
Erfan
  • 2,344
  • 3
  • 19
  • 45

1 Answers1

0

Possible solution is as I wrote earlier to create the adapter first and add the data afterwards and notify the adapter about that. I also added a Runnable for loadJson() to prevent the NetworkOnMainThreadException (could be that retrofit does that on his own, but just for the case).

    public class news extends Fragment {

        private RecyclerView recyclerView;
        private ArrayList<Deatails> data;
        private DataAdapter adapter;
        private View myFragmentView;


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

            myFragmentView = inflater.inflate(R.layout.news, container, false);
            initViews();
            return myFragmentView;
        }

        private void initViews() {
            recyclerView = (RecyclerView)myFragmentView.findViewById(R.id.card_recycler_view);
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(layoutManager);

data = new ArrayList<Deatails>();
adapter = new DataAdapter(getActivity(), data);
            recyclerView.setAdapter(adapter);

            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    loadJSON();
                }
            });
            t.start();
        }
        private void loadJSON() {
            if (isNetworkConnected()){
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://memaraneha.ir")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                RequestInterface request = retrofit.create(RequestInterface.class);
                Call<JSONResponse> call = request.getJSON();
                final ProgressDialog progressDialog = new ProgressDialog(getActivity());
                progressDialog.show();
                call.enqueue(new Callback<JSONResponse>() {
                    @Override
                    public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                        progressDialog.dismiss();
                        JSONResponse jsonResponse = response.body();
                        data.addAll((Arrays.asList(jsonResponse.getAndroid()));
                        adapter.notifyDataSetChanged();
                    }
                    @Override
                    public void onFailure(Call<JSONResponse> call, Throwable t) {
                        progressDialog.dismiss();
                        Log.d("Error", t.getMessage());
                    }
                });
            }
            else {
                Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show();}
        }
        private boolean isNetworkConnected() {
            ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo ni = cm.getActiveNetworkInfo();
            if (ni == null) {
                // There are no active networks.
                return false;
            } else
                return true;
        }
    }
GoneUp
  • 376
  • 3
  • 14
  • i got this exception: news.DataAdapter.getItemCount(DataAdapter.java:60) and point from this line: return android.size(); in my adapter get item count – Erfan Oct 06 '16 at 07:24
  • Fixed. Forgot to initialize data. – GoneUp Oct 06 '16 at 07:47
  • java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – Erfan Oct 06 '16 at 08:11
  • at com.example.er.test.tabs.news.loadJSON(news.java:76) at com.example.er.test.tabs.news.access$000(news.java:34) at com.example.er.test.tabs.news$1.run(news.java:63) – Erfan Oct 06 '16 at 08:12
  • Cmon, the error message is exactly saying what you need to do. – GoneUp Oct 06 '16 at 08:41
  • i edit my question look . but have this message : Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ – Erfan Oct 07 '16 at 07:52
  • Retrofit (technically Okhttp) *does* run on its own thread. The callback is at `call.enqueue(new Callback() {` – OneCricketeer Oct 07 '16 at 08:00
  • @cricket_007 just answer my question and tell me where i must edit my codes – Erfan Oct 07 '16 at 08:12
  • @erfan I can't help you without seeing `RequestInterface`. Your last comment here says "malformed JSON at line 1 column 1", which obviously means you are not getting JSON data from the server – OneCricketeer Oct 07 '16 at 08:14
  • public interface RequestInterface { @GET("Erfan/news.php") Call getJSON(); } – Erfan Oct 07 '16 at 08:16
  • @cricket_007 i post this issue :http://stackoverflow.com/questions/39918814/use-jsonreader-setlenienttrue-to-accept-malformed-json-at-line-1-column-1-path – Erfan Oct 07 '16 at 13:47