2014-01-16 75 views
3

即時通訊嘗試使動態圖像,使用asyntask它的下載圖像和設置到列表視圖的列表視圖。我的問題是,當滾動下來的圖像得到隨機更改..ListView圖像更改在滾動

class ps1 extends ArrayAdapter<String> { 
    Context context; 
    String[] images1; 
    List mList; 
    String[] namearray; 
    String[] rating; 

    static class ViewHolder { 
     ImageView localImageView1; 
     ImageView localImageView2; 
     ImageView localImageView3; 
    } 

    ps1(Context paramContext, String[] paramArrayOfString1, String[] paramArrayOfString2, String[] paramArrayOfString3) { 
     super(paramContext, R.layout.list2, R.id.imageView1, paramArrayOfString1); 
     this.context = paramContext; 
     this.images1 = paramArrayOfString3; 
     this.namearray = paramArrayOfString1; 
     this.rating = paramArrayOfString2; 
    } 

    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(context.LAYOUT_INFLATER_SERVICE); 

    ViewHolder viewHolder = new ViewHolder(); 
    if (paramView == null) { 
     paramView = inflater.inflate(R.layout.list2, paramViewGroup, false); 

    } 


    viewHolder.localImageView1 = (ImageView) paramView 
      .findViewById(R.id.imageView1); 
    viewHolder.localImageView2 = (ImageView) paramView 
      .findViewById(R.id.imageView2); 
    viewHolder.localImageView3 = (ImageView) paramView 
      .findViewById(R.id.imageView3); 


    viewHolder.localImageView1.setScaleType(ImageView.ScaleType.FIT_XY); 
    viewHolder.localImageView2.setScaleType(ImageView.ScaleType.FIT_XY); 
    viewHolder.localImageView3.setScaleType(ImageView.ScaleType.FIT_XY); 

    viewHolder.localImageView1.setTag(this.namearray[paramInt]); 
    new LoadImage().execute(viewHolder.localImageView1); 
    viewHolder.localImageView2.setTag(this.rating[paramInt]); 
    new LoadImage().execute(viewHolder.localImageView2); 
    viewHolder.localImageView3.setTag(this.images1[paramInt]); 
    new LoadImage().execute(viewHolder.localImageView3); 


    return paramView; 
    } 
    } 

    class LoadImage extends AsyncTask<Object, Void, Bitmap> { 
    private ImageView imv; 

    private Bitmap download_Image(String paramString) { 
    Bitmap localBitmap = null; 
    try { 
     Object localObject = null; 
     localBitmap = BitmapFactory 
       .decodeStream(((HttpURLConnection) new URL(paramString) 
         .openConnection()).getInputStream()); 
     localObject = localBitmap; 
     if (localObject != null) { 
      return localBitmap; 
     } 
    } catch (Exception e) { 

    } 
    return localBitmap; 
    } 

    protected Bitmap doInBackground(Object... paramVarArgs) { 
    this.imv = ((ImageView) paramVarArgs[0]); 
    Log.d("fsdf", (String) this.imv.getTag()); 
    return download_Image((String) this.imv.getTag()); 
    } 

    protected void onPostExecute(Bitmap paramBitmap) { 
    this.imv.setImageBitmap(paramBitmap); 
    } 
} 
+2

這是因爲異步任務在滾動期間完成其任務。在列表視圖的滾動和初始化期間,多次啓動getView方法。使用這樣的東西:https://github.com/thest1/LazyList或https://github.com/nostra13/Android-Universal-Image-Loader或其他的庫文件來做到這一點。 –

+0

當您將數組添加到數組中時,您需要爲每個圖像設置標籤值,並且當我們滾動列表時,@AdamRadomski是正確的。 –

+1

這是我如何設置標籤。檢查我的答案在這裏:http://stackoverflow.com/a/16569700/1739882 –

回答

0

我也經歷過相同。我也在尋找一個正確的解決方案。據我搜索,我知道ListView清除以前的視圖,而向下滾動時,並重新加載它,當你回滾。因此,在向上和向下滾動時,圖像可能會重新循環和錯誤對齊。 (我也在等待正確的解決方案)。

但我已經解決了它使用SmartImageView,這是一個庫,直接下載圖像並將其設置爲ImageView。它會將圖像保存在緩存中,因此您可以獲得正確的圖像。

相比之下,這也是更快。

+1

我覺得像使用庫讓我愚蠢,因爲我試圖讓自己的兄弟,找到我的編碼錯誤後我把我的編碼撕掉!!謝謝你的回答** VIGNESH ** –

-1

試試我在應用程序中使用過的這段代碼,它在我的應用程序中工作正常,我相信它會在你的最後工作。 在我的情況我從服務器檢索圖像和一些數據,並保持列表上的所有圖像滾動罰款。

