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

问题描述:

我使用抽球将json提取到recyclerview中,但它说/ RecyclerView:没有附加适配器;跳过布局,但它显示了模拟器上最后一个json对象的一行。我运行了Debug,setter和getter variabe刚刚收到最后一个对象。/RecyclerView:没有附加适配器;跳过布局,但只有一行显示在模拟器

我不知道如何解决这个问题。一直在处理这一切的一天,我是个初学者,

这是我的主要

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

代码这是我的二传手和吸气剂类

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


} 

这是配置类

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

尝试设置一个空适配器第一则更新当你有你的数据

为了进一步的说明,请访问:

recyclerview No adapter attached; skipping layout

No adapter attached; skipping layout

+0

我尝试使用适配器。 notifyDataSetChanged();但模拟器上没有任何显示。我运行一个调试和这个“listSuperHeroes.add(superHero);” MainActivity上的代码“超级英雄”只有一个对象,JSON获取没有问题,它获取所有对象,但它只保存该变量上的一个对象“超级英雄”,这是最后一个对象 –

+0

它只保存一个对象在“超级英雄“变量 –

+0

请访问下面的链接,我认为他们会对你非常有用! –