Update GridView in Asynctask doesn't work

304 views Asked by At

I have a GridView, i want to update it after one button is clicked (i want to change the text and the color of the button after being clicked). I try to do it with notifyDataSetChanged(); inside the onClick method but nothing happened.

I use Asynctask to store data in my sharedPrefereces (in PostExecute), part of this data is the "brandName" of the item/button i have clicked. I try to update de View with notifyDataSetChanged(); in PostExecute method but is not working.

I compare the data in getView method to change the color of the brandButton stored in sharedprefereces and i want to refresh the view if i click in other button.

P.D. sorry for my poor english

This is my getView method

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

and this is my 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();

    }

Please help me, thanks in advance.

1

There are 1 answers

0
Cruceo On

The problem is you're only setting the values of your views when convertView == null, which probably won't be true in your case after the first few times getView() is called.

If you move the calls to set the text/colors of your TextView to outside of the block where convertView is null, your code should work once the data has been downloaded.

Since you'll have to move the TextView's declaration to outside of the method, you should really use some kind of ViewHolder to make your life easier and the adapter more efficient. http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html actually does a decent job of explaining it, but here's a little bit related to your code:

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
}

The code needs you to fill in the rest, but I think that should point you in the right direction.