E/RecyclerView: No adapter attached; skipping layout (Using Fragment)

You can post your problem related to Computer Software Projects here. We will try our best to help you out.
Post Reply
Robert
Posts: 29
Joined: Sat Feb 18, 2017 7:21 am

E/RecyclerView: No adapter attached; skipping layout (Using Fragment)

Post by Robert » Thu Mar 09, 2017 8:54 am

I have been reading the different answers here on stackoverflow and on this blog post and tried to implement their solutions but I am still getting the error:No adapter attached; skipping layout. help me.

Code: Select all

package vn.jupviec.frontend.android.monitor.app.ui.candidate;

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.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.w3c.dom.Text;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import vn.jupviec.frontend.android.monitor.app.R;
import vn.jupviec.frontend.android.monitor.app.domain.candidate.TrainingDTO;
import vn.jupviec.frontend.android.monitor.app.util.Utils;

/**
 * Created by Windows 10 Gamer on 16/12/2016.
 */


    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
        private static final String TAG = "RecyclerView";
        private List<TrainingDTO> mTrainingDTOs;
        private Context mContext;
        private LayoutInflater mLayoutInflater;
        private String today;
        private static final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        AdapterInterface buttonListener;
        public RecyclerAdapter(Context context, List<TrainingDTO> datas, AdapterInterface buttonListener) {
            mContext = context;
            mTrainingDTOs = datas;
            this.mLayoutInflater = LayoutInflater.from(mContext);
            this.buttonListener = buttonListener;
        }

        @Override
        public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.candidate_item, parent, false);
           // View itemView = mLayoutInflater.inflate(R.layout.candidate_item, parent, false);
            return new RecyclerViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(RecyclerViewHolder holder, int position) {
            final ObjectMapper mapper = new ObjectMapper();
            final TrainingDTO trainingDTO = mapper.convertValue(mTrainingDTOs.get(position), TrainingDTO.class);
            holder.tvName.setText(trainingDTO.getMaidName());
            holder.status.setVisibility(View.GONE);
            Date date = new Date();
            String currentDate = sdf.format(date);
            for (TrainingDTO.TrainingDetailDto td : trainingDTO.getListTrain()) {
                today = Utils.formatDate((long) td.getTrainingDate(),"dd/MM/yyyy");
                break;
            }
            if(currentDate.equals(today)) {
                holder.status.setVisibility(View.VISIBLE);
                holder.status.setText("(Mới)");
            }
            holder.btnHistory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext,R.string.msg_no_candidate_history,Toast.LENGTH_SHORT).show();
                }
            });
            holder.btnTest.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    buttonListener.showComment(trainingDTO);

                }
            });

        }

        @Override
        public int getItemCount() {
            int size ;

            if(mTrainingDTOs != null && !mTrainingDTOs.isEmpty()) {

                size = mTrainingDTOs.size();
            }
            else {

                size = 0;

            }
            return size;

        }

        class RecyclerViewHolder extends RecyclerView.ViewHolder {
            private TextView tvName;
            private Button btnTest;
            private Button btnHistory;
            private TextView status;

            public RecyclerViewHolder(View itemView) {
                super(itemView);
                tvName = (TextView) itemView.findViewById(R.id.txtName);
                btnTest = (Button) itemView.findViewById(R.id.btnTest);
                btnHistory = (Button) itemView.findViewById(R.id.btnHistory);
                status = (TextView)itemView.findViewById(R.id.status);

            }
        }
    public boolean removeItem(int position) {
        if (mTrainingDTOs.size() >= position + 1) {
            mTrainingDTOs.remove(position);
            return true;
        }
        return false;
    }
    public interface AdapterInterface {
        void showComment(TrainingDTO trainingDTO);
        void showHistory(TrainingDTO trainingDTO);
        }


}




    package vn.jupviec.frontend.android.monitor.app.ui.candidate;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import vn.jupviec.frontend.android.monitor.app.R;
import vn.jupviec.frontend.android.monitor.app.domain.ResultDTO;
import vn.jupviec.frontend.android.monitor.app.domain.candidate.TrainingDTO;
import vn.jupviec.frontend.android.monitor.app.domain.training.CandidateService;
import vn.jupviec.frontend.android.monitor.app.util.Constants;
import vn.jupviec.frontend.android.monitor.app.util.Utils;

