【问题标题】:ArrayAdapter / ListView position 0 not being detected未检测到 ArrayAdapter / ListView 位置 0
【发布时间】:2018-12-01 00:21:56
【问题描述】:

我想为位置 1、2、3 放置奖牌图像并将其隐藏在 arrayadapterlistview 中的以下项目。

奇怪的是,经过测试,似乎位置返回正确(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)时才起作用。

【问题讨论】:

  • 我很确定错误来自片段代码,因为它适用于第一个事件接收(因此,如果 TopHorecasReceivedEvent 不为空,请执行代码...)有效。如果它是空的,我再次调用我的 API 并接收其他元素,这就是提到的问题所在..

标签: android listview fragment adapter


【解决方案1】:

你会试试吗?

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的结果吗?

【讨论】:

  • 嗨,不幸的是,似乎没有任何错误消息。
【解决方案2】:

变化

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

应该可以,如果不行,我不知道发生了什么。

【讨论】:

  • 感谢您的回答。我之前已经尝试过,不幸的是它不起作用。
  • @VanhaerenThomas 已编辑。如果它不起作用,我放弃:D
  • 我真的不明白这是从哪里来的。无论如何,我也尝试了您的代码,但它不起作用。这没有任何意义。还是非常感谢!
猜你喜欢
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 2016-11-16
相关资源
最近更新 更多