0

我使用这个查询来获取数据:

query Products($id: ID!){
            node(id: $id) {
                    id
                    ... on Collection {
                                title
                                products(first: 250){
                                                edges{
                                                    node{
                                                    title
                                                    id
                                            variants(first: 1){
                                                edges{
                                                    node{
                                                        price
                                                        }
                                                    }
                                                }
                                            images(first: 1,maxWidth:400,maxHeight:740,scale:3){
                                                edges{
                                                    node{
                                                        src
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

在 ApolloCall.CallBack 的 OnResponse 方法中,当我想要获取产品标题时,它会在这一行“ Log.i("CallBack products", String.valueOf(response.data().node().asCollection().products( ).edges())); " 我需要在 recyclerview 中显示产品的标题、价格和图片。获取它们的最佳方法是什么?提前致谢!

下面是我的代码:

      @Override                                                                                                                 
 protected void onCreate(@Nullable Bundle savedInstanceState) {                                                            
     super.onCreate(savedInstanceState);                                                                                   
     setContentView(R.layout.activity_product);                                                                            
     String s = getIntent().getStringExtra("id");                                                                          
     Bundle extras = getIntent().getExtras();                                                                              
     if (extras != null) {                                                                                                 
         s = extras.getString("id");                                                                                       
     }                                                                                                                     
     application = (SampleApplication) getApplication();                                                                 


     content = (ViewGroup) findViewById(R.id.rl_content_holder);                                                           
     progressBar = (ProgressBar) findViewById(R.id.loading_bar);                                                           
     productRecyclerView = (RecyclerView) findViewById(R.id.rvProductList);                                                
     mProductAdapter = new ProductAdapter(this);                                                                           
     mGridLayoutManager = new GridLayoutManager(this,2);                                                                   

     fetchProducts();                                                                                                      
 }                                                                                                                         

 ApolloCall.Callback<Products.Data> mDataCallback =                                                                        
         new ApolloCall.Callback<Products.Data>() {                                                                        

             @Override                                                                                                     
             public void onResponse(@Nonnull final Response<Products.Data> response) {                                     

                     Log.i("CallBack products", String.valueOf(response.data().node().asCollection().products().edges())); 

                     runOnUiThread(new Runnable() {                                                                        
                         @Override                                                                                         
                         public void run() {                                                                               
                             Toast.makeText(ProductActivity.this, "getting products", Toast.LENGTH_SHORT).show();          
                             mProductAdapter.setProductData(response.data().node().asCollection().products().edges());     
                             productRecyclerView.setAdapter(mProductAdapter);                                              
                             productRecyclerView.setLayoutManager(mGridLayoutManager);                                     
                             progressBar.setVisibility(View.GONE);                                                         
                             content.setVisibility(View.VISIBLE);                                                          
                         }                                                                                                 
                     });                                                                                                   

             }                                                                                                             

             @Override                                                                                                     
             public void onFailure(@Nonnull ApolloException e) {                                                           
                 Log.e("Product Activity", e.getMessage(), e);                                                             
                 runOnUiThread(new Runnable() {                                                                            
                     @Override                                                                                             
                     public void run() {                                                                                   
                         progressBar.setVisibility(View.GONE);                                                             
                         Toast.makeText(ProductActivity.this, "getting products failed", Toast.LENGTH_SHORT).show();       
                     }                                                                                                     
                 });                                                                                                       
             }                                                                                                             
         };                                                                                                                
 public void fetchProducts() {                                                                                             

     mHttpClient = new OkHttpClient.Builder()                                                                              
             .addInterceptor(new HttpInterceptor())                                                                        
             .build();                                                                                                     
     Products productQuery = Products                                                                                      
             .builder().id("id")                                                                                           
             .build();                                                                                                     
     productCall = application.apolloClient().newCall(productQuery);                                                       
     productCall.enqueue(mDataCallback);                                                                                   

 }  

这是 RecyclerView 适配器:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {

    // Store the context for easy access
    private Context mContext;

    private List<Products.Data.Edge> productData = Collections.emptyList();

    public void setProductData(List<Products.Data.Edge>productData) {
        this.productData = productData;
        this.notifyDataSetChanged();
    }

    public ProductAdapter(Context context ) {

        mContext = context;
    }

    private Context getContext() {
        return mContext;
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView productListItem;
        public ImageView productListImage;
        private View rlContainer;
        Context context;

        public ViewHolder(Context context,View itemView) {
            super(itemView);
            productListItem = (TextView) itemView.findViewById(R.id.itemProductTxt);
            productListImage = (ImageView)itemView.findViewById(R.id.itemProductImg);
            rlContainer =  itemView.findViewById(R.id.linear_layout);
            this.context = context;
        }

        public void setProductItem(final Products.Data.Edge productItem) {

            productListItem.setText(productItem.node().title());


            rlContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Toast.makeText(context, "list item pressed", Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

    @Override
    public ProductAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View productView = inflater.inflate(R.layout.listitem_product, parent, false);

        // Return a new holder instance
        ProductAdapter.ViewHolder viewHolder = new ProductAdapter.ViewHolder(context,productView);
        return viewHolder;
    }


    @Override
    public void onBindViewHolder(ProductAdapter.ViewHolder holder, int position) {

        final Products.Data.Edge productsEntry = this.productData.get(position);
        holder.setProductItem(productsEntry);



    @Override
    public int getItemCount() {
        return productData.size();
    }


}
4

1 回答 1

0

您能否提供堆栈跟踪。你确定你得到的回复不是空的吗?

您得到 NPE 可能是因为通过 id 请求的节点不存在。

于 2017-05-20T19:59:31.170 回答