/**
 * Created by Windows 10 Gamer on 09/12/2016.
 */

public class FragmentTapNew extends Fragment implements RecyclerAdapter.AdapterInterface {
    private static final String TAG = FragmentTapNew.class.getSimpleName();
    Activity myContext = null;
    private OnItemSelectedListener listener;
    ShapeDrawable shapeDrawable;
    @BindView(R.id.lvToday)
    RecyclerView lvToday;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.pb_loading)
    ProgressBar pbLoading;
    private Unbinder unbinder;
    private boolean loading = true;
    int pastVisiblesItems, visibleItemCount, totalItemCount;
    ArrayList<TrainingDTO> mTrainingDTO ;
    RecyclerAdapter mTrainingDTOAdapter;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_candidate_training, container, false);
        unbinder = ButterKnife.bind(this, v);
        initViews();

        return v;
    }
    private void initViews() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        lvToday.setHasFixedSize(true);
        lvToday.setNestedScrollingEnabled(false);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                today();
            }
        });
        t.start();
    }

    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            Toast.makeText(getActivity(), R.string.err_cannot_establish_connection, Toast.LENGTH_SHORT).show();
            return false;
        } else
            return true;
    }
    private void today() {
        if(isNetworkConnected()){
        String token = "a";
        Integer date = 0;
        setLoadingVisible(true);
        CandidateService.getApiDummyClient(getContext()).getCandidate(
                token,
                date,
                Constants.TRAINING,
                Arrays.asList(Constants.TRAINING_DAY_NEW),

                new Callback<ResultDTO>() {
                    @Override
                    public void success(ResultDTO resultDTO, Response response) {
                        setLoadingVisible(false);
                        mTrainingDTO = (ArrayList<TrainingDTO>) resultDTO.getData();
                        if (mTrainingDTO.size() == 0) {
                            if(textView!=null) {
                                textView.setVisibility(View.VISIBLE);
                                textView.setTextColor(Color.RED);
                                textView.setGravity(Gravity.CENTER | Gravity.BOTTOM);
                            }

                        } else {
                            if(textView!=null) {
                                textView.setVisibility(View.GONE);
                            }
                            if(null==mTrainingDTOAdapter) {
                                mTrainingDTOAdapter = new RecyclerAdapter(getActivity(), mTrainingDTO, FragmentTapNew.this);
                                lvToday.setAdapter(mTrainingDTOAdapter);

                            } else {
                                lvToday.setAdapter(mTrainingDTOAdapter);
                                mTrainingDTOAdapter.notifyDataSetChanged();
                            }
                                lvToday.invalidate();
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        Log.e(TAG, "JV-ERROR: " + error.getMessage());
                        Log.e(TAG, "JV-ERROR: " + error.getSuccessType());
                    }
                });
    }
    }


    @Override
    public void onResume() {
        setLoadingVisible(false);
        initViews();
        lvToday.setAdapter(mTrainingDTOAdapter);
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    private void setLoadingVisible(boolean visible) {
        // pbLoading.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
        if(pbLoading!=null) {
            pbLoading.setVisibility(visible ? View.VISIBLE : View.GONE);
            lvToday.setVisibility(visible ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof OnItemSelectedListener) {
            listener = (OnItemSelectedListener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + " phải implemenet MyListFragment.OnItemSelectedListener");
        }
    }

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

    @Override
    public void showComment(TrainingDTO trainingDTO) {
        Intent intent = new Intent(getContext(), CommentActivity.class);
        intent.putExtra(Constants.ARG_NAME_DETAIL, trainingDTO);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(intent);
    }

    @Override
    public void showHistory(TrainingDTO trainingDTO) {
        Intent intent = new Intent(getContext(), HistoryActivity.class);
        intent.putExtra(Constants.ARG_CANDIDATE_HISTORY_DETAIL, trainingDTO);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(intent);
    }

    public interface OnItemSelectedListener {
        void showComment(TrainingDTO trainingDTO);

        void showHistory(TrainingDTO trainingDTO);

    }

}

Post Reply