Spinner option doesn't show up

37 Views Asked by At

In my app, I want to show spinner in a list view. Once user select the option in the spinner, the data in SQLite database will be changed.

However, when I write the code for updating the SQLite database, the spinner does't show any option anymore.

This is the code I believe cause the problem:

   context.getContentResolver().update(currentUri, values, null, null);

The CursorAdapter class that include the code:

public class TimeCursorAdaptor extends CursorAdapter {
    public TimeCursorAdaptor(Context context, Cursor c) {
        super(context, c, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.list_item_layout, null, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView listItem_textView_showTime = view.findViewById(R.id.listItem_textView_showTimeRecord);
        Spinner listItem_spinner_punch = view.findViewById(R.id.listItem_spinner_punch);

        int idColumnIndex = cursor.getColumnIndex(TimeEntry._ID);
        int recordColumnIndex = cursor.getColumnIndexOrThrow(TimeEntry.COLUMN_RECORD);
        int punchColumnIndex = cursor.getColumnIndexOrThrow(TimeEntry.COLUMN_PUNCH);

        int recordId = cursor.getInt(idColumnIndex);
        String record = cursor.getString(recordColumnIndex);
        int punch = cursor.getInt(punchColumnIndex);

        Uri currentUri = ContentUris.withAppendedId(TimeEntry.CONTENT_URI, recordId);

        listItem_textView_showTime.setText(record);
        setUpSpinner(context, listItem_spinner_punch, punch, currentUri);
        listItem_spinner_punch.setSelection(punch);
    }

    private void setUpSpinner(final Context context, final Spinner listItem_spinner_punch, final int punch, final Uri currentUri){
        ArrayAdapter punchArrayAdapter = ArrayAdapter.createFromResource(context, R.array.Punch_Option, android.R.layout.simple_spinner_item);
        punchArrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        listItem_spinner_punch.setAdapter(punchArrayAdapter);

        listItem_spinner_punch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selection = (String) parent.getItemAtPosition(position);
                int newPunch = punch;
                if (!TextUtils.isEmpty(selection)){
                    if (selection.equals(context.getString(R.string.punch_in))){
                        newPunch = TimeEntry.PUNCH_IN;
                    }
                    else {
                        newPunch = TimeEntry.PUNCH_OUT;
                    }
                }
                ContentValues values = new ContentValues();
                values.put(TimeEntry.COLUMN_PUNCH, newPunch);

                context.getContentResolver().update(currentUri, values, null, null); //The code I believe cause the problem.
            }

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

            }
        });
    }
}

I wonder if I update the database in wrong place? Can anyone help me?

0

There are 0 best solutions below