Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.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
Android 在listView中加载异步映像_Android_Listview_Asynchronous - Fatal编程技术网

Android 在listView中加载异步映像

Android 在listView中加载异步映像,android,listview,asynchronous,Android,Listview,Asynchronous,在列表视图中加载数据后,我想从服务器加载图像。 我知道这个问题有很多话题,但我还没有找到解决办法 这是我的代码: //asyncTackClass for loadingpictures public class LoadImagesThread extends AsyncTask<Bundle, Void, Bitmap> { private ImageView view; private Bitmap bm; private Context context

在列表视图中加载数据后,我想从服务器加载图像。 我知道这个问题有很多话题,但我还没有找到解决办法

这是我的代码:

//asyncTackClass for loadingpictures
public class LoadImagesThread extends AsyncTask<Bundle, Void, Bitmap> {
    private ImageView view;
    private Bitmap bm;
    private Context context;
    private final WeakReference<ImageView> imageViewReference;

    private final String BUNDLE_URL = "url";
    private final String BUNDLE_NAME = "name";
    private final String BUNDLE_BM = "bm";

    public LoadImagesThread(Context context, ImageView view) {
        this.context=context;
        imageViewReference = new WeakReference<ImageView>(view);
    }

    @Override
    protected Bitmap doInBackground(Bundle... b) {

        Bitmap bm =null;
        if (StorageHelper.getBitmap(b[0].getString(BUNDLE_NAME)) != null) { // Check the sdcard
            bm = StorageHelper.getBitmap(b[0].getString(BUNDLE_NAME));
            Log.w("LoadImagesThread", "Get image from sdcard : "+b[0].getString(BUNDLE_NAME));
        } else { // Check the server
            bm = ServiceHelper.getBitmapFromURL(b[0].getString(BUNDLE_URL));
            StorageHelper.saveBitmap(bm, b[0].getString(BUNDLE_NAME)); // Save image on sdcard   
            Log.w("LoadImagesThread", "Get image from server : "+b[0].getString(BUNDLE_NAME));
        }

        return bm;
    }

    @Override
    protected void onPostExecute(final Bitmap bm) {
        super.onPostExecute(bm);        
        if (bm != null){ //if bitmap exists...
            view = imageViewReference.get();
            // Fade out
            Animation fadeOutAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeoutimage);
            fadeOutAnimation.setAnimationListener(new AnimationListener() {

            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationEnd(Animation animation) {
                // Fade in
                view.setImageBitmap(bm);
                Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeinimage);
                view.startAnimation(fadeInAnimation);
            }
        });

            // Launch the fadeout
            view.startAnimation(fadeOutAnimation);


        }else{ //if not picture, display the default ressource
            view.setImageResource(R.drawable.productcarre);
        }

    }

}
//用于加载图片的asyncTackClass
公共类LoadImagesThread扩展异步任务{
私有图像视图;
私有位图bm;
私人语境;
私有最终WeakReference imageViewReference;
私有最终字符串包\u URL=“URL”;
私有最终字符串包\u NAME=“NAME”;
专用最终字符串束\u BM=“BM”;
公共LoadImagesThread(上下文上下文,ImageView){
this.context=context;
imageViewReference=新的WeakReference(视图);
}
@凌驾
受保护位图doInBackground(束…b){
位图bm=null;
如果(StorageHelper.getBitmap(b[0].getString(BUNDLE_NAME))!=null){//检查SD卡
bm=StorageHelper.getBitmap(b[0].getString(BUNDLE_NAME));
Log.w(“LoadImagesThread”,“从SD卡获取图像:”+b[0].getString(BUNDLE_NAME));
}否则{//请检查服务器
bm=ServiceHelper.getBitmapFromURL(b[0].getString(BUNDLE_URL));
StorageHelper.saveBitmap(bm,b[0].getString(BUNDLE_NAME));//将图像保存到SD卡上
Log.w(“LoadImagesThread”,“从服务器获取图像:”+b[0].getString(BUNDLE_NAME));
}
返回bm;
}
@凌驾
受保护的void onPostExecute(最终位图bm){
super.onPostExecute(bm);
如果(bm!=null){//如果位图存在。。。
view=imageViewReference.get();
//淡出
Animation fadeOutAnimation=AnimationUtils.loadAnimation(上下文,R.anim.fadeoutimage);
setAnimationListener(新建AnimationListener()){
onAnimationStart上的公共无效(动画){
}
onAnimationRepeat上的公共无效(动画){
}
onAnimationEnd上的公共无效(动画){
//淡入
view.setImageBitmap(bm);
Animation fadeInAnimation=AnimationUtils.loadAnimation(上下文,R.anim.fadeinimage);
视图。startAnimation(fadeInAnimation);
}
});
//启动淡出
视图。开始动画(淡出动画);
}否则{//如果不是图片,则显示默认的ressource
view.setImageResource(R.drawable.productcarre);
}
}
}
该代码用于在ImageView中显示位图

这是适配器:

public class ListViewShoplistStoresAdapter extends BaseAdapter {

private ArrayList<Shop> shopList;
private Activity activity;

private HashMap<Integer, ImageView> views;
private final String BUNDLE_URL = "url";
private final String BUNDLE_NAME = "name";
private final String BUNDLE_POS = "pos";
private final String BUNDLE_ID = "id";


public ListViewShoplistStoresAdapter(Activity activity, ArrayList<Shop> shopList) {
    super();
    this.activity = activity;
    this.shopList = shopList;
    this.views = new HashMap<Integer, ImageView>();
}

public int getCount() {
    return shopList.size();
}

public Object getItem(int position) {
    return shopList.get(position);
}

public long getItemId(int position) {
    return shopList.get(position).getId();
}

private class ViewHolder {
    public TextView store;
    public TextView name;
    public ImageView ImageStore;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder view;
    LayoutInflater inflator = activity.getLayoutInflater();

