2016-06-01 9 views
-1

RecyclerViewVolleyを使用してローカルサーバーからJSONデータを入力しようとしていますが、RecyclerViewは表示されず、エラーはlogcatに表示されません。ここでJSON解析後のRecyclerViewがありません

は私のコードは次のとおりです。

MainActivity.java

List<Products> productsList; 
RecyclerView recyclerView; 

... 

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

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    toolbar2 = (Toolbar) findViewById(R.id.toolbar2); 

    setSupportActionBar(toolbar2); 

    recyclerView = (RecyclerView) findViewById(R.id.main_list); 
    recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 

    final DataAdapter dataAdapter = new DataAdapter(productsList); 
    recyclerView.setAdapter(dataAdapter); 

    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
    String url = "http://192.168.9.120/MenProducts.json"; 

    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray response) { 
      try { 
       if (response.length() > 0) { 
        productsList.clear(); 
        for (int i = 0; i < response.length(); i++) { 
         JSONObject jsonObject = response.getJSONObject(i); 
         Products products = new Products(); 
         if (!jsonObject.isNull("name")) { 
          products.name = jsonObject.getString("name"); 
         } 
         if (!jsonObject.isNull("age")) { 
          products.Departments = jsonObject.getString("age"); 
         } 
         productsList.add(i, products); 
        } 
        dataAdapter.notifyDataSetChanged(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      System.out.println(error.getMessage()); 
      Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 

    requestQueue.add(jsonArrayRequest); 
} 

DataAdapter.java

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ProductsViewHolder> { 

private List<Products> products; 

public DataAdapter(List<Products> products){ 
    this.products = products; 
} 

@Override 
public ProductsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i){ 
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.tile_view, viewGroup, false); 
    ProductsViewHolder pvh = new ProductsViewHolder(view); 
    return pvh; 
} 

@Override 
public void onBindViewHolder(ProductsViewHolder viewHolder, int i){ 
    viewHolder.item_title.setText(products.get(i).getTitle()); 
    viewHolder.item_desc.setText(String.valueOf(products.get(i).getDepartments())); 
} 

@Override 
public int getItemCount(){ 
    if(products != null){ 
     return products.size(); 
    } 
    return 0; 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView){ 
    super.onAttachedToRecyclerView(recyclerView); 
} 

public static class ProductsViewHolder extends RecyclerView.ViewHolder{ 

    TextView item_title; 
    TextView item_desc; 
    CardView cv; 

    ProductsViewHolder(View view){ 
     super(view); 

     cv = (CardView) view.findViewById(R.id.note_item); 
     item_title = (TextView) view.findViewById(R.id.item_title); 
     item_desc = (TextView) view.findViewById(R.id.item_desc); 
    } 
    } 
} 

Products.java

public class Products { 

public String name; 
public String Departments; 
public String info; 
public Float rate; 
public Double price; 

public Products(){ 

} 

public Products(String name, String Departments, String info, Float rate, Double price){ 
    this.name = name; 
    this.info = info; 
    this.price = price; 
    this.rate = rate; 
    this.Departments = Departments; 
} 

public String getTitle(){ 
    return name; 
} 

public void setTitle(String name){ 
    this.name = name; 
} 

public String getDepartments(){ 
    return Departments; 
} 

public void setDepartments(String Departments){ 
    this.Departments = Departments; 
} 


public String getInfo(){ 
    return info; 
} 

public void setInfo(String info) { 
    this.info = info; 
} 

public Float getRate(){ 
    return rate; 
} 

public void setRate(Float rate) { 
    this.rate = rate; 
} 

public Double getPrice(){ 
    return price; 
} 

public void setPrice(Double price) { 
    this.price = price; 
} 
} 

これはあなたの現在のgetItemCountは常に製品のデータが含まれている場合でも、nullを返し

{ 
"Products": [ 
    { 
     "Departments": "Grooming & Health", 

     "category": [ 

      { 
       "name": "Shave" 
      }, 

      { 
       "name": "Skin Care" 
      }, 

      { 
       "name": "Hair Care" 
      }, 

      { 
       "name": "Body Care" 
      }, 

      { 
       "name": "Oral Care" 
      }, 

      { 
       "name": "Cologne" 
      }, 

      { 
       "name": "Kits and Sets" 
      }, 

      { 
       "name": "Luxury Grooming" 
      } 
     ] 
    }, 

     { 
      "Departments": "Clothing, Shoes & Jewelry", 

      "category": [ 

       { 
        "name": "Clothing" 
       }, 

       { 
        "name": "Shoes" 
       }, 

       { 
        "name": "Jewelry" 
       }, 

       { 
        "name": "Watches" 
       }, 

       { 
        "name": "Accessories" 
       } 
      ] 
     }, 

    { 
     "Departments": "Clothing, Shoes & Jewelry", 

     "category": [ 

      { 
       "name": "Clothing" 
      }, 

      { 
       "name": "Shoes" 
      }, 

      { 
       "name": "Jewelry" 
      }, 

      { 
       "name": "Watches" 
      }, 

      { 
       "name": "Accessories" 
      } 
     ] 
    }, 
] 
} 
+0

本当に間違いはありませんか?あなたはJSONオブジェクトを持っていますが、JsonArrayRequestを使用しています。 –

+0

@ cricket_007私はJSONを修正して同じ問題を抱えています。どうすればJsonObjectへのJsonArrayRequestとその実装を変更できますか? –

+0

JsonObjectRequestを試してみることもできますが、最初のコメントで言ったように、エラーはないと思いますか?たぶんStringRequestを最初に使用し、単にレスポンスを出力して、必要なデータを取得していることを確認してください。 –

答えて

0

さて、が提案cricket_007 ように、私は私のJSONファイルにいくつかの他のコードの参照オブジェクトとともにJsonArrayRequestedからJsonObjectRequestedにそれを変更し、私は最終的にそれが動作するようになりました。

0

MensProducts.json

JSONファイルです。それでこれはやります。

@Override  
    public int getItemCount(){ 
     if(products== null){ 

     return 0; 

      } 
    else 
    { 
    return products.size(); 
    } 
} 
+0

コードと彼のコードがどのように違うのですか? – Bharatesh

関連する問題