class OfferCustomListAdapter extends ArrayAdapter<String> 
    {  
     private Context context; 
     Boolean OddNumber; 
     ArrayList<String> getDealID = new ArrayList<String>(); 
     ArrayList<String> getInAdpterUNamedlist = new ArrayList<String>(); 
     ArrayList<String> getShopNData = new ArrayList<String>(); 
     ArrayList<String> getUserFav = new ArrayList<String>(); 
     ArrayList<String> getTotalAmt = new ArrayList<String>(); 
     ArrayList<String> getDealImage = new ArrayList<String>(); 
     ArrayList<Boolean> getBoolnState = new ArrayList<Boolean>(); 
     //String Oflist[] ; 
     int favCount=0; 
     public OfferCustomListAdapter(Context context,ArrayList<String> dealIdlist, ArrayList<Boolean> AddBoolnList, ArrayList<String> dealNamelist,ArrayList<String> ShopNList,ArrayList<String> UserFave,ArrayList<String> TotalAmt,ArrayList<String> ImageList) { 
     super(context, android.R.layout.simple_list_item_1,dealNamelist); 
     this.context=context; 
     //Oflist = getFolwerUNamelis; 
     getDealID = dealIdlist; 
     getInAdpterUNamedlist = dealNamelist; 
     getShopNData = ShopNList; 
     getUserFav = UserFave; 
     getTotalAmt = TotalAmt; 
     getDealImage = ImageList; 
     getBoolnState = AddBoolnList; 


    } 

    @Override 
    public View getView(final int pos, View view, ViewGroup parent) { 

     final ViewHolder holder; 
     if (view == null) { 
      LayoutInflater inflater = LayoutInflater.from(this.context); 
      //view = inflater.inflate(R.layout.offer_custom_list, parent,false); 
      view = inflater.inflate(R.layout.reservatin_row, parent,false); 
      holder = new ViewHolder(); 
      //holder.FollowrName = (TextView) view.findViewById(R.id.OfferNameTxt); 
      holder.DealName = (TextView) view.findViewById(R.id.tv_name); 
      holder.ShopName = (TextView) view.findViewById(R.id.tv_address); 
      holder.FavBtn = (ImageView) view.findViewById(R.id.Ofr_FavBtn); 
      holder.listLayout = (LinearLayout) view.findViewById(R.id.OfferListLayout); 
      holder.profile_image = (ImageView)view.findViewById(R.id.profile_img); 
      holder.OfferAmtBtn =(Button)view.findViewById(R.id.TotalOfrBtn); 
      //holder.FavBtn = (ImageView) view.findViewById(R.id.offerFavBtn); 
      holder.FavBtn.setTag(pos); 
      view.setTag(holder); 
     } else { 
      holder = (ViewHolder) view.getTag(); 
     } 

     if (pos % 2 == 0){ 
      System.out.println("You entered an even number. "+pos % 2); 
      holder.listLayout.setBackgroundResource(R.drawable.offer_list_bg); 
     }else{ 
      System.out.println("You entered an odd number."); 
      holder.listLayout.setBackgroundResource(R.drawable.special_offer_bg); 
     } 
      /*if(getUserFav.get(pos).equals("0")){ 
      //BolArraylist.add(false); 
      holder.FavBtn.setBackgroundResource(R.drawable.fav_btn); 
      }else{ 
      //BolArraylist.add(true); 
      holder.FavBtn.setBackgroundResource(R.drawable.fav_active_btn); 
      }*/ 
      holder.DealName.setText(getInAdpterUNamedlist.get(pos)); 
      holder.ShopName.setText(getShopNData.get(pos)); 
      holder.OfferAmtBtn.setText("$"+getTotalAmt.get(pos)); 
      imgLoader.DisplayImage(getDealImage.get(pos), holder.profile_image); 

     holder.FavBtn.setOnClickListener(new View.OnClickListener() { 

       @Override  
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        if (isNetworkAvailable()) { 

        if(!userid.equals("")){ 
         Offer_ID = getDealID.get(pos); 
         GUsrFavState = getUserFav.get(pos); 
         if(GUsrFavState.equals("0")){ 
          GUsrFavState="1"; 
          getUserFav.remove(pos); 
          getUserFav.add(pos, "1"); 
          holder.FavBtn.setBackgroundResource(R.drawable.fav_active_btn); 
          getBoolnState.set(pos, true); 
          new Call_OfferFavWS().execute(); 
         }else{ 
          GUsrFavState="0"; 
          holder.FavBtn.setBackgroundResource(R.drawable.fav_btn); 
          getUserFav.remove(pos); 
          getUserFav.add(pos, "0"); 
          getBoolnState.set(pos, false); 
          new Call_OfferFavWS().execute(); 
         } 
        }else{ 
         Intent CallSignIn = new Intent(DollarMainActivity.this,SingInActivity.class); 
         startActivity(CallSignIn); 
         } 
        } else { 
         Toast alrtMsg = Toast.makeText(DollarMainActivity.this, "No network connection available !!!", Toast.LENGTH_LONG); 
         alrtMsg.setGravity(Gravity.CENTER, 0, 0); 
         alrtMsg.show(); 
        } 

       } 
      }); 

      if(getBoolnState.get(pos)){ 
       holder.FavBtn.setBackgroundResource(R.drawable.fav_active_btn); 
      }else{ 
       holder.FavBtn.setBackgroundResource(R.drawable.fav_btn); 
      } 
      return view; 
     } 

     class ViewHolder { 
      public TextView DealName,ShopName; 
      public ImageView FavBtn, profile_image; 
      public LinearLayout listLayout; 
      public Button OfferAmtBtn; 
     } 
    } 

希望它能幫助你。

如果您需要任何幫助,請讓我知道。