2017-09-18 20 views
-2

これは私のデータベースのスクリーンショットです。データを取得するのに多くの試みがありましたが、常にnullが返されるようです。私はfirebaseデータベースを使用しています。私はこのレイアウトにこのデータをどのように取り込むのか知りたいと思っていました。ところで、私はjavaとandroidに新しいので、質問に答えるときにそれを考慮してください。私が持っていたもう一つの質問は、それがクリックされたときにイメージの意図をどのように通すのかということでした。私は自宅のレイアウトで複数の画像ビューを使用していますが、その処理方法を理解できません。前もって感謝します。Firebaseからレイアウトにデータを取得

//製品データベース mDatabaseRef.child( "製品")から取得画像。addValueEventListener(新しいValueEventListener(){

 @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      Product product = dataSnapshot.child("Product ID").getValue(Product.class); 

      product_1_name = dataSnapshot.child("Product 1").child("Product Name").getValue().toString(); 
      product_1_price = dataSnapshot.child("Product 1").child("Product Price").getValue().toString(); 
      product_1_urlimage = dataSnapshot.child("Product 1").child("Product Image").getValue().toString(); 

      product_2_name = dataSnapshot.child("Product 2").child("Product Name").getValue().toString(); 
      product_2_price = dataSnapshot.child("Product 2").child("Product Price").getValue().toString(); 
      product_2_urlimage = dataSnapshot.child("Product 2").child("Product Image").getValue().toString(); 

      product_3_name = dataSnapshot.child("Product 3").child("Product Name").getValue().toString(); 
      product_3_price = dataSnapshot.child("Product 3").child("Product Price").getValue().toString(); 
      product_3_urlimage = dataSnapshot.child("Product 3").child("Product Image").getValue().toString(); 

      product_4_name = dataSnapshot.child("Product 4").child("Product Name").getValue().toString(); 
      product_4_price = dataSnapshot.child("Product 4").child("Product Price").getValue().toString(); 
      product_4_urlimage = dataSnapshot.child("Product 4").child("Product Image").getValue().toString(); 

      product_5_name = dataSnapshot.child("Product 5").child("Product Name").getValue().toString(); 
      product_5_price = dataSnapshot.child("Product 5").child("Product Price").getValue().toString(); 
      product_5_urlimage = dataSnapshot.child("Product 5").child("Product Image").getValue().toString(); 

      product_6_name = dataSnapshot.child("Product 6").child("Product Name").getValue().toString(); 
      product_6_price = dataSnapshot.child("Product 6").child("Product Price").getValue().toString(); 
      product_6_urlimage = dataSnapshot.child("Product 6").child("Product Image").getValue().toString(); 

      product_7_name = dataSnapshot.child("Product 7").child("Product Name").getValue().toString(); 
      product_7_price = dataSnapshot.child("Product 7").child("Product Price").getValue().toString(); 
      product_7_urlimage = dataSnapshot.child("Product 7").child("Product Image").getValue().toString(); 
     } 


     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

