我有一個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();
}
請幫助我,在此先感謝。