我有一个GridView,我想在点击一个按钮后更新它(我想在点击后更改文本和按钮的颜色)。我尝试使用notifyDataSetChanged();在onClick方法中,但什么也没发生。
我使用Asynctask将数据存储在我的sharedPrefereces中(在PostExecute中),此数据的一部分是" brandName"我点击的项目/按钮。我尝试使用notifyDataSetChanged()更新de View;在PostExecute方法中但不起作用。
我比较getView方法中的数据来改变存储在sharedprefereces中的brandButton的颜色,如果我点击其他按钮,我想刷新视图。
Pd积。抱歉我的英语不好
这是我的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();
}
请提前帮助我。
答案 0 :(得分:0)
问题是,您只是在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
}
代码需要你填写其余部分,但我认为这应该指向正确的方向。