Question

the thing is in my customlistadapter it will show a text and a picture in six rows. But in the last row, It will only show the text, not the picture. When I put 0 instead of the picture, they show the default pic, but I dont want to have any pic there. How to do it? Any tips would be really valuable thank you.

In this line:

-> 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));
}
Was it helpful?

Solution

You should hide the ImageView when the icon id is 0. Try this:

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

OTHER TIPS

No u have to give image id, You have not any other option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top