没有适配器附加跳过片段中的recyclerview

时间:2016-10-05 12:29:59

标签: android android-fragments android-adapter

我的应用程序运行良好,但android工作室给我1消息然后接受后似乎RecyclerView无法正常工作! 这是我的错误:RecyclerView:没有附加适配器;跳过布局

我的片段:

    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;
    }
}

我的适配器:

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();
}

1 个答案:

答案 0 :(得分:0)

可能的解决方案就像我之前写的那样,首先创建适配器并在之后添加数据并通知适配器。 我还为loadJson()添加了一个Runnable来阻止NetworkOnMainThreadException(可能是他自己改造的,但仅限于此情况)。

div