2

every time i change my ListView in Activity it jumps to first

I use this code at the first to set the listView

listView.setAdapter(new ArrayAdapter<String>(this, 
                    R.layout.home_row, R.id.home_row_price, items));

then I want to add some more arrays to list view so i use

 listView.setAdapter(new ArrayAdapter<String>(this, 
                    R.layout.home_row, R.id.home_row_price, items));

again with new String[] items

but every time it jumps to first of the listView what can I do ?

Harshavardhan Konakanchi
  • 4,238
  • 6
  • 36
  • 54
amir
  • 145
  • 1
  • 9

4 Answers4

3

First of all use an ArrayList instead of a String array for storing data. ArrayList can be dynamically changed(i.e they can change size dynamically). Instead of setting an anonymuos adapter to your listview, do it like this :

adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,arrayList);
lv=(ListView)findViewById(R.id.listView);
lv.setAdapter(adapter);

Here i have used an ArrayList for setting data to listview. And when you are adding new item to your listview do it like this:

arrayList.add(yourData);
...
adapter.notifyDataSetChanged();

The adapter.notifyDataSetChanged() method will refresh the listview without jumping to the first item in the listview.

The Problem in your code is that everytime you are changing the data, you are setting a new adapter to your ListView. Hence the ListView is "reset" instead of "refreshing".

Akhil Soman
  • 2,077
  • 2
  • 17
  • 30
1

The jumping to the first element is normal behavior of the ListView. To identify items to scroll to instead of to the start, it uses the id of the elements. But this only works if hasStableIds() returns true.

ArrayAdapter uses the position as id, so maybe just what you're looking for. But it returns false from hasStableIds(). You could make it work with a custom subclass of it.

public class StableArrayAdapter<T> extends ArrayAdapter<T> {
    public StableArrayAdapter(Context ctx, int res, int txt, T[] obj) {
        super(ctx, res, txt, obj);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}
tynn
  • 38,113
  • 8
  • 108
  • 143
0

You can use change String[] to

List<String> items= new LinkedList<String>();
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

You not need to jump at first everytime. In listview when you want to set adapter, at that time only you need to move first, then you can add, update or delete items from list without moving to first.

For this follow below steps.

(-) First Array of items should be declare at top, means outside of methods.
(-) Also declare instance of adapter also at top, as like
         String[] items;
         ArrayAdapter<> dataAdapter;

(-) Now at first assign values to items and set as adapter.

     like, 
      dataAdapter = new ArrayAdapter(this, R.layout.home_row, R.id.home_row_price, items)
      listView.setAdapter(dataAdapter);

(-) Now, when ever and in any method you want to add, update or delete items from array, perform that operation.

(-) And then, **most important** just write down below line after editing array.

     dataAdapter.notifyDataSetChanged();

So, now whenever you made changes in items array just call above line after it and your listview will update as per changes.

Hope this will work

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34