我想在位置1、2、3放置奖牌图像,并将其隐藏在arrayadapter
和listview
结合的以下项目中。
奇怪的是,经过测试,似乎位置可以正确返回(Pin指示器),并且我的想法适用于位置1和2(因此项目2和3),但不适用于位置0。所附的图像将更好地说明我认为。
代码在图片下方。
public class TopHorecaAdapter extends ArrayAdapter<TopHorecaElement> {
private final List<TopHorecaElement> mTopHorecas;
private final Context mContext;
TextView horeca_name;
TextView horeca_type;
TextView horeca_address;
TextView horeca_pins;
ImageView horeca_medal;
CircularImageView image;
public TopHorecaAdapter(@NonNull final Context context, @NonNull final List<TopHorecaElement> objects){
super(context, R.layout.fragment_top_horecas, objects);
mTopHorecas = objects;
mContext = context;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent){
final TopHorecaElement topHorecaElement = mTopHorecas.get(position);
if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_top_horeca, parent, false);
}
Integer pst = position;
horeca_name = convertView.findViewById(R.id.tophoreca_name);
horeca_type = convertView.findViewById(R.id.tophoreca_asl);
horeca_address = convertView.findViewById(R.id.tophoreca_pinnumber);
horeca_pins = convertView.findViewById(R.id.tophoreca_address);
horeca_medal = convertView.findViewById(R.id.tophoreca_medal);
horeca_name.setText(topHorecaElement.getBarName());
horeca_pins.setText(topHorecaElement.getNbr() + " Pin'Ups");
horeca_address.setText(pst.toString());
horeca_type.setText("");
horeca_medal.setImageResource(R.drawable.first);
if (pst <= 0) {
horeca_medal.setImageResource(R.drawable.first);
} else if (pst == 1) {
horeca_medal.setImageResource(R.drawable.second);
} else if (pst == 2) {
horeca_medal.setImageResource(R.drawable.third);
} else if (pst > 2 && pst < mTopHorecas.size()){
horeca_medal.setVisibility(INVISIBLE);
} else {
horeca_medal.setImageResource(R.drawable.first);
}
return convertView;
}
以下是该片段中的代码:
@Override
void initEvent(@Nullable final View view) { HorecaPinDataManager.getTopHoreca(PinUpApplication.getInstance().user.getCity());}
@Subscribe
public void onTopHorecaEventReceived(TopHorecasReceivedEvent event){
try {
if (!event.mTopHoreca.isEmpty()) {
adapter = new TopHorecaAdapter(getActivity(), event.mTopHoreca);
mTopHoreca.setAdapter(adapter);
} else {
HorecaPinDataManager.getAlltimeTopHoreca(PinUpApplication.getInstance().user.getCity());
}
} catch (IndexOutOfBoundsException | NullPointerException e){
HorecaPinDataManager.getAlltimeTopHoreca(PinUpApplication.getInstance().user.getCity());
}
}
@Subscribe
public void onTopAlltimeHorecaEventReceived(TopAlltimeHorecasReceivedEvent event){
adapter = new TopHorecaAdapter(getActivity(), event.mTopHoreca);
mTopHoreca.setAdapter(adapter);
}
似乎只有第一个事件被触发,该图标才会显示。仅在触发第二个事件(onAlltimeTopHorecaEvent)
时它才起作用。
答案 0 :(得分:0)
您会尝试吗?
SERVER_PORT
您能给我们LogMessage结果吗?
答案 1 :(得分:0)
改变
Integer pst = position
到
int pst = position
应该解决您的问题。
编辑:
好的,因此您始终在开始之前将图像设置为drawable.first
。
horeca_medal.setImageResource(R.drawable.first);
if (pst == 1) {
horeca_medal.setImageResource(R.drawable.second);
} else if (pst == 2) {
horeca_medal.setImageResource(R.drawable.third);
} else if(pst > 2) {
horeca_medal.setVisibility(INVISIBLE);
}
应该工作,如果不行,我不知道会发生什么。