Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库方法填写ImageView时遇到问题_Java_Android - Fatal编程技术网

Java 从数据库方法填写ImageView时遇到问题

Java 从数据库方法填写ImageView时遇到问题,java,android,Java,Android,所以我一直在安卓平台上工作,我遇到了一个问题。我一直在使用FireBase作为我选择的数据库来存储从应用程序上传的图像。当用户选择要上载的图像时,该图像用Base64编码到字符串中,这就是保存在数据库中的内容。现在,我想通过将字符串解码到位图上,用数据库中上传的所有图片填充应用程序中另一个活动的ImageView。但是,我在从数据库获取数据以填充此ImageView的方法方面遇到了问题: public class ImageAdapter extends BaseAdapter { publ

所以我一直在安卓平台上工作,我遇到了一个问题。我一直在使用FireBase作为我选择的数据库来存储从应用程序上传的图像。当用户选择要上载的图像时,该图像用Base64编码到字符串中,这就是保存在数据库中的内容。现在,我想通过将字符串解码到位图上,用数据库中上传的所有图片填充应用程序中另一个活动的ImageView。但是,我在从数据库获取数据以填充此ImageView的方法方面遇到了问题:

public class ImageAdapter extends BaseAdapter {


public ImageView imageView;
public Bitmap[] imagesDecoded;
public int i = 0;
private Context mContext;
public String holdsImage = "";

//these are just methods required by the ImageAdapter
public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {

    //more code to set up the imageView
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(250, 250));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }




    //setting up array for images extracted from database, this is where it gets messy
    //This is the method used for the database to retrieve values from it
    Firebase ref = new Firebase("https://myapp.firebaseio.com/posts");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            long flyerCount = snapshot.getChildrenCount();
            int flyerCountInt = (int) flyerCount;
            imagesDecoded = new Bitmap[flyerCountInt];
            for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                Flyer post = postSnapshot.getValue(Flyer.class);
                holdsImage = post.getTitle();
                turnImageStringToImage(holdsImage);
            }
        }
        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("The read failed: " + firebaseError.getMessage());
        }
    });


    imageView.setImageBitmap(imagesDecoded[position]);
    return imageView;
}







private void turnImageStringToImage(String s){

        byte[] decodedString = Base64.decode(s, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imagesDecoded[i] = decodedByte;
        i++;

}
所以我的问题是,在onDataChange方法中,无论在for循环内部发生什么,在它之外都是不可用的。例如,如果我尝试在方法onDataChange之后为holdsImage字符串分配字符串post.getTitle()后使用该字符串,则该字符串将返回到“”值,该值是在所有这些方法之外的全局范围内初始化的值。通常情况下,如果我可以放置线,这是可以解决的:

imageView.setImageBitmap(imagesDecoded[position]);

在for循环内部填充ImageView,但是position变量随后给了我一个错误,表示它需要声明为final,这是不可能的,因为它被声明为getView方法的参数。我还尝试将turnImageStringToImage的内容放在for循环中,但这也不起作用。无论我做什么,方法内部发生的任何事情都不会转移到写入的内容上,这样我就可以在方法外部使用它。令人困惑的是,所有这些变量和数组都被声明为全局变量,所以我不理解为什么它们的值会在onDataChange方法运行后恢复为原来的值。我还尝试创建一个对象并在循环中修改它的一个值,但当我在方法之外访问该值时,它仍然不是在方法中指定的值。在onDataChange方法中,我可以做些什么来保留在for循环中写入的数组内容吗?

“这是不可能的,因为它被声明为getView方法的参数”您可以在参数声明中抛出final,但不确定它是否仍然会重写。。。无论如何,你需要使用一个调试器来理解发生了什么。我做了,问题是在onDataChange方法中设置了一个值的任何变量,在该方法完成后都会丢失该值