从Firestore获取图像到RecyclerView Android Studio

时间:2019-09-17 19:51:19

标签: android google-cloud-firestore

我只是从互联网上学习如何从Firestore中获取文本,但我想从Cloud Firestore中获取我的图像,并将其也填充到回收站中。并且找不到任何资源可以在互联网上做到这一点,请帮帮我吗????

ActivityMain.java

public class MainActivity extends AppCompatActivity {

FirebaseFirestore db;
RecyclerView mRecyclerView;
ArrayList<User> userArrayList;
myRecyclerViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    userArrayList = new ArrayList<>();
    setUpRecyclerView();
    setUpFireBase();
    loadDatasFromFireBase();

}

private void loadDatasFromFireBase() {

    if(userArrayList.size() > 2)
        userArrayList.clear();

    db.collection("users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            for (DocumentSnapshot querySnapshot: task.getResult()){
                User user = new User(querySnapshot.getString("name"), querySnapshot.getString("status"));
                userArrayList.add(user);
            }
            adapter = new myRecyclerViewAdapter(MainActivity.this, userArrayList);
            mRecyclerView.setAdapter(adapter);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(MainActivity.this,"Problem--1--" , Toast.LENGTH_SHORT).show();
            Log.w("--1--", e.getMessage());
        }
    });
}


private void setUpFireBase(){
    db = FirebaseFirestore.getInstance();
}

private void setUpRecyclerView(){
    mRecyclerView = findViewById(R.id.mRecyclerView);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}

}

MyRecyclerViewHolder.java

public class MyRecyclerViewHolder extends RecyclerView.ViewHolder {
    public TextView mUserName, mUserStatus;

    public MyRecyclerViewHolder(@NonNull View itemView) {
        super(itemView);

        mUserName = itemView.findViewById(R.id.mRowUserName);
        mUserStatus = itemView.findViewById(R.id.mRowUserStatus);

    }
}

myRecyclerViewAdapter.java

public class myRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewHolder> {

    MainActivity mainActivity;
    ArrayList<User> userArrayList;

    public myRecyclerViewAdapter(MainActivity mainActivity, ArrayList<User> userArrayList){
        this.mainActivity = mainActivity;
        this.userArrayList = userArrayList;
    }

    @NonNull
    @Override
    public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(mainActivity.getBaseContext());
        View view = layoutInflater.inflate(R.layout.single_row, parent, false);

        return new MyRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, int position) {
        holder.mUserName.setText(userArrayList.get(position).getUserName());
        holder.mUserStatus.setText(userArrayList.get(position).getUserStatus());
    }

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

User.java

public class User {
    String userName;
    String userStatus;

    public User(){
        //Empty constructor do not delete.
    }

    public User(String userName, String userStatus){
        this.userName = userName;
        this.userStatus = userStatus;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(String userStatus) {
        this.userStatus = userStatus;
    }
}

single_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/mRowUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="TextView 1"/>
    <TextView
        android:id="@+id/mRowUserStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="TextView 2"/>

</LinearLayout>

0 个答案:

没有答案