我在我的Android应用程序中添加了一个最喜欢的按钮来节省药房 在一个使用SharedPreference的片段中,我得到了这个错误“无法访问的语句”在我更改单击按钮的颜色的行中
if (checkFavoriteItem(pharmacie)) {
holder.img_favorite.setImageResource(R.drawable.heart_red); /********error*********/
holder.img_favorite.setTag("red");
} else {
holder.img_favorite.setImageResource(R.drawable.heart_grey);
holder.img_favorite.setTag("grey");
}
return convertView;}
这是完整的PharmacyArrayAdapter活动:
public class PharmacyArrayAdapterfav extends ArrayAdapter<Pharmacie> {
Context myContext;
int layoutResourceId;
List<Pharmacie> pharmacie;
SharedPreference sharedPreference;
private Context context;
public PharmacyArrayAdapterfav(Context context, List<Pharmacie> pharmacie) {
super(context, R.layout.liste_layout, pharmacie);
this.context = context;
this.pharmacie = pharmacie;
myContext=context;
sharedPreference = new SharedPreference();
}
private class ViewHolder {
TextView nomTextView;
TextView villeTextView;
ImageView img_favorite;
TextView lonTextView;
}
@Override
public int getCount() {
return pharmacie.size();
}
@Override
public Pharmacie getItem(int position) {
return pharmacie.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fragment_product_list, null);
holder = new ViewHolder();
holder.nomTextView = (TextView) convertView
.findViewById(R.id.textViewNom);
holder.villeTextView = (TextView) convertView
.findViewById(R.id.txtViewVille);
holder.img_favorite = (ImageView) convertView
.findViewById(R.id.img_favorite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Pharmacie pharmacie = (Pharmacie) getItem(position);
holder.nomTextView.setText(pharmacie.getNom());
holder.villeTextView.setText(pharmacie.getVille());
return convertView;
if (checkFavoriteItem(pharmacie)) {
holder.img_favorite.setImageResource(R.drawable.heart_red);
holder.img_favorite.setTag("red");
} else {
holder.img_favorite.setImageResource(R.drawable.heart_grey);
holder.img_favorite.setTag("grey");
}
return convertView;}
/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Pharmacie checkProduct) {
boolean check = false;
List<Pharmacie> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (Pharmacie product : favorites) {
if (product.equals(checkProduct)) {
check = true;
break;
}
}
}
return check;
}
@Override
public void add(Pharmacie phar) {
super.add(phar);
pharmacie.add(phar);
notifyDataSetChanged();
}
@Override
public void remove(Pharmacie phar) {
super.remove(phar);
pharmacie.remove(phar);
notifyDataSetChanged();
}
}
此外,我想知道如何将列表保存到特定用户,在登录后找到他的收藏列表并点击收藏夹按钮,我需要帮助,谢谢。
答案 0 :(得分:0)
哦,天哪,我完全没有看到一个非常明显的原因,为什么你有无法访问的代码 - 你还有一个额外的return
不应该存在:
Pharmacie pharmacie = (Pharmacie) getItem(position);
holder.nomTextView.setText(pharmacie.getNom());
holder.villeTextView.setText(pharmacie.getVille());
return convertView; //THIS ONE -> just remove it and it'll work
if (checkFavoriteItem(pharmacie)) {
holder.img_favorite.setImageResource(R.drawable.heart_red);
holder.img_favorite.setTag("red");
} else {
holder.img_favorite.setImageResource(R.drawable.heart_grey);
holder.img_favorite.setTag("grey");
}
return convertView;
}
无法访问的代码警告通常是此 ^,返回时不在if分支中,而后面有一些代码,这意味着该方法在此时始终为return
并且将从不运行下面的代码。它本身并不是一个错误,它只是 IDE 警告你,你可能错误地将回报放在了你并不意味着的地方。