Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to save Data items in spinner

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 324
    Comment on it

    In the below  code I have created a spinner, when you add your name or text in edit text box and press Add Button your text data will be save on Spinner. Here first I have added a EditText, Button and Spinner with in activity_main.xml layout. In next step I have created a new spinner _item layout here I have added a TextView.  After this in Programming area , Here I have created a Databasehelper class and extended with SQLiteOpenHelper . In forth step i have created SQLController class here I have used insertData method , In MainActivity I have used loadtospinner() and OnClickListener() method, In MainActivity  I have created a MyAsync private  class and this class is extended with AsynTask. You can see below example code it clearly describe you "How to save Data items in spinner “.

     

     

    Step(1)-actvity_main.xml layout-
     

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="30dp" >
    
        <EditText
            android:id="@+id/et_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
    
            <requestFocus />
        </EditText>
    
        <Button
            android:id="@+id/addbtn_id"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Add Member" />
    
        <Spinner
            android:id="@+id/spinner_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>


    Step(2)-Created a spinner_item layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:textColor="#000000"
            android:textSize="20dp" />
    
    </LinearLayout>

     

    Step(3)- I have created DatabaseHelper class-

    public class DatabaseHelper extends SQLiteOpenHelper {
    
        // TABLE INFORMATTION
        public static final String TABLE_MEMBER = "member";
        public static final String MEMBER_ID = "_id";
        public static final String MEMBER_NAME = "name";
    
        // DATABASE INFORMATION
        static final String DB_NAME = "MEMBER.DB";
        static final int DB_VERSION = 1;
    
        // TABLE CREATION STATEMENT
        private static final String CREATE_TABLE = "create table " + TABLE_MEMBER
                + "(" + MEMBER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + MEMBER_NAME + " TEXT NOT NULL);";
    
        public DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
            onCreate(db);
        }
    }


    Step(4)-I have created a new SQLController class -

    public class SQLController {
        private DatabaseHelper dbhelper;
        private Context ourcontext;
        private SQLiteDatabase database;
    
        public SQLController(Context c) {
            ourcontext = c;
        }
    
        public SQLController open() throws SQLException {
            dbhelper = new DatabaseHelper(ourcontext);
            database = dbhelper.getWritableDatabase();
            return this;
    
        }
    
        public void close() {
            dbhelper.close();
        }
    
        public void insertData(String name) {
    
            ContentValues cv = new ContentValues();
            cv.put(DatabaseHelper.MEMBER_NAME, name);
            database.insert(DatabaseHelper.TABLE_MEMBER, null, cv);
        }
    
    
        public Cursor readData() {
            String[] allColumns = new String[] { DatabaseHelper.MEMBER_ID, DatabaseHelper.MEMBER_NAME };
            Cursor c = database.query(DatabaseHelper.TABLE_MEMBER, allColumns, null, null, null, null, null);
            if (c != null) {
                c.moveToFirst();
            }
            return c;
        }
    
    }

    Step(5)-MainActivity-
     

    public class MainActivity extends AppCompatActivity {
    
    
            Button AddBtn;
            EditText et;
            Spinner spn;
    
            SQLController SQLcon;
            ProgressDialog PD;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                AddBtn = (Button) findViewById(R.id.addbtn_id);
                et = (EditText) findViewById(R.id.et_id);
                spn = (Spinner) findViewById(R.id.spinner_id);
    
                SQLcon = new SQLController(this);
                // opening database
                SQLcon.open();
    
                loadtospinner();
    
                AddBtn.setOnClickListener(new View.OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
    
                        new MyAsync().execute();
    
                    }
                });
            }
    
    
            public void loadtospinner() {
    
                Cursor c = SQLcon.readData();
                final ArrayList<String> al = new ArrayList<String>();
    
                c.moveToFirst();
                while (!c.isAfterLast()) {
    
                    String name = c.getString(c.getColumnIndex(DatabaseHelper.MEMBER_NAME));
                    al.add(name);
                    c.moveToNext();
                }
    
                ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
                        getApplicationContext(), R.layout.spinner_item, R.id.textView1,
                        al);
    
                spn.setAdapter(aa1);
    
    
                // closing database
                SQLcon.close();
    
            }
    
            private class MyAsync extends AsyncTask<Void, Void, Void> {
    
                @Override
                protected void onPreExecute() {
    
                    super.onPreExecute();
                    PD = new ProgressDialog(MainActivity.this);
                    PD.setTitle("Please Wait..");
                    PD.setMessage("Loading...");
                    PD.setCancelable(false);
                    PD.show();
                }
    
                @Override
                protected Void doInBackground(Void... params) {
                    String name = et.getText().toString();
                    // opening database
                    SQLcon.open();
                    // insert data into table
                    SQLcon.insertData(name);
    
    
                    return null;
                }
    
                @Override
                protected void onPostExecute(Void result) {
                    super.onPostExecute(result);
                    loadtospinner();
                    PD.dismiss();
                }
    
    
            }
    
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: