1

I have a Spinner I'm filling with a ArrayAdapter objects, but i need that first position of this Array Always be null or appear something like "Select an object". I searched here Forums but without success, solutions for ArrayList String or spinner.setPrompt that did not work =/

ArrayList<Object> objects = null;

objects= findMyObjects();

final ArrayAdapter<Object> adapterObjects = new ArrayAdapter<Object>(contexto, R.layout.spinner_item, objects);
mySpinner = (Spinner) viewPai.findViewById(R.id.s_spinner);
mySpinner.setAdapter(adapterObjects);
adapterObjects.notifyDataSetChanged();
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });



public ArrayList<Object> findMyObjects() {
        allObjects = new ArrayList<Object>();
        Cursor cursor;

        String sql = "SELECT * FROM Object; ";
        cursor = database.rawQuery(sql, null);

        if (cursor.getCount() >= 0) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                Objects object = new Objects();
                object.setId(cursor.getLong(0));
                allObjects.add(object);
                cursor.moveToNext();
            }
        }
        cursor.close();
        return allObjects;
    }
Raphael MM
  • 103
  • 8

2 Answers2

2

You can try this example. link : http://android--code.blogspot.in/2015/08/android-spinner-hint.html

Sharad Gautam
  • 91
  • 5
  • 11
  • It don't work because he it creates a static string list and I use a list of dynamic objects. – Raphael MM Apr 29 '16 at 14:33
  • you can explicitly add a new String object in ArrayAdapter allObjects.add(new String("Select an Object ")) if (cursor.getCount() >= 0) { cursor.moveToFirst(); while (!cursor.isAfterLast()) { Objects object = new Objects(); object.setId(cursor.getLong(0)); allObjects.add(object); cursor.moveToNext(); } } after that you have to check that value is "Select an Object" or by instance operator – Sharad Gautam Apr 29 '16 at 14:39
  • Generates an error in AndroidStudio because my ArrayAdapter allObjects is type of Object and do not String and i need object – Raphael MM Apr 29 '16 at 14:49
0

Quick

Make a dummy object then when put in the adapter displays = "Select an object"

Bit longer

Extend the arrayadapter or the view in which you are placing your values so that the first object is always a text saying "Select an object".

namlik
  • 182
  • 1
  • 12
  • My object has only id, would not have to put a string just an id 0, but this solution would not be good. I would like to leave the field empty or add the string in the first position. How do I do that? – Raphael MM Apr 29 '16 at 14:07
  • http://stackoverflow.com/a/25438974/6270761 This explains what i said a bit better, as for how you would be doing it, casting your id's to String and put them in a ArrayAdapter. But i'm afraid i dont know a clean solution that looks like adapter.setPrompt("Select one of the following"); – namlik Apr 29 '16 at 14:14
  • I try but don't work because he use ArrayAdapter String and i use ArrayAdapter Object. String is so simple but i need the object. =/ – Raphael MM Apr 29 '16 at 14:30
  • Look at the other answer completely forgot hint existed, that should work alot better. – namlik Apr 29 '16 at 14:31