Search code examples
androidandroid-listviewlistviewitem

Manupilate a single row in listview


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

Solution

  • 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