-1

I need help creating dynamic buttons for listView items inside the adapter. Following code is my adapter getView function:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView==null) {
            convertView = getLayoutInflater().inflate(R.layout.sell_animal_list_entry,null);
        }
        Animal animal = (Animal) getItem(position);
        int count = 0;
        int value = 0;
        int age = 0;
        int lifespan = 0;
        int savePosition = 0;
        String name = null;

        lifespan = animal.getLifespan();
        age = animal.getAge();
        value = animal.getValue();
        name = animal.getName();
        savePosition = animal.getPosition();

        LinearLayout buttonLayout = (LinearLayout) convertView.findViewById(R.id.buttonIncludeLayout);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Button sellButton = new Button(SellAnimalActivity.this);
        sellButton.setText(getResources().getString(R.string.sellAnimalButtonText));
        count = buttonLayout.getChildCount();
        if (count == 0) {
            final int finalSavePosition = savePosition;
            sellButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    displaySellWarning(finalSavePosition);
                }
            });
            buttonLayout.addView(sellButton, lp);
        }

        return convertView;
    }

My problem now is that I can not sell the correct animals on button click. I have 5 different animals inside the List. The first 3 animals work correctly, but the 4th and 5th animal get wrong buttons to sell the first animal, even though the names are correctly looped.
EDIT: To further explain: My ListView layout shows the name of the animal, its lifespan and age and I dynamically add a button to it. The name, age and lifespan are correctly applied for each animal. The button is supposed to sell the animal. And it works up to animal 3 on the list. But the button for the 4th and 5th animal tries to sell the 1st animal. When I Log.d the savePosition I get the following values: 0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,3,4 It seems weird to me that he reads the 4th and 5th entry only on the 5th loop?

After so many hours on that problem, maybe you can give me the solution hint for this, why are the 4th and 5th animals wrong?

  • Why do you only set the ClickListener when the LinearLayout has no elements? – OneCricketeer Mar 18 '16 at 15:41
  • Also, a [mcve] would be great. You are missing a fair amount of code in order to see what your problem is. – OneCricketeer Mar 18 '16 at 15:42
  • What else would you need, cricket? The complete adapter? – Daniel Scholz Mar 18 '16 at 22:13
  • Your problem seems to be `(Animal) getItem(position)` for position 4 and 5 are not correct based on how you described the issue. At least describe what you mean by *get wrong buttons*... What is wrong about them, and more important -- what would make then correct? – OneCricketeer Mar 18 '16 at 22:23
  • I edited my entry above to further explain it, hope this gives you a better idea, I'm super confused about this bug. – Daniel Scholz Mar 19 '16 at 07:42
  • What do you mean by "5th loop"? There is no loop in the code you've shown. You may also want to try using the [ViewHolder Pattern](https://dzone.com/articles/optimizing-your-listview) – OneCricketeer Mar 19 '16 at 08:05

1 Answers1

0

For some reason I did not find it before, when I was searching, here is someone with the same problem as me: related question

And the solution is also posted there. ListView gets confused apparently when the listView has a layout:height of wrap_content. It needs to be fill_parent.

Community
  • 1
  • 1