JSON Parse之后没有RecyclerView

时间:2016-06-01 22:59:38

标签: android json android-volley

我尝试使用RecyclerView从我的本地服务器填充Volley JSON数据,但是RecyclerView没有显示,并且没有显示错误logcat。

这是我的代码:

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

这是json文件

MensProducts.json

{
"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"
            }
        ]
    },
]
}

2 个答案:

答案 0 :(得分:0)

即使产品包含数据,您当前的getItemCount也始终返回null。所以这样做。

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

         return 0;

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

答案 1 :(得分:0)

好的,正如 cricket_007 建议的那样,我将它从JsonArrayRequested更改为JsonObjectRequested以及其他一些引用我的JSON文件中的对象的代码,我终于让它工作了