如何使用从Firebase数据库中获取的URL设置imageView

时间:2017-04-14 19:28:14

标签: android firebase-realtime-database imageview

  

customitem.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/itempic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:visibility="visible"
        app:civ_border_color="#cccccc"
        app:civ_border_width="2dp" />

    <TextView
        android:id="@+id/itemtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="8dp"
        android:text="TextView"
        android:textSize="18sp" />

 </TableRow>
 </TableLayout>
  

这是arrayadapter   使用customitem.xml布局

 arrayAdapter = new ArrayAdapter<String>(Test.this, R.layout.customitem, R.id.itemtext, arr);
  

这就是我从数据库中获取数据的方法

        dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(final DataSnapshot dataSnapshot) {

                int length = (int) dataSnapshot.getChildrenCount();
                final ArrayList<String > idString = new ArrayList<String>(length);

                for (final DataSnapshot ds : dataSnapshot.getChildren())
                {
                    Log.d("ifkey", ds.getKey());

                    if (!ds.getKey().equals(user_id)) {
                        if (ds.hasChild("lastname"))
                        {
                            arr.add(ds.child("name").getValue().toString() + " " + ds.child("lastname").getValue().toString());
                            idString.add(i, ds.child("id").getValue().toString());
                            i++;
                        }
                        else {
        arr.add(ds.child("name").getValue().toString());
         String photoUrl = ds.child("photo").getValue().toString();

        idString.add(i, ds.child("id").getValue().toString());
                            i++;

                            Log.d("username", user_id);

                            Log.d("idString", "");
                            Log.d("getkey", ds.getKey());

                        }
                    }

                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                            Toast.makeText(getApplication(), "id :" + idString.get(position), Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(Test.this, usertest.class);
                            i.putExtra("userid", idString.get(position));
                            startActivity(i);
                        }
                    });
                    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                            final AlertDialog.Builder dialog = new AlertDialog.Builder(Test.this)
                                    .setTitle("ok").setMessage(
                                            "Send Game Request ...");
                            final AlertDialog alert = dialog.create();

                            alert.show();

                            return false;
                        }
                    });

                }
                Log.d("arr.size",""+arr.size());
                lv.setAdapter(arrayAdapter);


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

我想用数据库中的url显示imageview, 这就是我从数据库获取网址的方式

String photoUrl = ds.child("photo").getValue().toString();

如何在itempic中显示它们?

1 个答案:

答案 0 :(得分:2)

最好的方法是使用以下库之一:Picasso,UIL,Glide。

您也可以从新线程下载图像,然后传递给ImageView。