ArrayAdapter / ListView позиция 0 не обнаружена - PullRequest
0 голосов
/ 01 декабря 2018

Я хотел бы поместить изображение медали для позиции 1, 2, 3 и спрятать его для следующих предметов в arrayadapter в сочетании с listview.

Любопытно, что после тестирования кажется,эта позиция возвращается правильно (индикатор Pin), и моя идея работает для позиций 1 и 2 (поэтому пункты 2 и 3), но не для позиции 0. Думаю, прикрепленное изображение объяснит лучше.

Код находится под изображением.

enter image description here

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).

Ответы [ 2 ]

0 голосов
/ 01 декабря 2018

изменение

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);
}

должен работать, а если нет, я понятия не имею, что происходит.

0 голосов
/ 01 декабря 2018

Вы бы попробовали это?

try{
horeca_medal = (ImageView)getApplicationContext().findViewById(R.id.horeca_medal);
//Write your own reference.

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(View.INVISIBLE); //This edit
    } else {
        horeca_medal.setImageResource(R.drawable.first);
    }
}catch(Excaption ex)
{
android.util.Log.e("YourError", ex.getMessage());
}

Можете ли вы дать нам результат LogMessage?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...