2017-04-16 39 views
0

这是一个我用于图像轮播的例子,现在它适用于本地图像。我尝试了一切,以使它与来自Firebase数据库的图像一起工作。这是与本地图像一起使用的例子。如何使用图像轮播与火力点数据库

public class ItemFragment extends Fragment { 

    FirebaseDatabase firebaseDatabaseInstance; 
    private DatabaseReference booksInstance; 
    BookGridAdapter adapter ; 
    AlbumDBHandler db; 
    private static final String POSITON = "position"; 
    private static final String SCALE = "scale"; 
    private static final String DRAWABLE_RESOURE = "resource"; 
    private int screenWidth; 
    private int screenHeight; 

    private int[] imageArray = new int[]{R.drawable.zelal, R.drawable.mo2lfat, 
      R.drawable.saidkotb, R.drawable.sawtyat, R.drawable.black_image, 
      R.drawable.zelal, R.drawable.zelaltabs, R.drawable.mo2lfat, 
      R.drawable.sawtyat, R.drawable.zelal}; 

    ArrayList<BookData> books = new ArrayList<>(); 

    public static Fragment newInstance(testImage context, int pos, float scale) { 
     Bundle b = new Bundle(); 
     b.putInt(POSITON, pos); 
     b.putFloat(SCALE, scale); 
     return Fragment.instantiate(context, ItemFragment.class.getName(), b); 
    } 
    private void getData(){ 
     firebaseDatabaseInstance = FirebaseDatabase.getInstance(); 
     // get reference to 'users' node 
     booksInstance = firebaseDatabaseInstance.getReference("mo2lfat"); 
     books.clear(); 
     books.addAll(db.getAllBook()); 
     booksInstance.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       storeData(dataSnapshot); 
      } 
      @Override 
      public void onCancelled(DatabaseError error) { 
       // Failed to read value 
       Log.w(TAG, "Failed to read value.", error.toException()); 
      } 
     }); 

    } 

    private void storeData(DataSnapshot snapshot) { 
     books.clear(); 
     for (DataSnapshot alert: snapshot.getChildren()) { 
      BookData book = new BookData(
        (String)alert.child("id").getValue(), 
        (String)alert.child("book_name").getValue(), 
        (String)alert.child("book_path").getValue(), 
        (String)alert.child("book_path").getValue(), 
        "", 
        (String)alert.child("image_path").getValue(), 
        (String)alert.child("image_path").getValue(), 
        "" 
      ); 
      db.insertBook(book); 
     } 
     books.addAll(db.getAllBook()); 

     adapter.notifyDataSetChanged(); 
    } 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWidthAndHeight(); 
    } 

    @SuppressLint("SetTextI18n") 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
    CarouselLinearLayout root = (CarouselLinearLayout) linearLayout.findViewById(R.id.root_container); 
     ImageView imageView = (ImageView) linearLayout.findViewById(R.id.pagerImg); 

     imageView.setLayoutParams(layoutParams); 
     // imageView.setImageResource(imageArray.(postion)) 
     imageView.setImageResource(imageArray[postion]); 
     root.setScaleBoth(scale); 
     return linearLayout; 
    } 

} 

回答

0

您必须获取图片的网址,使用该网址加载图片。
对于加载图像,您可以使用高效的图像加载库。 这些库是 glidepicasso

这个库也很容易使用。

Picasso.with(context).load("imageUrl").into(imageView);