flapedriza flapedriza - 6 months ago 17
Java Question

Misaligned listview

I'm making a football league app, and i need to show the classification.
I'm trying to show a header that indicates what represents each field and a listview with the teams below.
I'm using a SimpleCursorAdapter to fill the list, but if the values have a different number of digits the list is misaligned. ¿How can i align it or at least add a 0 before the one-digit numbers with the adapter?

The soure code of the fragment and the xml:

ClassificacioFragment.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

String[] from = { EquipsContract.EquipEntry.COLUMN_NAME_NOM,
EquipsContract.EquipEntry.COLUMN_NAME_VICTORIES, EquipsContract.EquipEntry.COLUMN_NAME_DERROTES,
EquipsContract.EquipEntry.COLUMN_NAME_EMPATS, EquipsContract.EquipEntry.COLUMN_NAME_PUNTS};
int[] to = {R.id.item_nom, R.id.item_victories, R.id.item_derrotes, R.id.item_empats, R.id.item_punts};
View rootview = inflater.inflate(R.layout.fragment_classificacio, container, false);
ListView listView = (ListView) rootview.findViewById(R.id.classificacio);
listView.invalidateViews();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(rootview.getContext(), R.layout.equip_row,
DBHandler.getDbInstance(getContext()).cursorClassificacio(), from, to);
listView.setAdapter(adapter);
String size = Integer.toString(DBHandler.getDbInstance(getContext()).getAllEquips().size());
Toast toast = Toast.makeText(getContext(), size, Toast.LENGTH_LONG);
toast.show();
return rootview;
}


fragment XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="idi.francesc.footballleague.ClassificacioFragment">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:id="@+id/header_classificacio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/header_equip"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="28dp"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/header_victories"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/header_d"
android:layout_marginEnd="30dp"
/>

<TextView
android:id="@+id/header_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/header_derrotes"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/header_e"
android:layout_marginEnd="27dp"
/>

<TextView
android:id="@+id/header_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/header_empats"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/header_p"
android:layout_marginEnd="38dp"
/>

<TextView
android:id="@+id/header_p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/header_punts"
android:textSize="18sp"
android:layout_marginEnd="12dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>

<ListView
android:id="@+id/classificacio"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header_classificacio">
</ListView>
</RelativeLayout>


</FrameLayout>


Listview Row:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/item_nom"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:textSize="20sp"
android:padding="2dp"
android:layout_marginStart="10dp"
android:text="F.C. Barcelona"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/item_victories"
android:padding="2dp"
android:textSize="15sp"
android:text="10"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/item_derrotes"
android:layout_marginEnd="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/item_derrotes"
android:padding="2dp"
android:textSize="15sp"
android:text="20"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/item_empats"
android:layout_marginEnd="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="@+id/item_empats"
android:padding="2dp"
android:textSize="15sp"
android:text="30"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/item_punts"
android:layout_marginEnd="25dp"/>

<TextView
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="wrap_content"
android:id="@+id/item_punts"
android:padding="6dp"
android:textSize="18sp"
android:text="40"
android:gravity="center_vertical"
android:layout_marginEnd="2dp"
android:layout_alignParentEnd="true"/>


</RelativeLayout>


And this is the result:
enter image description here

thanks.

Answer

You can simplify your ListView Row to use LinearLayout with weight parameters. It's more efficient and prevents issues with TextView's floating width.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="9">
    <TextView
        android:id="@+id/item_nom"
        android:layout_width="0dp"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:padding="2dp"
        android:layout_marginStart="10dp"
        android:layout_weight="4"
        android:text="F.C. Barcelona"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_weight="1"
        android:id="@+id/item_victories"
        android:textSize="15sp"
        android:text="10"
        android:gravity="center"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_weight="1"
        android:id="@+id/item_derrotes"
        android:textSize="15sp"
        android:text="20"
        android:gravity="center"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_weight="1"
        android:id="@+id/item_empats"
        android:textSize="15sp"
        android:text="30"
        android:gravity="center"/>

    <TextView
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:id="@+id/item_punts"
        android:textSize="18sp"
        android:text="40"
        android:gravity="center"/>


</LinearLayout>
Comments