1. Are you ready for the Galaxy S20? Here is everything we know so far!

Can't find example - Use spinner to populate Textfield

Discussion in 'Android Lounge' started by GabeTheGreat73, Apr 18, 2020.

  1. GabeTheGreat73

    Thread Starter

    Hello All,

    I've been playing around with studio, so i'm a complete beginner. I found a youtube video about SQLite and was able to follow along to create a basic CRUD activity using SQLite. Now I put a new activity with a spinner that lists the names from my db fine (took forever to figure that out lol) but I want it to automatically pull in the rest of the selected record's info into textboxes I placed below the spinner.

    For example, my table columns are: Index, Name, Surname, Marks
    In my spinner, it lists all items in the Name column (i.e.: John, Susan, Matther, etc)
    When I click "John", for example, I want it to put the Surname in a text box called "editText_surname" and to put the Marks in a text box called "editText_marks".

    I cant find any examples or videos how to do this... if anyone knows of any, please link so I can learn. Really enjoying learning java basics and playing with android studio... thx in advance!
     


  2. MoodyBlues

    MoodyBlues Compassion is cool!
    VIP Member

    Welcome to Android Forums, GabeTheGreat73! :)

    I suggest that you post on our Android Development board, where other app developers are more likely to see your question.

    When you post there, please be as specific as you can be. For example, you're using AS, but what is its version, and which OS are you on? The more details you provide, the better!

    Good luck with your app!
     
  3. GabeTheGreat73

    Thread Starter

    Hello All,

    I've been playing around with android studio for windows x64 (version 3.6.3) , so i'm a complete beginner. I found a youtube video about SQLite and was able to follow along to create a basic CRUD activity using SQLite. Now I put a new activity with a spinner that lists the names from my database (took forever to figure that out lol) but I want it to automatically pull in the rest of the selected record's information into textboxes I placed below the spinner.

    For example: my table columns are: Index, Name, Surname, Marks
    In my spinner, it lists all items in the NAME column (i.e.: John, Susan, Matther, etc)
    When I click "John", for example, I want it to put the Surname in a text box called "editText_surname" and to put the Marks in a text box called "editText_marks".

    I can't find any examples or videos how to do this... if anyone knows of any, please link so I can learn. Really enjoying learning java basics and playing with android studio...I'm a truck driver and only get 1-2 hours after driving to play with A.S. before I gotta get some sleep, so sorry if I take a day or two to respond, so thx in advance for any help, I appreciate it!

    Here is my DatabaseHelper.java file....

    Code (Java):
    1. package com.example.sqlite;
    2.  
    3. import android.content.ContentValues;
    4. import android.content.Context;
    5. import android.database.Cursor;
    6. import android.database.sqlite.SQLiteDatabase;
    7. import android.database.sqlite.SQLiteOpenHelper;
    8. import androidx.annotation.Nullable;
    9. import java.util.ArrayList;
    10.  
    11. import static android.content.ContentValues.*;
    12.  
    13. public class DatabaseHelper extends SQLiteOpenHelper {
    14.     public static final String DATABASE_NAME = "student.db";
    15.     public static final String TABLE_NAME = "student_table";
    16.     public static final String COL_1 = "ID";
    17.     public static final String COL_2 = "NAME";
    18.     public static final String COL_3 = "SURNAME";
    19.     public static final String COL_4 = "MARKS";
    20.  
    21.     public DatabaseHelper(@Nullable Context context) {
    22.         super(context, DATABASE_NAME, null, 1);
    23.         SQLiteDatabase db = this.getWritableDatabase();
    24.     }
    25.  
    26.     @Override
    27.     public void onCreate(SQLiteDatabase db) {
    28.         db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
    29.     }
    30.  
    31.     @Override
    32.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    33.         db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    34.         onCreate(db);
    35.     }
    36.  
    37.     public boolean insertData(String name,String surname,String marks) {
    38.         SQLiteDatabase db = this.getWritableDatabase();
    39.         ContentValues contentValues = new ContentValues();
    40.         contentValues.put(COL_2,name);
    41.         contentValues.put(COL_3,surname);
    42.         contentValues.put(COL_4,marks);
    43.         long result = db.insert(TABLE_NAME,null ,contentValues);
    44.         if(result == -1) return false;
    45.         else
    46.             return true;
    47.     }
    48.  
    49.     public Cursor getAllData() {
    50.         SQLiteDatabase db = this.getWritableDatabase();
    51.         Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
    52.         return res;
    53.     }
    54.  
    55.     public boolean updateData(String id,String name,String surname,String marks) {
    56.         SQLiteDatabase db = this.getWritableDatabase();
    57.         ContentValues contentValues = new ContentValues();
    58.         contentValues.put(COL_1,id);
    59.         contentValues.put(COL_2,name);
    60.         contentValues.put(COL_3,surname);
    61.         contentValues.put(COL_4,marks);
    62.         db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
    63.         return true;
    64.     }
    65.  
    66.     public Integer deleteData (String id) {
    67.         SQLiteDatabase db = this.getWritableDatabase();
    68.         return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
    69.     }
    70.  
    71.     public ArrayList<String> getAllNames(){
    72.         ArrayList<String> list=new ArrayList<String>();
    73.         SQLiteDatabase db=this.getReadableDatabase();
    74.         db.beginTransaction();
    75.         String selectQuery="SELECT * FROM "+"student_table";
    76.         Cursor cursor=db.rawQuery(selectQuery,null);
    77.         if(cursor.getCount()>0){
    78.             list.add(0,"SELECT NAME");
    79.             while(cursor.moveToNext()){
    80.                 String personname = cursor.getString(cursor.getColumnIndex("NAME"));
    81.                 list.add(personname);
    82.             }
    83.         }
    84.         db.setTransactionSuccessful();
    85.         db.endTransaction();
    86.         db.close();
    87.         return list;
    88.     }
    89. }
    90.  
    91.  

    And this is my spinner test page activity.....

    Code (Java):
    1. package com.example.sqlite;
    2.  
    3. import androidx.appcompat.app.AppCompatActivity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.AdapterView;
    7. import android.widget.ArrayAdapter;
    8. import android.widget.EditText;
    9. import android.widget.Spinner;
    10. import android.widget.Toast;
    11. import java.util.ArrayList;
    12.  
    13. public class SpinnerActivity extends AppCompatActivity {
    14.     DatabaseHelper myDb;
    15.     EditText editName,editSurname,editMarks,editLoadsTemp;
    16.     Spinner spinnerTestname;
    17.  
    18.     @Override
    19.     protected void onCreate(Bundle savedInstanceState) {
    20.         super.onCreate(savedInstanceState);
    21.         setContentView(R.layout.activity_spinner);
    22.  
    23.         myDb = new DatabaseHelper(this);
    24.         Spinner testname=(Spinner)findViewById(R.id.spinner_testname);
    25.      
    26.         ArrayList<String> arrayList1=myDb.getAllNames();
    27.         ArrayAdapter<String> arrayAdapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, arrayList1);
    28.         testname.setAdapter(arrayAdapter1);
    29.         testname.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    30.             @Override
    31.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    32.                 Toast.makeText(SpinnerActivity.this,"You clicked on something!",Toast.LENGTH_LONG).show();
    33.             }
    34.             @Override
    35.             public void onNothingSelected(AdapterView<?> parent) {
    36.             }
    37.         });
    38.  
    39.         editName = findViewById((R.id.editText_name));
    40.         editSurname = findViewById((R.id.editText_surname));
    41.         editMarks = findViewById((R.id.editText_marks));
    42.         spinnerTestname = (Spinner)findViewById(R.id.spinner_testname);
    43.     }
    44. }
    45.  
     
    #3 GabeTheGreat73, Apr 18, 2020
    Last edited: Apr 18, 2020
  4. GabeTheGreat73

    Thread Starter


    Thanks, I re-posted in that forum with more details and the code of my files, but I don't see how I can delete this thread.... I see the option to EDIT , but not delete... How can I do that? or do I just leave it up in this section too?
     
  5. MoodyBlues

    MoodyBlues Compassion is cool!
    VIP Member

    You're welcome. :)
    No worries! Just leave it as is, or you can 'edit' the OP and replace its text with something like, 'reposted on app development board.' But it's fine to just leave it as is.
     
Loading...

Share This Page

Loading...