• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Add Spinner

LV67

Lurker
I'm testing whith an example code I found on the internet and try to make some changes.

The app, as far as it is now has 5 fields that make a record that is added in SQLite database.

On this moment all the data is added by EditText fields.

Now I like to change the field etKleurwaaier in a Spinner.

I found on the internet a lot of solutions to make a spinner but my Java knowledge isn't enough to fix this by my self. Can someone explain me how to do this.
I added here the code is for as I have :

TableManipulationActivity.java


Java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class TableManipulationActivity extends Activity {

    EditText etLocatie;
    EditText etProduct;
    EditText etKleur;
    EditText etKleurwaaier;
    EditText etInhoud;

    Button btnDML;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table_manipulation);

        getAllWidgets();
        bindWidgetsWithEvent();
        checkForRequest();

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.kleur_arrays, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);

    }

    public class SpinnerActivity extends Activity implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent, View view,
                                   int pos, long id) {
            // An item was selected. You can retrieve the selected item using
            // parent.getItemAtPosition(pos)
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Another interface callback
        }
    }
    
    private void checkForRequest() {
        String request = getIntent().getExtras().get(Constants.DML_TYPE).toString();
        if (request.equals(Constants.UPDATE)) {
            btnDML.setText(Constants.UPDATE);
            etLocatie.setText(getIntent().getExtras().get(Constants.LOCATIE).toString());
            etProduct.setText(getIntent().getExtras().get(Constants.PRODUCT).toString());
            etKleurwaaier.setText(getIntent().getExtras().get(Constants.KLEURWAAIER).toString());
            etKleur.setText(getIntent().getExtras().get(Constants.KLEUR).toString());
            etInhoud.setText(getIntent().getExtras().get(Constants.INHOUD).toString());
        } else {
            btnDML.setText(Constants.INSERT);
        }
    }

    private void bindWidgetsWithEvent() {
        btnDML.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onButtonClick();
            }
        });
    }

    private void getAllWidgets() {
        etLocatie = (EditText) findViewById(R.id.etLocatie);
        etProduct = (EditText) findViewById(R.id.etProduct);
        etKleurwaaier = (EditText) findViewById(R.id.etKleurwaaier);
        etKleur = (EditText) findViewById(R.id.etKleur);
        etInhoud = (EditText) findViewById(R.id.etInhoud);

        btnDML = (Button) findViewById(R.id.btnDML);
    }

    private void onButtonClick() {
        if (etLocatie.getText().toString().equals("") || etProduct.getText().toString().equals("") || etKleurwaaier.getText().toString().equals("") || etKleur.getText().toString().equals("") || etInhoud.getText().toString().equals("")) {
            Toast.makeText(getApplicationContext(), "Add Both Fields", Toast.LENGTH_LONG).show();
        } else {
            Intent intent = new Intent();
            intent.putExtra(Constants.LOCATIE, etLocatie.getText().toString());
            intent.putExtra(Constants.PRODUCT, etProduct.getText().toString());
            intent.putExtra(Constants.KLEURWAAIER, etKleurwaaier.getText().toString());
            intent.putExtra(Constants.KLEUR, etKleur.getText().toString());
            intent.putExtra(Constants.INHOUD, etInhoud.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}

table_manipulation.xml

Java:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutAddUpdate"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/etLocatie"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Locatie"
        android:inputType="textCapWords" />

    <EditText
        android:id="@+id/etProduct"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Product"
        android:inputType="textCapWords" />

    <EditText
        android:id="@+id/etKleurwaaier"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Kleurwaaier"
        android:inputType="textCapWords" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/selVersion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/etKleur"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Kleur"
        android:inputType="textCapWords" />

    <EditText
        android:id="@+id/etInhoud"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Inhoud"
        android:inputType="textCapWords" />

    <com.rey.material.widget.Button
        android:id="@+id/btnDML"
        style="@style/RaiseWaveColorButtonRippleStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/color_blue"
        android:text="Bewaar"
        android:textColor="@color/color_white"
        android:textStyle="bold"
       />

</LinearLayout>

strings.xml

Java:
<resources>
    <string name="app_name">SQLiteDemoAndroid</string>
    <string name="action_settings">Settings</string>

    <string name="kleur_prompt">Kies kleurwaaier</string>

    <string-array name="kleur_arrays">
        <item>Ral</item>
        <item>NCS</item>
        <item>Flamant</item>
        <item>Levis</item>
        <item>Histor</item>
        <item>Boss</item>
        <item>Caparol</item>
        <item>Sikkens</item>
    </string-array>
</resources>
 
Last edited:
To start with, your SpinnerActivity class isn't required.
Your TableManipulationActivity can implement the listener

Code:
public class TableManipulationActivity extends Activity implements OnItemSelectedListener {
 
I changed this in my code but after Run I got following error :

Error:(15, 8) error: TableManipulationActivity is not abstract and does not override abstract method onNothingSelected(AdapterView<?>) in OnItemSelectedListener
 
Any class implementing an interface must implement all the methods of that interface, or extend from a class which does.
In this case, the onItemSelectedListener interface contains two methods, both of which you must implement

https://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html

Btw how familiar are you with the Java programming language? If you don't know what an interface is, then you need to do some reading on the basics. This will save you a lot of trouble in future.
 
Back
Top Bottom