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

Can't find example - Use spinner to populate Textfield

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!
 
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!
 
Upvote 0
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....

Java:
package com.example.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.util.ArrayList;

import static android.content.ContentValues.*;

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "student.db";
    public static final String TABLE_NAME = "student_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "NAME";
    public static final String COL_3 = "SURNAME";
    public static final String COL_4 = "MARKS";

    public DatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String name,String surname,String marks) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,surname);
        contentValues.put(COL_4,marks);
        long result = db.insert(TABLE_NAME,null ,contentValues);
        if(result == -1) return false;
        else
            return true;
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
        return res;
    }

    public boolean updateData(String id,String name,String surname,String marks) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,id);
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,surname);
        contentValues.put(COL_4,marks);
        db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id });
        return true;
    }

    public Integer deleteData (String id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME, "ID = ?",new String[] {id});
    }

    public ArrayList<String> getAllNames(){
        ArrayList<String> list=new ArrayList<String>();
        SQLiteDatabase db=this.getReadableDatabase();
        db.beginTransaction();
        String selectQuery="SELECT * FROM "+"student_table";
        Cursor cursor=db.rawQuery(selectQuery,null);
        if(cursor.getCount()>0){
            list.add(0,"SELECT NAME");
            while(cursor.moveToNext()){
                String personname = cursor.getString(cursor.getColumnIndex("NAME"));
                list.add(personname);
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        return list;
    }
}


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

Java:
package com.example.sqlite;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;

public class SpinnerActivity extends AppCompatActivity {
    DatabaseHelper myDb;
    EditText editName,editSurname,editMarks,editLoadsTemp;
    Spinner spinnerTestname;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);

        myDb = new DatabaseHelper(this);
        Spinner testname=(Spinner)findViewById(R.id.spinner_testname);
      
        ArrayList<String> arrayList1=myDb.getAllNames();
        ArrayAdapter<String> arrayAdapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, arrayList1);
        testname.setAdapter(arrayAdapter1);
        testname.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(SpinnerActivity.this,"You clicked on something!",Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        editName = findViewById((R.id.editText_name));
        editSurname = findViewById((R.id.editText_surname));
        editMarks = findViewById((R.id.editText_marks));
        spinnerTestname = (Spinner)findViewById(R.id.spinner_testname);
    }
}
 
Last edited:
Upvote 0
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!


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?
 
Upvote 0
You're welcome. :)
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?
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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones