2016-08-18 8 views
0

ボレーを使用してjsonをrecyclerviewにフェッチしていますが、/ RecyclerView:アダプターは接続されていません。レイアウトをスキップしますが、最後のjsonオブジェクトであるエミュレータ上に1つの行が表示されます。私はデバッグを実行し、最後のオブジェクトを受け取ったばかりのsetterとgetterのvariabe。/RecyclerView:アダプターは接続されていません。レイアウトをスキップしますが、エミュレータには1行しか表示されていません

これを解決する方法はわかりません。一日中これに対処、begginerイム、

これはこれはこれは

Configクラスで私のセッターとゲッタークラス

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

答えて

0

まずを空のアダプタを設定してみてください、あなたのデータを持っている時にさらに説明するために

をそれを更新ご覧ください

recyclerview No adapter attached; skipping layout

No adapter attached; skipping layout

+0

を、私は、アダプタを使用してみました。 notifyDataSetChanged();エミュレータには何も表示されません。私はデバッグを実行し、この "listSuperHeroes.add(スーパーヒーロー);" MainActivityのコードでは、 "スーパーヒーロー"は1つのオブジェクトしか持っていません。JSONのフェッチは問題ありません。すべてのオブジェクトをフェッチしますが、最後のオブジェクトである変数 "スーパーヒーロー"に1つのオブジェクトを保存します。 –

+0

"スーパーヒーロー "変数 –

+0

下記のリンクを参照してください。私は彼らがあなたにとってとても便利だと思います! –

関連する問題