Search code examples
androidunicodeandroid-spinnersimplecursoradapter

Need help getting my Android Spinner to correctly populate with images and unicode text that are stored in the SQLite Database


I would like to populate a Spinner with values of images I have stored in the SQLite database, but I am having trouble getting this to work properly.

Here is an image of what is going on:

First, here is what I have in my database:

CREATE TABLE `STATUS` (`_id` INTEGER, `value` TEXT, `image` TEXT);
INSERT INTO "STATUS" VALUES (1, "\u003c", "status_image_01");
INSERT INTO "STATUS" VALUES (2, "\u2248", "status_image_02");
INSERT INTO "STATUS" VALUES (3, "\u003e", "status_image_03");

Here is my layout activity_main.xml:

<LinearLayout>
    <Spinner android:id="@+id/status_spinner" />
</LinearLayout>

Here is my custom row for each line in the spinner line_of_status.xml:

<LinearLayout>
    <ImageView android:id="@+id/status_icon" />
    <TextView android:id="@+id/status_text" />
</LinearLayout>

Here is my activity MainActivity.java:

public class Status extends AppCompatActivity {
    private Spinner statusSpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContenteView(R.layout.status);

        setUpSpinner();
    }

    private void setUpSpinner(){
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        String[] fromColumns = {"image", "value"};
        int[] toViews = {R.id.status_icon, R.id.status_text};
        Cursor statusCursor = dbHelper.getStatusCursor();
        statusSpinner = (Spinner)findViewById(R.id.status_spinner);
        StatusSimpleCursorAdapter statusSimpleCursorAdapter = new StatusSimpleCursorAdapter(this, R.layout.line_of_status, statusCursor, fromColumns, toViews, 0);
        statusSpinner.setAdapter(statusSimpleCursorAdapter);
        dbHelper.close();
    }
}

Here is my custom SimpleCursorAdapter StatusSimpleCursorAdapter.java

public class StatusSimpleCursorAdapter extends SimpleCursorAdapter {
    private Context context;
    private Cursor cursor;

    public StatusSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags){
        super(context, layout, cursor, from, to, flags);
        this.context = context;
        this.cursor = cursor;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View row = super.getView(position, convertView, parent);

        cursor.moveToPosition(position);

        String statusimage = cursor.getString(cursor.getColumnIndex("image"));
        ImageView imageView = (ImageView)row.findViewById(R.id.status_icon);
        imageView.setImageResource(context.getResources().getIdentifier(status_image, "drawable", com.mypackage));

        return row;
    }
}

In my app/src/main/res/drawable directory I have the three images:

    status_image_01.png
    status_image_02.png
    status_image_03.png

Problems:

What I have here almost works. When I select a different status from the spinner, the appropriate image appears; however, it only appears on the final step, after I have selected a status and the drop-down is gone. The list of images will not appear in the drop-down itself, even if I put it into dialog mode.

Also, the unicode characters are not appearing properly. For example, the status "\u003c" will appear as '\u003c' and not '<'. I did a quick test in a new project where I populated a Spinner with strings from an array instead of an SQLite database:

<string-array>
    <item>\u003c</item>
    <item>\u2248</item>
    <item>\u003e</item>
</string-array>

I used an ArrayAdapter and the unicode characters appeared as they should.

So, I need help getting all of the images to populate in the Spinner drop-down list, and I need help getting my unicode characters to display properly.

Thank you



UPDATE:

I fixed my issue with the Unicode characters not showing properly. After some digging, I discovered that the SQLite database was escaping my Unicode strings when inserting them into the database. So, I found this post that showed me how to un-escape the strings, it uses org.apache.commons.lang3.StringEscapeUtils.unescapeJava(str); to accomplish this. So, I added this to my StatusSimpleCursorAdapter.java class:

import static org.apache.commons.lang3.StringEscapeUtils.unescapeJava;

TextView textView = (TextView) row.findViewById(R.id.status_text);
String value = unescapeJava(cursor.getString(cursor.getColumnIndex("value")));

textView.setText(value);

As I said, this fixes the issue of my Unicode not displaying properly, however I still have the one remaining problem ... with a new twist. The images are still not displaying in the drop-down itself, and now the escaped Unicode is also displaying in the drop-down. With the selected item, both the image and un-escaped Unicode display properly, it is just within the drop-down that nothing seems to work.

Any advice?

Thanks.


Solution

  • Ok, after beating my head against the wall for a week now, I finally found an answer that worked for me.

    Here it is.

    This fixed both my image and Unicode issues in my Spinner drop-down.