图像视图随着firebase适配器不断变化

时间:2016-06-15 09:06:09

标签: java listview android-studio firebase imageview

我正在构建一个在列表视图中显示产品的应用,我从firebase获取产品列表,问题是我将图片从firebase下载到我的应用上的图片视图由于某种原因,这对夫妇第一次图像完成下载后开始制造麻烦,并继续将图像更改为应用程序上的其他图像,当我使用调试时发现populateView运行如下:

i=0
i=1
i=2
i=3

然后再次:

i=0
i=1
i=2
i=3

我的产品数组列表中有超过4个项目。 我确定我的问题,但我无法弄清楚为什么会发生这种情况

这是我的mainFragment代码:

public class MainFragment extends Fragment {
View myView;
public static Context context;
private MainFragmentAdapter mainFragmentAdapter;
private ListView listView;
private Firebase mRootRef;
private ArrayList<Product> products;
private TextView textName;
private TextView textOverview;
private TextView textPrice;
private ImageView bannerImage;
private ImageView productImage;
public static Bitmap imageBitmap;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.main_fragment, container, false);
    bannerImage = (ImageView) myView.findViewById(R.id.imageView2);
    products = new ArrayList<>();
    //check if its mainPage and remove home button
    if (MainActivity.isMainPage == false) {
        MainActivity.homeButton.setVisibility(View.GONE);
        MainActivity.isMainPage = true;
    }
    final ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(context, R.layout.row_main, products);
    listView = (ListView) myView.findViewById(R.id.listView);

    Firebase.setAndroidContext(context);
    //press of a button on listview
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, new ProdcutFragment()).addToBackStack("tag").commit();
            MainActivity.homeButton.setVisibility(View.VISIBLE);

            ProdcutFragment.sendProdcut(products.get(position));
        }
    });
    Firebase ref = new Firebase("https://project-6597442007130040075.firebaseio.com");
    Firebase productsRef = ref.child("Products");
    FirebaseListAdapter<Product> fireAdapter = new FirebaseListAdapter<Product>(
            getActivity(),
            Product.class, R.layout.row_main,
            productsRef) {
        @Override
        protected void populateView(View view, Product product, int i) {
            textName = (TextView) view.findViewById(R.id.textName);
            textOverview = (TextView) view.findViewById(R.id.textOverView);
            textPrice = (TextView) view.findViewById(R.id.textPrice);
            productImage = (ImageView) view.findViewById(R.id.imageView);
            products.add(product);
            textName.setText(product.getName().toString());
            textOverview.setText(product.getOverview().toString());
            textPrice.setText(String.valueOf(product.getPrice()));

            new ImageLoadTask(product.getImageURL(), productImage).execute();
        }
    };
    listView.setAdapter(fireAdapter);
    return myView;
}
public static void reciveBitmap(String url, ImageView imageView) {
    new ImageLoadTask(url, imageView).execute();
}
public static class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
    private String url;
    private ImageView imageView;
    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    }
    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);

        imageView.setImageBitmap(result);
    }

1 个答案:

答案 0 :(得分:1)

尝试使用picaso:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

        Picasso.with(this)
                .loadproduct.getImageURL())
                .into(imageView);