Android gallery text only

I looked at the API demos and they have an example gallery where they show only text, the code just uses the cursor, however I want to use a String array. How can I achieve using it? Here is the code for an example in the Demo API:

        Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
    startManagingCursor(c);

    SpinnerAdapter adapter = new SimpleCursorAdapter(this,
    // Use a template that displays a text view
            android.R.layout.simple_gallery_item,
            // Give the cursor to the list adatper
            c,
            // Map the NAME column in the people database to...
            new String[] {People.NAME},
            // The "text1" view defined in the XML template
            new int[] { android.R.id.text1 });
+5
source share
2 answers

If you just want to display a static list of objects String, you should do: ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});

However, if you want to be able to add more objects Stringto the list later, you should use List.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());
+5
source

You need to replace CursorAdapterwith ArrayAdapter.

+4
source

Source: https://habr.com/ru/post/1722241/


All Articles