4

So I'm wondering how to go about filling a ListView with empty rows. The ListView is populated via SQLite db so say for instance there is only 3 items in the list I want to fill the rest of the screen with empty rows. Here is a screenshot of what I mean. Yes I know it's from iPhone but it demonstrates what I mean:

image 1

4 Answers 4

3

The cheap way is to add extra "rows" in your layout xml. Any extra rows will be cut off by the screen. This can get messy: a higher-res screen might require you add many extra TextViews. Since they get cut off, I suppose you could add as many as you'd like. It would look something like this:

<LinearLayout>

<ListView></ListView>
    
<TextView/>
<TextView/>
<TextView/>
...

</LinearLayout>

Another option is to add extra rows to your ListView, as mentioned previously. If you are bound to an array, add extra blank rows to the array and handle it appropriately. Coincidentally, if you are using a Cursor you can use a MaxtrixCursor & MergeCursor as described in this answer

I ended up using a combination of these methods. I try my best to calculate the number of rows I want to add to my ListView, but I error on the side of caution and have a couple TextViews underneath my ListView that make it all come together.

Sign up to request clarification or add additional context in comments.

Comments

2

When you create the Array the is going to be bound to the ListView you just need to add a few rows at the end of the Array with empty strings.

4 Comments

Hm, but I just want to fill the blank area of the screen (if it exists) like above. If I had just blank sqlite entries they still show empty rows when scrolling.
Could you place the ListView in a RelativeLayout and set the RelativeLayout height = "fill_parent"?
You could also find the height of the screen in your java file and determine how many "blank" rows to add.
I thought of an alternate solution that doesn't involve adding empty items. I just wrapped the contents of listview and added a view underneath where I could draw lines. Got the lines down...now just to get the line spacing to match that of the listview.
0

Using a textview below the listview seems to give the illusion of what I was going for.

Comments

0

@kabir's solution works. Now if you are like me (wanted to have two alternate colors in background, this is his dispached method rewritten (or let's say edited)

 @Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    int caseLastChildBottom = -1;
    int caselastChildHeight = -1;
    int caseNrOfLines = -1;

    //makes the colors follow the order (alternateColor1 - alternateColor2 - alternateColor1 - etc.)
    int plusIndex = 1;
    if (this.getChildCount() % 2 == 0)
        plusIndex = 0;

    // ListView's height
    final int currentHeight = getMeasuredHeight();

    // this will let you know the status for the ListView, fitting/not fitting content
    final int scrolledHeight = computeVerticalScrollRange();

    //empty listview (no item)
    if (scrolledHeight == 0) {
        //no childs exist so we take the top
        caseLastChildBottom = 0;
        //last child doesn't exist, so we set a default height (took the value in dp in the item row's height)
        caselastChildHeight = convertDpToPx(DEFAULT_CHILD_ROW_S_HEIGHT);
        // determine the number of lines required to fill the ListView
        caseNrOfLines = currentHeight / caselastChildHeight;
    }

    //there is a remaining gap to fill
    else {
        final View lastChild = getChildAt(getChildCount() - 1);
        if (lastChild == null) return;
        // values used to know where to start drawing lines
        caseLastChildBottom = lastChild.getBottom();
        // last child's height(use this to determine an appropriate value for the row height)
        caselastChildHeight = lastChild.getMeasuredHeight();
        // determine the number of lines required to fill the ListView
        caseNrOfLines = (currentHeight - caseLastChildBottom) / caselastChildHeight;
    }
    // values used to know where to start drawing lines
    final int lastChildBottom = caseLastChildBottom;
    // last child's height(use this to determine an appropriate value for the row height)
    final int lastChildHeight = caselastChildHeight;
    // determine the number of lines required to fill the ListView
    final int nrOfLines = caseNrOfLines;

    int i = 0;
    for (i = 0; i < nrOfLines; i++) {
        Rect r = new Rect(0, lastChildBottom + i * lastChildHeight, getMeasuredWidth(), lastChildBottom + (i + 1) * lastChildHeight);
        canvas.drawRect(r, (i + plusIndex) % 2 == 0 ? alternateColorView1 : alternateColorView2);
    }

    //is there a gap at the bottom of the list
    if(currentHeight - (nrOfLines *lastChildHeight) > 0){
        Rect r = new Rect(0, lastChildBottom + i * lastChildHeight,getMeasuredWidth(), currentHeight);
        canvas.drawRect(r, (i + plusIndex) % 2 == 0 ? alternateColorView1 : alternateColorView2);
    }
    return;
}

I forgot these two Paint colors (just as @kabir declared the colors):

    alternateColorView1.setColor(....);
    alternateColorView1.setStyle(Paint.Style.FILL);

    alternateColorView2.setColor(....);
    alternateColorView2.setStyle(Paint.Style.FILL);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.