在android中使用从firebase检索的base64字符串的Imageview

时间:2016-02-26 05:43:57

标签: java android imageview firebase

     int i=0;
  ArrayList<String> list;
        queryRef.addChildEventListener(new ChildEventListener() {

                    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
                        Map<String, Object> value = (Map<String, Object>) snapshot.getValue();
                        String name1 = String.valueOf(value.get("Base64String"));
         **list = new ArrayList<String>();
                        list.add(name1);**
                        System.out.println("Base64 " + i++ +" "+ name1 );
                **System.out.println("1 "+list.toString());**

                    }

这个监听器每次获取值并打印例如:在下面的输出中i值递增,直到所有值从firebase获取。

在控制台中输出

Base64 0    basfbjksdvkjvskjvbskdj
1           basfbjksdvkjvskjvbskdj
Base64 1    dvfjovbfdjbsklcnsalcks
1           dvfjovbfdjbsklcnsalcks
Base64 2    dvbjsvfbvjksdkvbsdvjkb
1           dvbjsvfbvjksdkvbsdvjkb
Base64 3    qdncwe98yfecbsdjcksbdv
1           qdncwe98yfecbsdjcksbdv

我必须使用这个base64字符串并显示图像。下面是如果有一个base64字符串,则将base64字符串显示为图像的代码。问题是String name1每次检索时都会更改,因此在放入第一个base64字符串后必须更改imageview。在从firebase获取下一个base64字符串之前,我不知道如何移动到另一个imageview。

使用firebase base64字符串的imageview代码

byte[] imageAsBytes = Base64.decode(name1.getBytes(), Base64.DEFAULT); 
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0,imageAsBytes.length) );

如果我使用上面的代码,因为每次更改name1中的值都不起作用。我怎么能这样做。

编辑:

这是我所指的链接 http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/

此链接包含

String[] itemname ={
            "Safari",
            "Camera",
            "Global",
            "FireFox",
            "UC Browser",
            "Android Folder",
            "VLC Player",
            "Cold War"
        };

    Integer[] imgid={
            R.drawable.pic1,
            R.drawable.pic2,
            R.drawable.pic3,
            R.drawable.pic4,
            R.drawable.pic5,
            R.drawable.pic6,
            R.drawable.pic7,
            R.drawable.pic8,
    };

对于上面的输入我必须替换我的输入。但我的图像是在base64字符串中,但示例是使用Interger数组。我怎么能这样做。

下面是代码中存储base64字符串和list1

中员工姓名的代码
  name1 = String.valueOf(value.get("Path"));
                    name2 = String.valueOf(value.get("Name"));

                    list.add( name1);
                    list1.add(name2);

下面的代码我将列表值存储在String数组中,然后捆绑这些值发送,使用输入显示员工的姓名和图片,而不是 itemname和imgid。

        spath = list.toArray(new String[list.size()]);
        sname = list1.toArray(new String[list1.size()]);
        Bundle bundle =new Bundle();
        bundle.putStringArray("names",sname);
        bundle.putStringArray("paths",spath);
        Intent ilist = new Intent(getApplicationContext(),MActivity.class); 
        startActivity(ilist);

外汇数据:

String itemname[]=bundle.getStringArray("names");
    String abcd[]=bundle.getStringArray("paths");

1 个答案:

答案 0 :(得分:0)

尝试这个...

在第一个Activity中创建CustomArrayList而不是ArrayList string.So你不需要在另一个arraylist中保存名称..

     ArrayList<ListCustomObjects> datas;

oncreate初始化后。

     datas=new ArrayList<ListCustomObjects>();

将此代码放入 queryRef.addChildEventListener

   ListCustomObjects objects=new ListCustomObjects();
    //you can change to original data
    String name1="Data";
    String name2="Datas";
    objects.setName(name1);
    objects.setPath(name2);
    //add the custom objects to arraylist
    datas.add(objects);

在第一个活动中,按钮onclick将此代码...

   Intent secondIntent=new Intent(CustomArrayList.this,SecondArrayList.class);
           Bundle bundle=new Bundle();
           //inorder to send custom arraylist from one activity to another activity we need to use either parcelable or serializable.here am using parcelable for beter performance..
            bundle.putParcelableArrayList("List",datas);
            secondIntent.putExtra("Bundle",bundle);
            startActivity(secondIntent);

在第二个Activty中输入此代码......

    ListView listview=(ListView)findViewById(R.id.listView2);
    Bundle bundle=getIntent().getBundleExtra("Bundle");
    ArrayList<ListCustomObjects> datas=bundle.getParcelableArrayList("List");
    SecondArrayListAdapter adapter=new SecondArrayListAdapter(SecondArrayList.this,datas);
    listview.setAdapter(adapter);

为ArrayList自定义对象创建一个类文件 ListCustomObjects

public class ListCustomObjects implements Parcelable{


String name;
String path;

public ListCustomObjects() {

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPath() {
    return path;
}

public void setPath(String path) {
    this.path = path;
}

public ListCustomObjects(Parcel in) {
    name = in.readString();
    path= in.readString();

}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(path);
}

public static final Parcelable.Creator<ListCustomObjects> CREATOR = new Parcelable.Creator<ListCustomObjects>() {
    public ListCustomObjects createFromParcel(Parcel in) {
        return new ListCustomObjects(in);
    }

    public ListCustomObjects[] newArray(int size) {
        return new ListCustomObjects[size];

    }
};

}

并将适配器名称创建为 SecondArrayListAdapter 。我正在使用基础适配器。

public class SecondArrayListAdapter extends BaseAdapter {

ArrayList<ListCustomObjects> datas;
Context context;


public SecondArrayListAdapter(Context context,ArrayList<ListCustomObjects> datas) {
this.context=context;
    this.datas=datas;
}

@Override
public int getCount() {
    return datas.size();
}

@Override
public Object getItem(int position) {
    return datas.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    //where we check the convertview whethere it is creting first time or not
    if(convertView==null){
        convertView=((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sample_data_listview_item,null);
        viewHolder=new ViewHolder();

        viewHolder.image=(ImageView)convertView.findViewById(R.id.sample_listview_item_imageView);
        viewHolder.name=(TextView)convertView.findViewById(R.id.sample_listview_item_name);

        convertView.setTag(viewHolder);
    }
    //where we get the views from convertview instead of creating new one.It reduces memory consumption
    else{
        viewHolder=(ViewHolder)convertView.getTag();
    }
    //here we set the name
    viewHolder.name.setText(datas.get(position).getName());
    //here conversion takes place.
    byte[] imageAsBytes = android.util.Base64.decode(datas.get(position).getPath().getBytes(), android.util.Base64.DEFAULT);
    viewHolder.image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
    return convertView;
}
//this is used to bind the view as single class
class ViewHolder{
    ImageView image;
    TextView name;
}
}

最终为适配器 sample_data_listview_item 创建布局

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sample_listview_item_imageView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/sample_listview_item_name" />
  </LinearLayout>