    if(convertView == null) {
        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.listviewshops, null);

        view.store = (TextView) convertView.findViewById(R.id.store);
        view.name = (TextView) convertView.findViewById(R.id.name);
        view.ImageStore = (ImageView) convertView.findViewById(R.id.imgstore);

        convertView.setTag(view);
    }else {
        view = (ViewHolder) convertView.getTag();
    }

    Typeface regular=Typeface.createFromAsset(activity.getAssets(), "fonts/RobotoRegular.ttf");
    view.store.setTypeface(regular);

    Typeface light=Typeface.createFromAsset(activity.getAssets(), "fonts/RobotoLight.ttf");
    view.store.setTypeface(light);
    Brand brand = StorageHelper.getBrand(activity, shopList.get(position).getBrandId());
    if (brand == null) {
        Log.e("SetShopInAdapter","Brand null");
        Toast.makeText(activity, "Impossible d'afficher la liste de magasins", Toast.LENGTH_LONG).show();
    } else {
        view.store.setText(brand.getName());
        view.name.setText(shopList.get(position).getName());
        view.ImageStore.setImageResource(R.drawable.productcarre);
    }

    Bundle b = new Bundle();

    //url of the pict
    b.putString(BUNDLE_URL, ServiceHelper.getImageUrl("brand", brand.getName()));

    // name of image
    b.putString(BUNDLE_NAME, ServiceHelper.getCleanImageName(brand.getName()));

    //position in the listView
    b.putInt(BUNDLE_POS, position);

    //id of the current object
    b.putInt(BUNDLE_ID, brand.getId());

    //put info in the map in order to display in the onPostExecute
    if(views.get(position)==null){
        views.put(position, view.ImageStore);
        // launch thread
        new LoadImagesThread(activity.getApplicationContext(), view.ImageStore).execute(b);
    }

    return convertView;

}

}
公共类ListViewShoplistStoresAdapter扩展BaseAdapter{
私人ArrayList购物清单;
私人活动;
私有HashMap视图;
私有最终字符串包\u URL=“URL”;
私有最终字符串包\u NAME=“NAME”;
专用最终字符串束\u POS=“POS”;
私有最终字符串束\u ID=“ID”;
public ListViewShoplistStoresAdapter(活动活动,ArrayList商店列表){
超级();
这个。活动=活动;
this.shopList=购物清单;
this.views=newhashmap();
}
public int getCount(){
return shopList.size();
}
公共对象getItem(int位置){
返回购物清单。获取(位置);
}
公共长getItemId(int位置){
return shopList.get(position.getId();
}
私有类视窗持有者{
公共文本视图存储;
公共文本视图名称;
公共图像库;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图持有者视图;
LayoutFlater充气器=活动。GetLayoutFlater();
if(convertView==null){
视图=新的ViewHolder();
convertView=充气机。充气(R.layout.listviewshops,null);
view.store=(TextView)convertView.findViewById(R.id.store);
view.name=(TextView)convertView.findViewById(R.id.name);
view.ImageStore=(ImageView)convertView.findViewById(R.id.imgstore);
setTag(视图);
}否则{
view=(ViewHolder)convertView.getTag();
}
Typeface regular=Typeface.createFromAsset(activity.getAssets(),“font/robotregular.ttf”);
view.store.setTypeface(常规);
Typeface light=Typeface.createFromAsset(activity.getAssets(),“font/RobotoLight.ttf”);
view.store.setTypeface(灯光);
Brand Brand=StorageHelper.getBrand(活动,shopList.get(位置).getBrandId());
如果(品牌==null){
Log.e(“SetShopInAdapter”、“品牌空”);
Toast.makeText(活动,“不可能的演讲”,Toast.LENGTH_LONG.show();
}否则{
view.store.setText(brand.getName());
view.name.setText(shopList.get(position.getName());
view.ImageStore.setImageResource(R.drawable.productcarre);
}
Bundle b=新Bundle();
//pict的url
b、 putString(BUNDLE_URL,serviceheloper.getImageUrl(“brand”,brand.getName());
//图像名称
b、 putString(BUNDLE_NAME,serviceheloper.getCleanImageName(brand.getName());
//在listView中的位置
b、 putInt(线束位置、位置);
//当前对象的id
b、 putInt(BUNDLE_ID,brand.getId());
//将信息放入地图中,以便在onPostExecute中显示
if(views.get(position)==null){
视图.放置(位置,视图.图像存储);
//启动线程
新的LoadImagesThread(activity.getApplicationContext(),view.ImageStore).execute(b);
}
返回视图;
}
}
所以,当我使用GridView时,没有问题,但是当我使用ListView时,图像仅在第一项中更改

示例: 我想显示“汽车”、“房子”和“苹果”项目的产品图像。 代码将启动线程,所有图像(汽车、房子、最后是苹果)将显示在第一个项目(汽车项目)中。。。 房子和苹果虽然没有图像

你知道我该怎么做吗


谢谢

关于这一点,这里有很多,所以。。 异步的