作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从 Firebase 后端服务器开发一个 Android 博客应用程序...我正在 ViewPager 布局中成功地将图像获取到我的应用程序。现在我想从 Firebase 存储中获取视频和图像...请帮助我实现这个方法。
MainActivity.java
public class MainActivity extends AppCompatActivity implements IFirebaseLoadDone {
private static final String TAG = "MainActivity";
ViewPager viewPager;
MyAdapter adapter;
DatabaseReference movies;
IFirebaseLoadDone iFirebaseLoadDone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Menu menu = navigation.getMenu();
MenuItem menuItem = menu.getItem(0);
menuItem.setChecked(true);
FirebaseApp.initializeApp(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("MyNotifications","MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
FirebaseMessaging.getInstance().subscribeToTopic("general")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "Welcome";
if (!task.isSuccessful()) {
msg = "failed";
}
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
movies = FirebaseDatabase.getInstance().getReference("Movies");
iFirebaseLoadDone = this;
loadMovie();
viewPager = (ViewPager)findViewById(R.id.view_pager);
viewPager.setPageTransformer(true,new DeapthPageTransformer());
}
private void loadMovie()
{
movies.addListenerForSingleValueEvent(new ValueEventListener()
{
List<Movie>movieList = new ArrayList<>();
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
for(DataSnapshot movieSnapShot:dataSnapshot.getChildren()) {
movieList.add(movieSnapShot.getValue(Movie.class));
iFirebaseLoadDone.onFirebaseLoadSuccess(movieList);
}
Collections.reverse(movieList);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
iFirebaseLoadDone.onFirebaseLoadFailed(databaseError.getMessage());
}
});
}
@Override
public void onFirebaseLoadSuccess(List<Movie> movieList) {
adapter = new MyAdapter(this,movieList);
viewPager.setAdapter(adapter);
}
@Override
public void onFirebaseLoadFailed(String message) {
Toast.makeText(this, ""+ message, Toast.LENGTH_SHORT).show();
}
<小时/>
public class MyAdapter extends PagerAdapter {
Context context;
List<Movie> movieList;
LayoutInflater inflater;
public MyAdapter(Context context, List<Movie> movieList) {
this.context = context;
this.movieList = movieList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return movieList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
((ViewPager) container).removeView((View) object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View view = inflater.inflate(R.layout.view_pager_item, container, false);
final ImageView movie_image = (ImageView) view.findViewById(R.id.movie_image);
final TextView movie_title = (TextView) view.findViewById(R.id.movie_title);
final TextView movie_description = (TextView) view.findViewById(R.id.movie_description);
ImageView likeImage = (ImageView) view.findViewById(R.id.img_like);
ImageView sendImage = (ImageView) view.findViewById(R.id.img_shr);
Picasso.get().load(movieList.get(position).getImage()).into(movie_image);
movie_title.setText(movieList.get(position).getName());
<小时/>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/movie_image"
android:scaleType="fitXY"/>
<小时/>
public class Movie
{
private String name,description,image;
public Movie() {
}
public Movie(String name, String description, String image) {
this.name = name;
this.description = description;
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
最佳答案
我正在从 firebase 数据库中获取我的个人资料图片:-
final ArrayList<Movie> yourList= new ArrayList<>();
DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference("Movies");
mDatabaseReference.child("0").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
pd.dismiss();
if (dataSnapshot.getValue() != null) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Movie object = snapshot.getValue(Movie.class);
yourList.add(mobile);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
关于java - 如何从 firebase 服务器获取图像和视频到我的博客应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55662096/
我是一名优秀的程序员,十分优秀!