プライベートView.OnClickListener HomeImageClickListener =新しいView.OnClickListener(){ テント意図;​​

@Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.iv_hotcakes_topleft: 
       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_1_name); 
       intent.putExtra("url", product_1_urlimage); 
       intent.putExtra("price", product_2_price); 
       startActivity(intent); 
       break; 
      case R.id.iv_hotcakes_bottomleft: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_2_name); 

       intent.putExtra("url", product_2_urlimage); 
       intent.putExtra("price", product_2_price); 

       startActivity(intent); 
       break; 
      case R.id.iv_hotcakes_right: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_3_name); 

       intent.putExtra("url", product_3_urlimage); 
       intent.putExtra("price", product_3_price); 

       startActivity(intent); 
       break; 
      case R.id.iv_specials_left: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_4_name); 

       intent.putExtra("url", product_4_urlimage); 
       intent.putExtra("price", product_4_price); 

       startActivity(intent); 
       break; 
      case R.id.iv_specials_right: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_5_name); 
       intent.putExtra("url", product_5_urlimage); 
       intent.putExtra("price", product_5_price); 
       startActivity(intent); 
       break; 
      case R.id.iv_categories_top_left: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_6_name); 

       intent.putExtra("url", product_6_urlimage); 
       intent.putExtra("price", product_6_price); 
       startActivity(intent); 
       break; 
      case R.id.iv_categories_top_right: 
       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", product_7_name); 

       intent.putExtra("url", product_7_urlimage); 
       intent.putExtra("price", product_7_price); 
       startActivity(intent); 
       break; 
      case R.id.iv_categories_center_left: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", "Muffins"); 
       intent.putExtra("url", url); 
       intent.putExtra("price", "280"); 
       startActivity(intent); 
       break; 
      case R.id.iv_categories_center_right: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", "Muffins"); 
       intent.putExtra("url", url); 
       intent.putExtra("price", "280"); 
       startActivity(intent); 
       break; 
      case R.id.iv_categories_bottom_left: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", "Muffins"); 
       intent.putExtra("url", url); 
       intent.putExtra("price", "280"); 
       startActivity(intent); 
       break; 
      case R.id.iv_categories_bottom_right: 

       intent = new Intent(getContext(), ProductDetailActivity.class); 
       intent.putExtra("title", "Muffins"); 
       intent.putExtra("url", url); 
       intent.putExtra("price", "280"); 
       startActivity(intent); 
       break; 

     } 


    } 
}; 

https://ibb.co/hpviJk

https://ibb.co/et9wM5

+0

これまでに何を試しましたか? –

+0

これは、私がデータベースのデータを取得している方法です。これが私のデータベースです。私がやっていることは、文字列を使ってデータベースからデータを取得することによって、画像ビューを塗りつぶすことです。私はpojoについて多くのチュートリアルを見てきましたが、私がそれを使用するとnullを返します。 https://ibb.co/hpviJk https://ibb.co/et9wM5 https://ibb.co/et9wM5https://ibb.co/mpRnTk。 –

+0

私たちにいくつかのコードを教えてください。 –

答えて

0

使用orderByValue()これはあなたの仕事を行います

mReference.child("Products").orderByValue().addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 

      ProductsClass productsClass = dataSnapshot.getValue(ProductsClass.class); 

      if(productsClass!=null) 
      { 
       // do what ever you want 
      }else 
       Log.d(TAG, "onChildAdded: NULL"); 

     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

//これはあなたのコードの一部ではなく

ArrayList<HashMap<String,String>> list = new ArrayList<>(); 
     //HashMap<KEY,VALUE> 
     // just a example how to use hashmap in list 
     for(int i = 0;i<3;i++) 
     {   
      HashMap<String,String> map = new HashMap<>(); 
      map.put("product_key"+i,"product_name"+i); 
      map.put("product_key_image"+i,"product_image_link"+i); 
      list.add(map); 
     } 
     // to get values 

     for(int i=0;i<list.size();i++) 
     { 
      HashMap<String,String> map = list.get(i); 
      System.out.println("product name is "+map.get("product_key"+i)); 
      System.out.println("product name is "+map.get("product_key"+i)); 
     } 

     //for single value 

     HashMap<String,String> map = new HashMap<>(); 
     map.put("name", "salman"); 
     map.put("full_name", "123123123"); 


     System.out.println(map.get("name")); 
     System.out.println(map.get("full_name")); 

出力をハッシュマップの使用方法:

product name is product_name0 
product name is product_name0 
product name is product_name1 
product name is product_name1 
product name is product_name2 
product name is product_name2 
salman 
123123123 
+0

私はこの回答で2つの質問をしました。このhttps://ibb.co/eTBRM5レイアウトに製品イメージノードからのデータを入力するにはどうすればいいですか。製品ノードからのイメージのみのデータをフィルタリングする必要があります。別の問題は、pojoを使って最初のスクリーンショットレイアウトにデータを埋め込む方法を理解できなかったことです。私はあなたが質問で見ているように、意図を余計に伝える方法を理解することができませんでした。 –

+0

はいそれは何ですか.. – Salman500

+0

私は製品IDに基づいて画像をロードして、そのIDをインテントに渡すことができる方法はありません。あなたの答えに感謝します。 –

関連する問題