Ben Shabat
Ben Shabat

Reputation: 387

Image view keep changing with firebase adapter

I'm building an app that shows products in list view,I'm getting the list of products from firebase, The problem is that I'm downloading images from firebase to image views on my app and for some reason after the couple first images finish downloading it start to make trouble and keep changing the image to other images on app,when i used debug i found out the populateView is running like this:

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

and then again:

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

And i have more then 4 items in my products array list. I'm sure that my problem but i cant figure out why this is happening

Here my mainFragment code:

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);
    }

Upvotes: 3

Views: 259

Answers (1)

ediBersh
ediBersh

Reputation: 1135

try using picaso:

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

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

Upvotes: 1

Related Questions