【发布时间】: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