Home » Android » Spinner not working properly in android

Spinner not working properly in android

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am having problem with the spinner dropdown when the value of first spinner has inserted into second spinner. The value have been stored in sqlite db. What I want to do is two button spinner come out with different values from db. Im not sure how the value can be inserted into second spinner. Already tried to create a new method for second spinner in databasehelper but still not working.

1) DatabaseHelper
package com.development.user.onebajacrops;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 22/10/2017.
 */

public class DatabaseHelper2 extends SQLiteOpenHelper {


    static final String DATABASE_NAME = "tasks.db";
    static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "tasks";
    public static final String DATABASE_CREATE= "create table tasks" + "( " + "id" + " integer primary key autoincrement, " + " poption TEXT NULL UNIQUE, " + " environ TEXT NULL UNIQUE ); ";
    public static final String DELETE_TABLE = "DROP TABLE IF EXITS" + TABLE_NAME;
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_OPTION = "poption";
    private static final String  COLUMN_ENVIRON = "environ";


    //Constructor
    public DatabaseHelper2 (Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    @Override
    public void onCreate(SQLiteDatabase db1) {
        db1.execSQL(DATABASE_CREATE);

    }


    @Override
    public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) {

        //Drop older table if existed
        db1.execSQL(DELETE_TABLE);
        //Create tables again
        onCreate(db1);

    }


    public void insertValue(String poption) {
        ContentValues values = new ContentValues();
        // Open the database for writing
        SQLiteDatabase db1 = this.getWritableDatabase();
        // Start the transaction.
        db1.beginTransaction();

        try

        {
            values = new ContentValues();
            values.put("poption", poption);
            // Insert Row
            db1.insert(TABLE_NAME, null, values);
            // Insert into database successfully.
            db1.setTransactionSuccessful();


        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            {
                db1.endTransaction();
                // End the transaction.
                db1.close();
                // Close database
            }
        }
    }


    public void insertValue1(String environ) {
        ContentValues values1 = new ContentValues();
        // Open the database for writing
        SQLiteDatabase db1 = this.getWritableDatabase();
        // Start the transaction.
        db1.beginTransaction();

        try

        {
            values1 = new ContentValues();
            values1.put("environ", environ);
            // Insert Row
            db1.insert(TABLE_NAME, null, values1);
            // Insert into database successfully.
            db1.setTransactionSuccessful();


        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            {
                db1.endTransaction();
                // End the transaction.
                db1.close();
                // Close database
            }
        }
    }





    public ArrayList<String> getAllValues(){

        ArrayList<String> list=new ArrayList<String>();
        // Open the database for reading
        SQLiteDatabase db1 = this.getReadableDatabase();
        // Start the transaction.
        db1.beginTransaction();


        try
        {

            String selectQuery = "SELECT * FROM "+ TABLE_NAME;
            Cursor cursor = db1.rawQuery(selectQuery, null);
            if(cursor.getCount() >0)

            {
                while (cursor.moveToNext()) {
                    // Add province name to arraylist
                    String poption= cursor.getString(cursor.getColumnIndex("poption"));
                    list.add(poption);



                }


            }
            db1.setTransactionSuccessful();

        }
        catch (SQLiteException e)
        {
            e.printStackTrace();

        }
        finally
        {
            db1.endTransaction();
            // End the transaction.
            db1.close();

            // Close database
        }
        return list;


    }


   public List<String> getAllInfo(){

        List<String> list1=new ArrayList<String>();
        // Open the database for reading
        SQLiteDatabase db1 = this.getReadableDatabase();
        // Start the transaction.
        db1.beginTransaction();


        try
        {

            String selectQuery = "SELECT * FROM "+ TABLE_NAME;
            Cursor cursor = db1.rawQuery(selectQuery, null);
            if(cursor.getCount() > 1)

            {
                while (cursor.moveToNext()) {
                    // Add province name to arraylist
                    String environ= cursor.getString(cursor.getColumnIndex("environ"));
                    list1.add(environ);



                }


            }
            db1.setTransactionSuccessful();

        }
        catch (SQLiteException e)
        {
            e.printStackTrace();

        }
        finally
        {
            db1.endTransaction();
            // End the transaction.
            db1.close();

            // Close database
        }
        return list1;


    }
}

2)Java class:
package com.development.user.onebajacrops;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

public class TaskScreenActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
        // Create DataHelper object and insert some sample data
        DatabaseHelper2 databaseHelper1 = new DatabaseHelper2(this);
        databaseHelper1.insertValue("Vegetables");
        databaseHelper1.insertValue("Flowers");
       databaseHelper1.insertValue1("Indoor");
        databaseHelper1.insertValue1("Container");
        databaseHelper1.insertValue1("Outdoor");
        // Get sample data from the database and display them in the spinner
        ArrayList<String> list = databaseHelper1.getAllValues();
        List<String> list1 = databaseHelper1.getAllInfo();
        Spinner sp = (Spinner)findViewById(R.id.spinnerPlant);
        Spinner sp1 = (Spinner)findViewById(R.id.spinnerEnvi);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txt, list);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txt, list1);
        sp.setAdapter(adapter);
       sp1.setAdapter(adapter1);
    }

}
Answers: