54

Why when i use SimpleCursorAdapter for ListView i have items height in ListView like this - listview image

(My code based on this)

But when using arrays Listview items have big height

listview big

(I learn listview based on this)

Row layout for item listview is

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

So My question is why there is a difference in row heights when using ArrayAdapter and SimpleCursorAdapter?

Community
  • 1
  • 1
olegi
  • 2,051
  • 3
  • 16
  • 9

7 Answers7

136
  android:textAppearance="?android:attr/textAppearanceLarge" 

seemed no effect.

  android:minHeight="?android:attr/listPreferredItemHeight" 

changed the height for me

Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83
Bamboo
  • 2,046
  • 2
  • 16
  • 21
37

The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.

Evan
  • 749
  • 6
  • 11
  • Spent almost an hour trying to find a solution. This is exactly what did the trick for me. – ejel Jun 10 '13 at 00:06
11

You need to use padding on the list item layout so space is added on the edges of the item (just increasing the font size won't do that).

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:padding="8dp" />
afollestad
  • 2,929
  • 5
  • 30
  • 44
5

I did something like that :

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

    View view = super.getView(position, convertView, parent);

    TextView textView = (TextView) view.findViewById(android.R.id.text1);
    textView.setHeight(30);
    textView.setMinimumHeight(30);

    /*YOUR CHOICE OF COLOR*/
    textView.setTextColor(Color.BLACK);

    return view;
}

You must put the both fields textView.setHeight(30); textView.setMinimumHeight(30); or it won't change anything. For me it worked & i had the same problem.

Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
omnisius
  • 229
  • 5
  • 12
1

The height of list view items are adjusted based on its contents. In first image, no content. so height is very minimum. In second image, height is increased based on the size of the text. Because, you specified android:layout_height="wrap_content".

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
spr
  • 322
  • 3
  • 15
0

Here is my solutions; It is my getView() in BaseAdapter subclass:

public View getView(int position, View convertView, ViewGroup parent)
{
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.list_view, parent,false);
        System.out.println("In");
    }
    convertView.setMinimumHeight(100);
    return convertView;
}

Here i have set ListItem's minimum height to 100;

Sravan Kumar
  • 1,447
  • 3
  • 19
  • 41
-1

This is my solution(There is a nested LinearLayout):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item"
            android:layout_width="match_parent"
            android:layout_height="47dp"
            android:background="@drawable/box_arrow_top_bg"
            android:gravity="center"
            android:text="全部收支"
            android:textColor="#666"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>
Praise Song
  • 187
  • 2
  • 2
  • Unnecessary nesting is bad practice and bad for performance. The nested LinearLayout doesn't make a different here, either. – afollestad Mar 25 '15 at 17:00