0

The Problem

My way to add the view makes every fifth item to add the view when i only want one position to have this "Mängd" row. Why Can i only edit listitems when they are visible on the screen. The child will be static at 5 items even though i got like 20 item.... Is there any way to only say that item 1 will have this and not

position - firstvisibleposition

i think this is the problem with the listview

My code is not understandable at the time because of other things so i hope you get my problem anyways.

This is my main question

It seems like the thing i add to position 0 also happens to 6 and 12 Why is ListView this wierd ? enter image description here It's on swedish, but this is what i got with list view. Every listview item has a empty Linearlayout that i add a view to when i press the down arrow button. the problem is that every fifth item gets this or i can only click on the first 5.

I dont get why they make ListView so complicated. i want to be able to get a child that is in the list without seening it!

CODE getView

    public View getView (int position, View convertView, ViewGroup parent){
            if (convertView == null)
            {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.view_meal_item_editable, null);
            }
            convertView.setOnTouchListener(new ItemSwipeListener(position,
                    getResources().getDisplayMetrics().density));
            convertView.setClickable(true);
            // Lookup view for data population
            TextView food_item_name = (TextView) convertView.findViewById(R.id.food_item_name);
            food_item_name.setHint("hello");
    }

Where i add the view

    View view = searchResultList.getAdapter().getView(position, searchResultList.getChildAt(position - searchResultList.getFirstVisiblePosition()), searchResultList);
        LinearLayout extendedView = (LinearLayout)view.findViewById(R.id.extended_food_information);
        View convertExtendedView = LayoutInflater.from(this).inflate(R.layout.change_amount_on_food_view, null);
extendedView.addView(convertExtendedView);
  • 1
    It's happen because of ListView recycling mechanism: http://stackoverflow.com/a/14108676/4224337 . Post your adapter code (maybe just the *getView()* method ), if you want some help. – Rami Apr 01 '15 at 10:58
  • Added the getView :) – Marcus Lagerstedt Apr 02 '15 at 14:40
  • Where is the "thing" that you have added to position 0? I don't see anything special for this position. – Rami Apr 02 '15 at 15:04
  • Added the code where i add the extra view depending on position – Marcus Lagerstedt Apr 02 '15 at 15:33
  • 1
    If you want some fixed view at pos = 0, just use a header view. – natario Apr 02 '15 at 15:39
  • 1
    I agree with "m iav" comment, to use a header view if its only for the first element. Otherwise it will be better if you add your extra view in *getView()* method, something like `if(position==0){// add extra view} else {// remove extra view if exist}`. Or you can remove this line: `if (convertView == null)` so you will inflate a new layout each time, it will solve your problem but this is not good for list performance – Rami Apr 02 '15 at 16:06
  • I think i got this! Maybe i can handle that the performance is lower to get a list that is more like i want! Then if the if (convertView == null) is removed i will redraw the row item every time right ? What is the header view ? – Marcus Lagerstedt Apr 03 '15 at 12:16
  • @Rami if you write a answer i will mark it asa solution for this problem, thanks for the help :) – Marcus Lagerstedt Apr 04 '15 at 10:20

1 Answers1

1

It's recommended to use a header view if you do this stuff only for the first element.

Otherwise it will be better if you add your extra view in getView() method, something like:

if(position==0){
    // add extra view
} else {
    // remove extra view if exist
} 

Or you can remove the IF condition: if (convertView == null), so you will inflate a new layout each time, it will solve your problem but this is not good for list performance

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66