Android 片段中的回收器视图

Android 片段中的回收器视图,android,android-fragments,android-recyclerview,android-cardview,Android,Android Fragments,Android Recyclerview,Android Cardview,您好,我有一个片段没有显示recylerview/cards,我用JSON从MySQL数据库中获取数据,该数据库被解析为一个数组。这很好,我可以在日志中看到数组,但是片段中没有显示任何内容 我被难住了 碎片类别: public class ProductFragment extends Fragment { private RecyclerView rvProduct; private Singleton singleton; private RequestQueue requestQueue

您好,我有一个片段没有显示recylerview/cards,我用JSON从MySQL数据库中获取数据,该数据库被解析为一个数组。这很好,我可以在日志中看到数组,但是片段中没有显示任何内容

我被难住了

碎片类别:

public class ProductFragment extends Fragment {

private RecyclerView rvProduct;
private Singleton singleton;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
public static final String url = "http://10.0.2.2/salesapp/productlist.php";
final String TAG = "ProductFragment";
final String TAG1 = "JSONRequest ProdFrag";
private ArrayList<Product> productList = new ArrayList<>();
private ProductAdapter productAdapter;


public ProductFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    singleton=Singleton.getInstance();
    requestQueue = singleton.getRequestQueue();
    sendJsonRequest();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_product,container,false);
    rvProduct = (RecyclerView)view.findViewById(R.id.rvProduct);
    rvProduct.setHasFixedSize(true);
    LinearLayoutManager manager = new LinearLayoutManager(getActivity());
    rvProduct.setLayoutManager(manager);
    productAdapter = new ProductAdapter(getActivity());
    rvProduct.setAdapter(productAdapter);
    sendJsonRequest();
    return view;
}

private void sendJsonRequest(){
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
            url,
            null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    productList = parseJSONResponse(response);
                    productAdapter.setProductList(productList);
                    Log.d(TAG1,productList.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                     if(error!=null){
                         Log.d(TAG,error.toString());                             
                    }
                }
            }
    );
    requestQueue.add(request);
}

private ArrayList<Product> parseJSONResponse(JSONObject response){
    ArrayList<Product> productList = new ArrayList<>();
    if(response!=null||response.length()>0){
        try {
            JSONArray arrayProducts=response.getJSONArray(KEY_Products);
            for(int i=0; i<arrayProducts.length(); i++){
                JSONObject Product = arrayProducts.getJSONObject(i);
                String Name=Product.getString(KEY_Name);
                String Description=Product.getString(KEY_Description);
                double Retail=Product.getDouble(KEY_RetailPrice);
                double Wholesale=Product.getDouble(KEY_WholesalePrice);
                String URL=null;
                if(Product.has(KEY_URL)) {
                    URL = Product.getString(KEY_URL);
                }else{
                    URL = "NA";
                }
                Product product=new Product();
                product.setName(Name);
                product.setDescription(Description);
                product.setRetailPrice(Retail);
                product.setWholesalePrice(Wholesale);
                product.setURL(URL);
                productList.add(product);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return productList;
}
}
公共类ProductFragment扩展了片段{
私人回收产品;
私人独生子女;
私有请求队列请求队列;
私有图像加载器;
公共静态最终字符串url=”http://10.0.2.2/salesapp/productlist.php";
最后一个字符串TAG=“ProductFragment”;
最终字符串TAG1=“JSONRequest ProdFrag”;
private ArrayList productList=new ArrayList();
私有产品适配器;
公共产品片段(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
singleton=singleton.getInstance();
requestQueue=singleton.getRequestQueue();
sendJsonRequest();
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图=充气机。充气(右布局。碎片\产品,容器,假);
rvProduct=(RecyclerView)view.findViewById(R.id.rvProduct);
rvProduct.setHasFixedSize(真);
LinearLayoutManager=newlinearlayoutmanager(getActivity());
rvProduct.setLayoutManager(经理);
productAdapter=新的productAdapter(getActivity());
rvProduct.setAdapter(productAdapter);
sendJsonRequest();
返回视图;
}
私有void sendJsonRequest(){
JsonObjectRequest=新的JsonObjectRequest(request.Method.GET,
网址,
无效的
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
productList=parseJSONResponse(响应);
productAdapter.setProductList(productList);
Log.d(TAG1,productList.toString());
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
if(错误!=null){
Log.d(标记,error.toString());
}
}
}
);
添加(请求);
}
私有ArrayList parseJSONResponse(JSONObject响应){
ArrayList productList=新的ArrayList();
if(response!=null | | response.length()>0){
试一试{
JSONArray arrayProducts=response.getJSONArray(关键产品);

对于(int i=0;i当您第一次调用
notifyItemRangeChanged(0,productList.size())
时,在
0
productList.size()的范围内没有项目。因此,从技术上讲,没有项目会发生更改。您应该使用
notifyDataSetChanged()
这里,以及数据集完全更改时的任何地方。

尝试使用
notifyDataSetChanged()
而不是
notifyItemRangeChanged(0,productList.size());
谢谢Pavneet!成功!
private ArrayList<Product> productList = new ArrayList<>();
private LayoutInflater layoutInflater;
private Singleton singleton;
private ImageLoader imageLoader;

public ProductAdapter(Context context){
    layoutInflater = LayoutInflater.from(context);
    singleton=Singleton.getInstance();
    imageLoader=singleton.getImageLoader();
}

public void setProductList(ArrayList<Product> productList){
    this.productList=productList;
    notifyItemRangeChanged(0, productList.size()); }

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = layoutInflater.inflate(R.layout.product_cardview, parent, false);
    ProductViewHolder productViewHolder = new ProductViewHolder(view);
    return productViewHolder;
}

@Override
public void onBindViewHolder(final ProductViewHolder holder, int position) {
    Product product = productList.get(position);
    holder.tvProductSKU.setText(product.getSKU());
    holder.tvProductName.setText(product.getName());
    holder.tvProductWholesalePrice.setText(""+product.getWholesalePrice());
    holder.tvProductRetailPrice.setText(""+product.getRetailPrice());
    String URL=product.getURL();
    String fullURL="http//10.0.2.2/salesapp/images/"+URL;
    if(URL!=null){
        imageLoader.get(fullURL, new ImageLoader.ImageListener(){
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate){
                holder.ivProductImage.setImageBitmap(response.getBitmap());
            }
            @Override
            public void onErrorResponse(VolleyError error){

            }
        });
    }
}


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

static class ProductViewHolder extends RecyclerView.ViewHolder{
    private ImageView ivProductImage;
    private TextView tvProductSKU;
    private TextView tvProductName;
    private TextView tvProductWholesalePrice;
    private TextView tvProductRetailPrice;

    public ProductViewHolder(View itemView) {
        super(itemView);
        ivProductImage = (ImageView)itemView.findViewById(R.id.ivProductImage);
        tvProductSKU = (TextView)itemView.findViewById(R.id.tvProductSKU);
        tvProductName = (TextView)itemView.findViewById(R.id.tvProductName);
        tvProductWholesalePrice = (TextView)itemView.findViewById(R.id.tvProductWholesalePrice);
        tvProductRetailPrice = (TextView)itemView.findViewById(R.id.tvProductRetailPrice);
    }
}
}