android - 如何在 firebase android 中使用 ListView 设置图像?

标签 android listview firebase firebase-realtime-database

我正在从 firebase 数据库中检索数据,这很好用,但看起来很尴尬,我想用图像检索 ListView 。这个怎么做? 这是我用来从 firebase 检索数据的代码。

package com.food.sheenishere.stark;


import android.app.ProgressDialog;

import android.content.Intent;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;

import android.view.MenuItem;


import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.Toast;

import com.firebase.client.Firebase;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

public class profilemain extends AppCompatActivity {
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;


    DatabaseReference mref =FirebaseDatabase.getInstance().getReference("users");
    private ProgressDialog progressDialog;
    ListView mlistview;
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;

    private BottomNavigationView.OnNavigationItemSelectedListener
            mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
progressDialog.show();
                    startActivity(new Intent(profilemain.this, profilemain.class));
                    return true;
                case R.id.navigation_dashboard:
                    Toast.makeText(profilemain.this, "hi hello", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.navigation_notifications:
                    Toast.makeText(profilemain.this, "hi hello", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.navigation_signout:

                    Toast.makeText(profilemain.this, "You have successfully Signed out", Toast.LENGTH_SHORT).show();
                    mAuth.signOut();
                    return true;
            }
            return false;
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profilemain);
        progressDialog=new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
progressDialog.show();

        mlistview = (ListView) findViewById(R.id.listview);

        Firebase.setAndroidContext(this);

        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("users");

        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
        mlistview.setAdapter(arrayAdapter);


        mref.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String value = dataSnapshot.getValue(String.class);

                arrayList.add(value);
                arrayAdapter.notifyDataSetChanged();
                progressDialog.dismiss();
                //

                //

            }

            @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) {

            }
        });


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        mAuth = FirebaseAuth.getInstance();


        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() == null) {
                    startActivity(new
                            Intent(profilemain.this, MainActivity.class));

                }
            }
        };


    }

    @Override
    protected void onStart() {
        super.onStart();

        mAuth.addAuthStateListener(mAuthListener);
    }
}

最佳答案

要将图像设置为 View ,我建议您像这样使用 Glide:

Glide.with(imageView.getContext())
    .load(profilePhotoUrl)
    .centerCrop()
    .transform(new CircleTransform(imageView.getContext()))
    .override(40,40)
    .into(imageView);

其中 imageView 是您的 ImageView 的对象。

不要忘记在 build.gradle 文件中的 dependencies 中添加以下行:

compile 'com.github.bumptech.glide:glide:3.7.0'

希望对您有所帮助。

关于android - 如何在 firebase android 中使用 ListView 设置图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45264546/

相关文章:

来自可绘制图像的Android可滚动 ListView

ios - 按特定字段截断 firebase 项目

firebase - 一个域用于两个独立的 Firebase 项目

java - 获取 ListView 单元格的高度以方便向上滚动

java - 从 Android 更新 Facebook

android - 如何获取 Android 应用程序的构建/版本号?

Android去除桌面快捷方式

asp.net - 为什么我的 CommandArgument 为空?

firebase - 如何在同一个Firebase应用程序中为两种类型的用户分别处理登录?

android - 在 getjar.com 上推送 Android 应用程序更新