Android 在Asynctask中更新GridView';行不通

Android 在Asynctask中更新GridView';行不通,android,gridview,android-asynctask,sharedpreferences,notifydatasetchanged,Android,Gridview,Android Asynctask,Sharedpreferences,Notifydatasetchanged,我有一个GridView,我想在点击一个按钮后更新它(我想在点击后更改按钮的文本和颜色)。我试着用notifyDataSetChanged()完成它;在onClick方法内部,但什么也没有发生 我使用Asynctask将数据存储在我的SharedPreferes中(在PostExecute中),其中一部分数据是我单击的项/按钮的“品牌名”。我尝试使用notifyDataSetChanged()更新de视图;在PostExecute方法中,但不起作用 我比较getView方法中的数据以更改存储在S

我有一个GridView,我想在点击一个按钮后更新它(我想在点击后更改按钮的文本和颜色)。我试着用notifyDataSetChanged()完成它;在onClick方法内部,但什么也没有发生

我使用Asynctask将数据存储在我的SharedPreferes中(在PostExecute中),其中一部分数据是我单击的项/按钮的“品牌名”。我尝试使用notifyDataSetChanged()更新de视图;在PostExecute方法中,但不起作用

我比较getView方法中的数据以更改存储在SharedPreferences中的brandButton的颜色,如果单击其他按钮,我希望刷新视图

对不起,我的英语很差

这是我的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 View getView(最终int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图网格;
LayoutFlater充气器=(LayoutFlater)mContext
.getSystemService(上下文布局\充气机\服务);
if(convertView==null){
网格=新视图(mContext);
网格=充气机。充气(R.layout.grid_single,null);
TextView品牌名称=(TextView)grid.findviewbyd(R.id.grid\u text);
ImageView品牌徽标=(ImageView)grid.findViewById(R.id.grid\u 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){

对于(int i=0;i而言,问题在于您仅在
convertView==null
时设置视图的值,在调用
getView()
的前几次之后,这种情况可能不会出现

如果将调用移动到
convertView
为空的块之外,以将TextView的文本/颜色设置为空,则下载数据后,代码应能正常工作

由于您必须将TextView的声明移到方法之外,因此您应该真正使用某种ViewHolder,以使您的生活更轻松,适配器更高效。实际上,在解释它方面做得很好,但这里有一点与您的代码相关:

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
}
代码需要您填写其余部分,但我认为这应该为您指明正确的方向

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
}