0

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>
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Valentin
  • 280
  • 5
  • 15
  • Can you post your code? – aQuigs Feb 15 '15 at 21:39
  • You may found solution here [Retaining position in ListView after calling notifyDataSetChanged][1] [1]: http://stackoverflow.com/questions/8276128/retaining-position-in-listview-after-calling-notifydatasetchanged – Thaer Zaghal Feb 15 '15 at 21:45
  • Does you `ListView` take all available space - did you use `android:layout_height="match_parent"`? Anyway, let us see your layout xml so we don't have to guess what's wrong. – theb1uro Feb 15 '15 at 21:47
  • Oh yeah... I just realised how stupid i am for missing this easy fix :O Thx guys :) – Valentin Feb 15 '15 at 21:50

2 Answers2

1

You can just add

android:layout_alignParentTop="true"

To your listView.

Gaskoin
  • 2,469
  • 13
  • 22
0

You can use Collections.reverse(yourarraylist);

For this approach you can change list order

Erdem Köşk
  • 190
  • 1
  • 10