图像视图随 Firebase 适配器不断变化

Posted

技术标签:

【中文标题】图像视图随 Firebase 适配器不断变化【英文标题】:Image view keep changing with firebase adapter 【发布时间】:2016-10-16 07:13:03 【问题描述】:

我正在构建一个在列表视图中显示产品的应用程序,我正在从 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】:

尝试使用 picaso:

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

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

【讨论】:

非常感谢您解决了我的问题,甚至提高了应用程序的性能,现在图像加载速度更快

以上是关于图像视图随 Firebase 适配器不断变化的主要内容,如果未能解决你的问题,请参考以下文章

Firebase数据库部分列表视图

带有光标适配器滚动问题的android列表视图

在自定义适配器的列表视图项中加载不同的图像

如何为列表视图创建自定义光标适配器以用于图像和文本?

如何从 Firebase 检索数据到 ListView(使用自定义数组适配器)

使用带有文本和图像的自定义适配器在列表视图中搜索