【发布时间】:2016-04-01 13:16:54
【问题描述】:
我的片段中有一个由 BaseAdapter 填充的网格视图。
问题是在我的适配器中我有一个最喜欢的按钮,当我点击时我希望按钮改变。但是API中的逻辑已经完成,但是当我重新加载页面时我的按钮会改变。
我尝试使用 adapter.notifyDataSetChanged();但没用。
我的 getView 适配器代码:
//Implementando os clicks
itemHolder.imgFavorito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!util.isLogado(context)) {
((MainActivity) context).selectDrawerItem(new LoginFragment());
}
try {
SharedPreferences pref = context.getSharedPreferences("MyPref", 0);
produtoFavorito(new favoritarProduto().execute(pref.getString("USID", "null"), item.getId() + "").get());
} catch (Exception e) {
e.printStackTrace();
}
fragment.notifyAdapterChanged();
}
});
在 fragment.notifyAdapterChanged();行 a 只需调用我在适配器的构造函数中收到的 Fragment,这就是函数:
public void notifyAdapterChanged(){
adapter.notifyDataSetChanged();
}
EDIT1:我的布局如何 http://i.imgur.com/SPr0HBS.png
EDIT2:我的完整适配器
公共类 ProdutoAdapter 扩展 BaseAdapter {
private LayoutInflater mInflater;
private List<ProdutoResumido> itens;
private HomeFragment fragment;
public ProdutoAdapter(Context context, List<ProdutoResumido> itens, HomeFragment fragment) {
//Itens do listview
this.itens = itens;
//Objeto responsável por pegar o Layout do item.
//mInflater = LayoutInflater.from(context);
mInflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.fragment = fragment;
}
public int getCount() {
return itens.size();
}
public ProdutoResumido getItem(int position) {
return itens.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View view, ViewGroup parent) {
ItemSuporte itemHolder;
//se a view estiver nula (nunca criada), inflamos o layout nela.
if (view == null) {
//infla o layout para podermos pegar as views
view = mInflater.inflate(R.layout.lista_produtos_fg, null);
//cria um item de suporte para não precisarmos sempre
//inflar as mesmas informacoes
itemHolder = new ItemSuporte();
itemHolder.imgProduto = ((ImageView) view.findViewById(R.id.imgProduto));
itemHolder.txtPreco = ((TextView) view.findViewById(R.id.txtPreco));
itemHolder.imgFavorito = ((ImageView) view.findViewById(R.id.imgFavorito));
itemHolder.txtNomeProduto = ((TextView) view.findViewById(R.id.txtNomeProduto));
itemHolder.txtEstadoProduto = ((TextView) view.findViewById(R.id.txtEstadoProduto));
itemHolder.imgLoja = ((ImageView) view.findViewById(R.id.imgLoja));
itemHolder.txtNomeLoja = ((TextView) view.findViewById(R.id.txtNomeLoja));
//define os itens na view;
view.setTag(itemHolder);
} else {
//se a view já existe pega os itens.
itemHolder = (ItemSuporte) view.getTag();
}
final Context context = view.getContext();
//pega os dados da lista
//e define os valores nos itens.
final ProdutoResumido item = itens.get(position);
Loja loja = new Loja();
itemHolder.txtPreco.setText("R$ " + new DecimalFormat("#.00").format(item.getValor()));
itemHolder.imgFavorito.setImageResource(produtoFavorito(item.getFavorito()));
itemHolder.txtNomeProduto.setText(item.getNome());
itemHolder.txtEstadoProduto.setText("ESTADO: " + item.getTempoUso());
itemHolder.txtNomeLoja.setText(item.getLoja());
try {
if (item.getImagem() != null)
new DownloadImageTask(itemHolder.imgProduto).execute("http://200.98.202.23/JustForMoms/" + item.getImagem());
if (item.getLojaImg() != null)
new DownloadImageTask(itemHolder.imgLoja).execute("http://200.98.202.23/JustForMoms/" + item.getLojaImg());
} catch (Exception e) {
e.printStackTrace();
}
//Implementando os clicks
itemHolder.imgFavorito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!util.isLogado(context)) {
((MainActivity) context).selectDrawerItem(new LoginFragment());
}
try {
SharedPreferences pref = context.getSharedPreferences("MyPref", 0);
produtoFavorito(new favoritarProduto().execute(pref.getString("USID", "null"), item.getId() + "").get());
} catch (Exception e) {
e.printStackTrace();
}
fragment.notifyAdapterChanged();
}
});
//retorna a view com as informações
return view;
}
private int produtoFavorito(Boolean favorito) {
if (favorito)
return R.drawable.favorite;
else
return R.drawable.notfavorite;
}
/**
* Classe de suporte para os itens do layout.
*/
private class ItemSuporte {
ImageView imgProduto;
TextView txtPreco;
ImageView imgFavorito;
TextView txtNomeProduto;
TextView txtEstadoProduto;
ImageView imgLoja;
TextView txtNomeLoja;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
public class favoritarProduto extends AsyncTask<String, Void, Boolean> {
private String retorno;
private String url;
@Override
protected Boolean doInBackground(String... params) {
String usID = params[0];
String prodID = params[1];
url = "http://200.98.202.23/JustForMoms/Produto/FavoritarProduto/" + usID + "/" + prodID;
webclient client = new webclient(url);
retorno = client.getmodel();
if (retorno != null) {
if (retorno.equals("\"incluido\"")) {
return true;
} else {
return false;
}
}
return false;
}
}
}
编辑 3:
//Implementando os clicks
itemHolder.imgFavorito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!util.isLogado(context)) {
((MainActivity) context).selectDrawerItem(new LoginFragment());
}
try {
SharedPreferences pref = context.getSharedPreferences("MyPref", 0);
itemHolder.imgFavorito.setImageResource(produtoFavorito(new favoritarProduto().execute(pref.getString("USID", "null"), item.getId() + "").get()));
} catch (Exception e) {
e.printStackTrace();
}
ProdutoAdapter.this. notifyDataSetChanged();
}
});
【问题讨论】:
-
发布您的完整适配器类
-
两件事 1> produtoFavorito 方法声明显示它返回 int,在你的代码中你正在调用它但没有捕获它的返回值:o din 理解你的逻辑 2>为什么在片段上 notifyAdapterChanged?你在适配器里面吗??为什么在片段上调用 notifyAdapterChanged?为什么不简单地调用 this.notifyDataSetChanged();
-
1 - 事情,这是我的错误,已解决,但问题一直在发生。 2 - 因为 this.notifyDataSetChanged();代码不接受“无法解析方法”
-
老兄,您的 ProdutoAdapter 没有扩展任何适配器类,如 BaseAdapter 或类似的东西:o 这就是为什么它不接受 this.notifyDataSetChanged(); :) 如果可能,将您的适配器扩展为 BaseAdapter :) 然后只需调用 this.notifyDataSetChanged();并让我知道问题是否仍然存在。
-
当然是扩展:public class ProdutoAdapter extends BaseAdapter import android.widget.BaseAdapter;
标签: android gridview android-adapter notifydatasetchanged