Nick
Nick

Reputation: 3494

Return empty row in Custom ArrayAdapter?

when my ListView is being filled by a custom Array Adapter, I check for a certain parameter in the current list item, and if it is false, I want to return an empty row. At the moment, I am doing this by inflating an empty xml file, but that creates ugly list item dividers when a number of items should not be shown, and is obviously not the best way ;-)!

    if (list.get(position).equals("i-dont-want-this-in-the-list-view")){
        View empty=inflater.inflate(R.layout.empty_row, parent, false);
        return(empty);
    }

I have tried to return "null", but it clearly expects a View to be returned. Is there anything I could do without having to filter the list that is being used for the ListView beforehand (I want to keep it intact as I am doing other things with it, too).

Thanks,

Nick

Upvotes: 4

Views: 4257

Answers (5)

lx222
lx222

Reputation: 248

What's about:

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
   //use a global Array to set the rows visible/hidden and return empty rows
   if(disArray[position] == false) return new View(getContext()); 

   ... // continue with your getView() implementation
   return convertView;
}

to update the ListView use

adapter.notifyDataSetChanged();

Upvotes: 1

Zoombie
Zoombie

Reputation: 3610

View.Gone will give u empty list row. try using layout params and set to zero value like this:

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(0,0); viewholder.programRow.setLayoutParams(layoutParams);

Layoutparams should be used as according to parent viewgroup used

Upvotes: 0

Pedantic
Pedantic

Reputation: 5022

What're in the rows that aren't empty? Images can be replaced by empty, transparent pngs and text views can be set to "", etc.

Upvotes: 0

user705142
user705142

Reputation: 461

To fix the issue of line dividers, remove the line dividers from the ListView and put your own in inside the individual items.

Upvotes: 6

Pentium10
Pentium10

Reputation: 207863

Inflate a View and setVisibility to VIEW.GONE

Upvotes: 4

Related Questions