0

我有一個GridView,我想在單擊一個按鈕後更新它(我想在點擊後更改文本和按鈕的顏色)。我嘗試通過notifyDataSetChanged()來做到這一點。在onClick方法中,但沒有發生任何事情。在Asynctask中更新GridView不起作用

我使用Asynctask將數據存儲在我的sharedPrefereces中(在PostExecute中),這些數據的一部分是我單擊的項目/按鈕的「品牌名稱」。我嘗試使用notifyDataSetChanged()更新de View;在PostExecute方法但不起作用。

我比較getView方法中的數據,以改變存儲在sharedprefereces中的brandButton的顏色,我想刷新視圖,如果我點擊其他按鈕。

P.D.對不起,我的英文不好

這是我getView方法

public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    View grid; 
    LayoutInflater inflater = (LayoutInflater) mContext 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (convertView == null) { 
     grid = new View(mContext); 
     grid = inflater.inflate(R.layout.grid_single, null); 
     TextView brandName = (TextView) grid.findViewById(R.id.grid_text); 
     ImageView brandLogo = (ImageView) grid.findViewById(R.id.grid_image); 
     ImageView twitterImg = (ImageView) grid.findViewById(R.id.twitterImg); 
     ImageView facebookImg = (ImageView) grid.findViewById(R.id.facebookImg); 
     ImageView instagramImg = (ImageView) grid.findViewById(R.id.instagramImg); 
     Button selectBrand = (Button) grid.findViewById(R.id.selectBrand); 

     if(mBrandGenericData.getDataList().get(position).socialList.size()>0){ 
      for(int i=0; i<mBrandGenericData.getDataList().get(position).socialList.size(); i++){ 
       if(mBrandGenericData.getDataList().get(position).socialList.get(i).socialType.equalsIgnoreCase("FB")){ 
        facebookImg.setImageDrawable(facebookDrawable); 
       } 
       if(mBrandGenericData.getDataList().get(position).socialList.get(i).socialType.equalsIgnoreCase("TW")){ 
        twitterImg.setImageDrawable(twitterDrawable); 
       } 
       if(mBrandGenericData.getDataList().get(position).socialList.get(i).socialType.equalsIgnoreCase("IT")){ 
        instagramImg.setImageDrawable(instagramDrawable); 
       } 
      } 
     } 
     new ImageLoadTask("http://api.socialmanageranalytics.com/"+mBrandGenericData.getDataList().get(position).logo, brandLogo).execute(); 
     brandName.setText(mBrandGenericData.getDataList().get(position).name); 
//Here i change the color and text of my button   
     if (brandName.getText().equals(PreferenceManager.getDefaultSharedPreferences(mContext).getString("brandName", ""))){ 
      selectBrand.setText(R.string.selected); 
      selectBrand.setBackgroundColor(0xFF18abd5); 
     } 
     selectBrand.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       new SelectBrandTask("http://api.socialmanageranalytics.com/"+mBrandGenericData.getDataList().get(position).logo, mBrandGenericData.getDataList().get(position).name, mBrandGenericData.getDataList().get(position).id).execute(); 
       CustomGrid.this.notifyDataSetChanged(); 
      } 
     }); 
    } else { 
     grid = (View) convertView; 
    } 

    return grid; 
} 

,這是我的AsyncTask

public class SelectBrandTask extends AsyncTask<Void, Void, Bitmap> { 

    private String url; 
    private String brandName; 
    private String id; 
    private ProgressDialog dialog; 

    public SelectBrandTask(String url, String brandName, String id) { 
     this.url = url; 
     this.brandName = brandName; 
     this.id = id; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     dialog = ProgressDialog.show(mContext, "Seleccionando marca", 
        "Por favor espere..."); 
    } 

    @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); 
     SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(mContext); 
     SharedPreferences.Editor editor = myPreference.edit(); 
     editor.putString("brandName", brandName); 
     editor.putString("user", user); 
     editor.putString("brandImage", encodeTobase64(result)); 
     editor.putString("brandId", id); 
     editor.commit(); 

     if (dialog != null) 
      dialog.dismiss(); 

     CustomGrid.this.notifyDataSetChanged(); 

    } 

請幫助我,在此先感謝。

回答

0

問題是你只是設置你的意見的價值,當convertView == null,這可能不會在你的情況下,在前幾次調用getView()後真實。

如果您移動呼叫以將TextView的文本/顏色設置爲convertView爲空的塊的外部,則代碼應在數據下載完成後運行。

由於您必須將TextView的聲明移到方法的外部,因此您應該使用某種ViewHolder來使您的生活更輕鬆,並且適配器更加高效。 http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html實際上做了解釋它的一個體面的工作,但這裏涉及到你的代碼一點點:

public View getView(final int position, View convertView, ViewGroup parent) { 
    final BrandsHolder holder; 
    if(convertView == null){ 
     holder = new BrandsHolder(); 
     convertView = inflater.inflate(R.layout.grid_single, parent, false); 

     holder.brandName = (TextView)convertView.findViewById(R.id.grid_text); 
     .... // The rest of your item declarations 

     convertView.setTag(holder); 
    } 
    else holder = (BrandsHolder)convertView.getTag(); 

    final BrandGenericData brandData = BrandGenericData.getDataList().get(position); 

    new ImageLoadTask("http://api.socialmanageranalytics.com/" + brand.logo, holder.brandLogo).execute(); 

    holder.brandName.setText(brand.name); 
    if (holder.brandName.getText().equals(PreferenceManager.getDefaultSharedPreferences(mContext).getString("brandName", ""))){ 
     holder.selectBrand.setText(R.string.selected); 
     holder.selectBrand.setBackgroundColor(0xFF18abd5); 
    } 

    ... // Any other adjustments you want to make for each item 
} 

private class BrandsHolder { 
    TextView brandName; 
    ImageView brandLogo; 
    Button selectBrand; 
    ... // The rest of your ViewHolder's variable declarations 
} 

代碼需要您填寫的休息,但我認爲應該指向你在正確的方向。