片段中的Recycler视图

时间:2017-02-10 14:33:47

标签: android android-fragments android-recyclerview android-cardview

你好我有没有显示recylerview / cards的片段我把它从JSON中的MySQL数据库中获取数据,并将其解析为数组。这工作正常我可以在日志中看到数组,但片段中没有显示任何内容。

我很难过。

FragmentClass:

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

适配器:

公共类ProductAdapter扩展了RecyclerView.Adapter {

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

1 个答案:

答案 0 :(得分:0)

首次拨打0时,productList.size()notifyItemRangeChanged(0, productList.size())范围内没有任何内容。因此,技术上没有更改的项目。您应该在此处以及数据集完全更改时的任何位置使用notifyDataSetChanged()