【问题标题】:Update GridView in Asynctask doesn't work在 Asynctask 中更新 GridView 不起作用
【发布时间】:2015-06-10 16:46:40
【问题描述】:

我有一个 GridView,我想在单击一个按钮后对其进行更新(我想在单击后更改按钮的文本和颜色)。我尝试使用 notifyDataSetChanged();在 onClick 方法中,但什么也没发生。

我使用 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;
}

这是我的异步任务

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();

    }

请帮助我,提前谢谢。

【问题讨论】:

    标签: android gridview android-asynctask sharedpreferences notifydatasetchanged


    【解决方案1】:

    问题是您仅在 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
    }
    

    代码需要您填写其余部分,但我认为这应该为您指明正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2018-02-21
      相关资源
      最近更新 更多