我的CustomListAdapter

我的CustomListAdapter

事情在我的customlistadapter中,它将在六行中显示文本和图片。但是在最后一行,它将仅显示文本,而不显示图片。当我放0而不是图片时,它们显示默认图片,但是我不想在那儿有任何图片。怎么做?任何提示都是非常有价值的,谢谢。

在这一行:

-> pilsTyper.add(new Pilstyper("Egendefinert", 0));


ListAdapterClass

private class MyListAdapter extends ArrayAdapter<Pilstyper> {
    public MyListAdapter() {
        super(velgDinPromille.this, R.layout.activity_item_view, pilsTyper);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(
                    R.layout.activity_item_view, parent, false);
        }

        // finn pilstype som du skal jobbe med
        Pilstyper nyPils = pilsTyper.get(position);

        // ImageView
        ImageView imageView = (ImageView) itemView
                .findViewById(R.id.lettol);
        imageView.setImageResource(nyPils.getIkonId());

        // Tekst
        TextView tekst = (TextView) itemView.findViewById(R.id.tekst_pils);
        tekst.setText(nyPils.getTekst());

        return itemView;
    }
}

private void pilsTyperList() {
    // TODO Auto-generated method stub
    pilsTyper.add(new Pilstyper("Øl", R.drawable.ol));
    pilsTyper.add(new Pilstyper("Cider og rusbrus", R.drawable.cider));
    pilsTyper.add(new Pilstyper("Vin", R.drawable.vin));
    pilsTyper.add(new Pilstyper("Sterkvin", R.drawable.portvin2));
    pilsTyper.add(new Pilstyper("Brennevin/Drinker", R.drawable.drink2));
    pilsTyper.add(new Pilstyper("Egendefinert", 0));
}

最佳答案

当图标ID为0时,您应该隐藏ImageView。尝试以下操作:

if (nyPils.getIkonId() == 0) {
    imageView.setVisibility(View.GONE); // or View.INVISIBLE
} else {
    imageView.setVisibility(View.VISIBLE);
    imageView.setImageResource(nyPils.getIkonId());
}

10-08 20:21