/ RecyclerView:没有连接适配器;跳过布局,但只有一行显示在模拟器上

时间:2016-08-18 15:34:04

标签: android json android-recyclerview android-volley

我使用volley将json提取到recyclerview但它说/ RecyclerView:没有连接适配器;跳过布局,但它在模拟器上显示一行,这是最后一个json对象。我运行Debug,setter和getter变量刚收到最后一个对象。

我不知道如何解决这个问题。我一整天都在处理这个问题,

这是我在Main上的代码

public class MainActivity extends Activity {



     TextView text;
        String image_sm;
        private List<SuperHeroes> listSuperHeroes;
       // ArrayList<String> image_smm;
        //Creating Views
        private RecyclerView recyclerView;
       RecyclerView.LayoutManager layoutManager;
        private RecyclerView.Adapter adapter;
        SuperHeroes superHero = new SuperHeroes();

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

            recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            layoutManager = new LinearLayoutManager(this);


            listSuperHeroes = new ArrayList<>();

            getData();
        }


        private void getData(){
            //Showing a progress dialog



            JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,Config.DATA_URL,null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject req) {
                            //Dismissing progress dialog
                            final ProgressDialog loading = ProgressDialog.show(MainActivity.this,"Loading Data", "Please wait...",false,false);
                            loading.dismiss();

                            //calling method to parse json array
                            parseData(req);

                            listSuperHeroes.add(superHero);
                            recyclerView.setLayoutManager(layoutManager);
                            adapter = new CardAdapter(listSuperHeroes, MainActivity.this);
                            recyclerView.setAdapter(adapter);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d("HELLO BIGBENTA", "Error: " + error.getMessage());

                        }
                    });

    //        //Creating request queue
    //        RequestQueue requestQueue = Volley.newRequestQueue(this);
    //
    //        //Adding request to the queue
    //        requestQueue.add(req);

            AppController.getInstance().addToRequestQueue(req);
        }


        private void parseData(JSONObject req) {

            try {
                // Parsing json array response
                // loop through each json object


                JSONArray json = req
                        .getJSONArray("worldpopulation");


                for (int i = 0; i < json.length(); i++) {

                    JSONObject jsonOb = json
                            .getJSONObject(i);

                    superHero.setrank(jsonOb.getInt(Config.TAG_rank));
                    superHero.setcountry(jsonOb.getString(Config.TAG_country));
                    superHero.setpopulation(jsonOb.getString(Config.TAG_population));
                    superHero.setflag(jsonOb.getString(Config.TAG_flag));
                    //superHero.setFirstAppearance(json.getString(Config.TAG_image_sm));


                }




            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

这是我的Setter和Getters类

public class SuperHeroes {


    private int rank;
    private String country;
    private String population;
    private String flag;


    public int getrank() {
        return rank;
    }

    public void setrank(int rank) {
        this.rank = rank;
    }



    public String getcountry() {
        return country;
    }

    public void setcountry(String country) {
        this.country = country;
    }

    public String getpopulation() {
        return population;
    }

    public void setpopulation(String population) {
        this.population = population;
    }

    public String getflag() {
        return flag;
    }

    public void setflag(String flag) {
        this.flag = flag;
    }


}

这是Config类

     public class Config {
    public static final String DATA_URL = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";

    //Tags for my JSON

    public static final String TAG_rank= "rank";
    public static final String TAG_country = "country";
    public static final String TAG_population = "population";
    public static final String TAG_flag = "flag";
    public static final String TAG_image_sm= "image_sm";

}

这是适配器类

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

    private ImageLoader imageLoader;
    private Context context;

    //List of superHeroes
    List<SuperHeroes> superHeroes;

    public CardAdapter(List<SuperHeroes> superHeroes, Context context){
        super();
        //Getting all the superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.superheroes_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

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

        SuperHeroes superHero =  superHeroes.get(position);

        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getflag(), ImageLoader.getImageListener(holder.imageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

        holder.imageView.setImageUrl(superHero.getflag(), imageLoader);

        holder.textViewRank.setText(String.valueOf(superHero.getrank()));
        holder.textViewRealName.setText(superHero.getcountry());
        holder.textViewCreatedBy.setText(superHero.getpopulation());

    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewRank;
        public TextView textViewRealName;
        public TextView textViewCreatedBy;
        public TextView textViewFirstAppearance;
        public TextView  textViewPowers;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
            textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
            textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
            textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
            textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试先设置空适配器,然后在获得数据时更新

如需进一步说明,请访问:

recyclerview No adapter attached; skipping layout

No adapter attached; skipping layout