Hello. Quite green in this topic, started only recently. Most of the application is based on the course Udacity - Android Basics: Data Storage. Therefore, most of the names of the form pet, gender, etc (the originals are left for the purpose of analyzing what and where works with the original comments).

The application allows you to add \ edit acts, and display them in the list. Data is loaded and retrieved from a SQLite database using a contract, content_provider, etc.

Now the task is to create a spinner like: "Option1", "Option2", "Own option". When selecting the option “Your option”, the user enters his value into the text field, which is then loaded into the database, and is loaded back when entering the Act editing mode. I know how to create a spinner with a ready-made data set, but I still cannot understand how to create a "hybrid version". I apologize if the question is too noob.

Activity code where the act is created. In the code there is an ordinary spinner, over the second I think:

public class CreateActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{ private static final int EXISTING_PET_LOADER = 0; private Uri mCurrentPetUri; private EditText mNameEditText; private EditText mBreedEditText; private EditText mWeightEditText; private Spinner mGenderSpinner; private Spinner mMNspinner; private EditText mMN2EditText; private int mGender = PetEntry.GENDER_UNKNOWN; private int mMN_M = PetEntry.M_1; private boolean mPetHasChanged = false; private View.OnTouchListener mTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { mPetHasChanged = true; return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create); Intent intent = getIntent(); mCurrentPetUri = intent.getData(); if (mCurrentPetUri == null) { setTitle(getString(R.string.editor_activity_title_new_pet)); invalidateOptionsMenu(); } else { setTitle(getString(R.string.editor_activity_title_edit_pet)); getLoaderManager().initLoader(EXISTING_PET_LOADER, null, this); } mNameEditText = (EditText) findViewById(R.id.edit_pet_name); mBreedEditText = (EditText) findViewById(R.id.edit_pet_breed); mWeightEditText = (EditText) findViewById(R.id.edit_pet_weight); mGenderSpinner = (Spinner) findViewById(R.id.spinner_gender); mMNspinner = (Spinner) findViewById(R.id.spinner); mMN2EditText = (EditText) findViewById(R.id.editTextMN); mNameEditText.setOnTouchListener(mTouchListener); mBreedEditText.setOnTouchListener(mTouchListener); mWeightEditText.setOnTouchListener(mTouchListener); mGenderSpinner.setOnTouchListener(mTouchListener); mMNspinner.setOnTouchListener(mTouchListener); setupSpinner(); //setupSpinner2(); } private void setupSpinner() { ArrayAdapter genderSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_gender_options, android.R.layout.simple_spinner_item); genderSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); mGenderSpinner.setAdapter(genderSpinnerAdapter); mGenderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String selection = (String) parent.getItemAtPosition(position); if (!TextUtils.isEmpty(selection)) { if (selection.equals(getString(R.string.gender_male))) { mGender = PetEntry.GENDER_MALE; } else if (selection.equals(getString(R.string.gender_female))) { mGender = PetEntry.GENDER_FEMALE; } else if (selection.equals(getString(R.string.gender_femalele))) { mGender = PetEntry.GENDER_FEMALELE; } else { mGender = PetEntry.GENDER_UNKNOWN; } } } @Override public void onNothingSelected(AdapterView<?> parent) { mGender = PetEntry.GENDER_UNKNOWN; } }); } private void setupSpinner2() { ArrayAdapter genderSpinnerAdapter2 = ArrayAdapter.createFromResource(this, R.array.array_test, android.R.layout.simple_spinner_item); genderSpinnerAdapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); mMNspinner.setAdapter(genderSpinnerAdapter2); mMNspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String selection = (String) parent.getItemAtPosition(position); if (!TextUtils.isEmpty(selection)) { if (selection.equals(getString(R.string.good))) { mMN_M = PetEntry.M_1; } else if (selection.equals(getString(R.string.tak_sebe))) { mMN_M = PetEntry.M_2; } else if (selection.equals(getString(R.string.hrenovo))) { mMN_M = PetEntry.M_3; } else { // Поле со "Своим вариантом", пока просто длявида т.к. тип данных int mMN_M = PetEntry.M_4; } } } @Override public void onNothingSelected(AdapterView<?> parent) { mMN_M = PetEntry.M_4; } }); } private void savePet() { String nameString = mNameEditText.getText().toString().trim(); String breedString = mBreedEditText.getText().toString().trim(); String weightString = mWeightEditText.getText().toString().trim(); if (mCurrentPetUri == null && TextUtils.isEmpty(nameString) && TextUtils.isEmpty(breedString) && TextUtils.isEmpty(weightString) && mGender == PetEntry.GENDER_UNKNOWN && mMN_M == PetEntry.M_1) { return; } ContentValues values = new ContentValues(); values.put(PetEntry.COLUMN_PET_NAME, nameString); values.put(PetEntry.COLUMN_PET_BREED, breedString); values.put(PetEntry.COLUMN_PET_GENDER, mGender); values.put(PetEntry.COLUMN_MN, mMN_M); int weight = 0; if (!TextUtils.isEmpty(weightString)) { weight = Integer.parseInt(weightString); } values.put(PetEntry.COLUMN_PET_WEIGHT, weight); if (mCurrentPetUri == null) { Uri newUri = getContentResolver().insert(PetEntry.CONTENT_URI, values); if (newUri == null) { Toast.makeText(this, getString(R.string.editor_insert_pet_failed), Toast.LENGTH_SHORT).show(); } else { // Otherwise, the insertion was successful and we can display a toast. Toast.makeText(this, getString(R.string.editor_insert_pet_successful), Toast.LENGTH_SHORT).show(); } } else { int rowsAffected = getContentResolver().update(mCurrentPetUri, values, null, null); if (rowsAffected == 0) { Toast.makeText(this, getString(R.string.editor_update_pet_failed), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, getString(R.string.editor_update_pet_successful), Toast.LENGTH_SHORT).show(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_create, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); if (mCurrentPetUri == null) { MenuItem menuItem = menu.findItem(R.id.action_delete); menuItem.setVisible(false); } return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_save: savePet(); finish(); return true; case R.id.action_delete: showDeleteConfirmationDialog(); return true; case android.R.id.home: if (!mPetHasChanged) { NavUtils.navigateUpFromSameTask(CreateActivity.this); return true; } DialogInterface.OnClickListener discardButtonClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { NavUtils.navigateUpFromSameTask(CreateActivity.this); } }; showUnsavedChangesDialog(discardButtonClickListener); return true; } return super.onOptionsItemSelected(item); } @Override public void onBackPressed() { if (!mPetHasChanged) { super.onBackPressed(); return; } DialogInterface.OnClickListener discardButtonClickListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }; showUnsavedChangesDialog(discardButtonClickListener); } @Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { String[] projection = { PetEntry._ID, PetEntry.COLUMN_PET_NAME, PetEntry.COLUMN_PET_BREED, PetEntry.COLUMN_PET_GENDER, PetEntry.COLUMN_PET_WEIGHT, PetEntry.COLUMN_MN }; return new CursorLoader(this, // Parent activity context mCurrentPetUri, // Query the content URI for the current pet projection, // Columns to include in the resulting Cursor null, // No selection clause null, // No selection arguments null); // Default sort order } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { if (cursor == null || cursor.getCount() < 1) { return; } if (cursor.moveToFirst()) { int nameColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_NAME); int breedColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_BREED); int genderColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_GENDER); int spinnerColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_MN); int weightColumnIndex = cursor.getColumnIndex(PetEntry.COLUMN_PET_WEIGHT); String name = cursor.getString(nameColumnIndex); String breed = cursor.getString(breedColumnIndex); int gender = cursor.getInt(genderColumnIndex); int spinner = cursor.getInt(spinnerColumnIndex); int weight = cursor.getInt(weightColumnIndex); mNameEditText.setText(name); mBreedEditText.setText(breed); mWeightEditText.setText(Integer.toString(weight)); switch (gender) { case PetEntry.GENDER_MALE: mGenderSpinner.setSelection(1); break; case PetEntry.GENDER_FEMALE: mGenderSpinner.setSelection(2); break; case PetEntry.GENDER_FEMALELE: mGenderSpinner.setSelection(3); break; default: mGenderSpinner.setSelection(0); break; } switch (spinner) { case PetEntry.M_1: mMNspinner.setSelection(0); break; case PetEntry.M_2: mMNspinner.setSelection(1); break; case PetEntry.M_3: mMNspinner.setSelection(2); break; default: mMNspinner.setSelection(3); break; } } } @Override public void onLoaderReset(Loader<Cursor> loader) { mNameEditText.setText(""); mBreedEditText.setText(""); mWeightEditText.setText(""); mGenderSpinner.setSelection(0); mMNspinner.setSelection(0); } private void showUnsavedChangesDialog( DialogInterface.OnClickListener discardButtonClickListener) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.unsaved_changes_dialog_msg); builder.setPositiveButton(R.string.discard, discardButtonClickListener); builder.setNegativeButton(R.string.keep_editing, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (dialog != null) { dialog.dismiss(); } } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } private void showDeleteConfirmationDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.delete_dialog_msg); builder.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { deletePet(); } }); builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { if (dialog != null) { dialog.dismiss(); } } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } private void deletePet() { if (mCurrentPetUri != null) { int rowsDeleted = getContentResolver().delete(mCurrentPetUri, null, null); if (rowsDeleted == 0) { Toast.makeText(this, getString(R.string.editor_delete_pet_failed), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, getString(R.string.editor_delete_pet_successful), Toast.LENGTH_SHORT).show(); } } // Close the activity finish(); } } 

Something like trying to do:

enter image description here

  • You do not ask a question, you ask for help to solve a voluminous problem, the solution of which will require a large amount of code and many paragraphs of text. Try to break your task into small subtasks and solve each of them separately. Then maybe the questions will disappear. And so hardly anyone will help you - P. Ilyin
  • @ P.Ilyin I apologize for what happened. From the standpoint of a beginner, it seemed to me that the solution lies in the correct setup of the spinner and the addition of a couple of lines in the following code. Now I understand that everything is not so simple. It seems to me that, until now, the collective farm option has come up - the creation of a separate column for "their options" and conditions in the code. But so far, unfortunately, there is not enough knowledge to implement. - Denis V

0