0

I've a ListView with some items that users can select.

I want that the first element appears selected, and after 1 or 2 seconds, the selected item will be the second automatically.

How can I do this?

When a item is selected, it can has a bold text for example.

I have a custom adapter for the ListView.


Update:

listview.setSelection(1);
System.out.println(listview.getSelectedItemPosition());

I've tested the code with this println, but it returns "-1". Not selects any row.

adlagar
  • 877
  • 10
  • 31

5 Answers5

1

For a great tutorial on ListView see this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html

Back to the original question to select an item use the following:

 ListView.setSelection(int); 

Calling will simply change the UI without direct user input.

For the delay, you can use the following snippet:

final Handler handler = new Handler(Looper.getMainLooper());

handler.postDelayed(new Runnable() {
   @Override
   public void run() {
        ListView.setSelection(int); 
   }
}, 100);

For information on how change the Android ListView background color of selected item see this post.

Community
  • 1
  • 1
jodm
  • 2,607
  • 3
  • 25
  • 40
  • Note, Handler should be created on the UI thread as you modify UI element in the runnable. If you are not on the main thread, you could create the handler with `new Handler(Looper.getMainLooper());` – Nicolas Dusart Feb 04 '15 at 15:20
  • This does not answer the question, adri wants this to repeat. –  Feb 04 '15 at 15:42
0

If you are asking to iterate through the list and change it's selector you can use a handler like this:

private Handler handler = new Handler();

private Runnable runnable = new Runnable() {
    @Override public void run() {
        int currentSelectedIndex = mListView.getSelectedItemPosition();
        if ((currentSelectedIndex + 1) < listCount) {
            mListView.setSelection(currentSelectedIndex + 1);
        } else {
            mListView.setSelection(0);
        }
        handler.postDelayed(runnable, 1000);
    }
};

And then in your code, in onCreate() for example call:

handler.postDelayed(runnable, 1000);

This will change the list selection every second as you would like.

If you do want to stop the automatic list selector, call this:

handler.removeCallbacks(runnable);
Sam Bains
  • 548
  • 2
  • 8
0

It's considered bad UX to automatically change the UI without direct user input, but if you want to select an item in a ListView programatically, you can call

ListView.setSelection(int);

Sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. If the specified selection position is less than 0, then the item at position 0 will be selected.

For the delay, you will want to place this inside a Handler.

public class ListLooper {

    private LoopHandler mHandler;
    private ListView mListView;

    public ListLooper(Activity activity) {
        mListView = new ListView(activity);
        mHandler = new LoopHandler(mListView);
    }

    private void start() {
        mHandler.sendEmptyMessageDelayed(LoopHandler.MSG_LOOP, LoopHandler.DELAY);
    }

    private void stop() {
        mHandler.removeMessages(LoopHandler.MSG_LOOP);
    }

    private static class LoopHandler extends Handler {

        private static final int MSG_LOOP = 1;
        private static final int DELAY = 2000;

        /**
         * Use a WeakReference so we don't keep an implicit reference to the Activity
         */
        private WeakReference<ListView> mListRef;
        private int mPosition;

        private LoopHandler(ListView list) {
            mListRef = new WeakReference<>(list);
        }

        @Override public void handleMessage(Message msg) {
            super.handleMessage(msg);

            // Check if we still have a reference to the ListView, and the Activity/Fragment hasn't been destroyed
            if (mListRef.get() == null) {
                return;
            }

            // If we're looping, run this code
            if (msg.what == MSG_LOOP) {
                int count = mListRef.get().getAdapter().getCount();

                mListRef.get().setSelection(mPosition);

                // If the position is less than the count, increment it, otherwise set it to 0
                if (mPosition < count - 1) {
                    mPosition++;
                } else {
                    mPosition = 0;
                }

                // Send the same message again, so we repeat this process
                sendEmptyMessageDelayed(MSG_LOOP, DELAY);
            }
    }
}
0

Thanks so much!

This instruction:

listview.setSelection(int)

does not work in my code.

When I select some item, the background colour turns blue. That's correct. But when the activity is just loaded, there is not item selected automatically.

adlagar
  • 877
  • 10
  • 31
0

I use performItemClick and the item is selected, highlighted and the getSelectedItem method will return the correct value:

ListView1.performItemClick(ListView1.getChildAt(1),1,ListView1.getItemIdAtPosition(1));

where 1 = position in the list

schnatterer
  • 7,525
  • 7
  • 61
  • 80
John Callahan
  • 171
  • 1
  • 2