Valentin
Valentin

Reputation: 280

Android - Java ListView Item Order

im currently learning to programm apps for Android. So im trying to programm a little app, that allows u to add and delete items to a ListView.

It currently looks like this:

I want the items in the ListView to stay at the top, like this

Im sorry for my English, thats why i tried to express it with the pictures. I hope u understood what my problem is.

Thats what the Java Code looks like at the moment, not much, no Listener added yet. Im just trying to find out how to make it look better at the moment:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lvItems = (ListView)  findViewById(R.id.listView);
    items = new ArrayList<String>();
    items.add("First Item");
    items.add("Second Item");
    itemsAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items );
    lvItems.setAdapter(itemsAdapter);

}

and this is the XML Code at the moment:

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Item"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignTop="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dip"
        android:layout_toStartOf="@+id/button"
        android:layout_marginTop="10dip"
        android:layout_marginRight="5dip"
        android:text="Enter a new Item" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/button"
        android:gravity="top"
     />
</RelativeLayout>

Upvotes: 0

Views: 572

Answers (2)

Erdem K&#246;şk
Erdem K&#246;şk

Reputation: 190

You can use Collections.reverse(yourarraylist);

For this approach you can change list order

Upvotes: 0

Gaskoin
Gaskoin

Reputation: 2485

You can just add

android:layout_alignParentTop="true"

To your listView.

Upvotes: 1

Related Questions