Click here to Skip to main content
15,921,577 members
Home / Discussions / Java
   

Java

 
GeneralRe: PLEASE HELP JAVA Pin
Member 1477360615-Mar-20 10:11
Member 1477360615-Mar-20 10:11 
GeneralRe: PLEASE HELP JAVA Pin
Richard MacCutchan15-Mar-20 12:26
mveRichard MacCutchan15-Mar-20 12:26 
AnswerRe: PLEASE HELP JAVA Pin
MallardsReach13-Mar-20 3:12
MallardsReach13-Mar-20 3:12 
GeneralRe: PLEASE HELP JAVA Pin
Richard MacCutchan13-Mar-20 4:30
mveRichard MacCutchan13-Mar-20 4:30 
GeneralRe: PLEASE HELP JAVA Pin
MallardsReach13-Mar-20 6:33
MallardsReach13-Mar-20 6:33 
GeneralRe: PLEASE HELP JAVA Pin
Richard MacCutchan13-Mar-20 6:49
mveRichard MacCutchan13-Mar-20 6:49 
GeneralRe: PLEASE HELP JAVA Pin
MallardsReach13-Mar-20 7:15
MallardsReach13-Mar-20 7:15 
QuestionTransfer data from SQL database to populate spinner Pin
Member 1475169121-Feb-20 5:55
Member 1475169121-Feb-20 5:55 
Hello Everyone! I have found some code and developed a database that you can enter a student's name and their parents phone number. However, I am stuck. I need the data from the SQL database to populate and spinner, but I don't even know where to begin with coding that. I would appreciate any help that could be given. Here is the code I am currently using (I did get this code online and tailored it to work with my version of Android Studio. (I am using version 3.5)

The following code is in MainActivity.java

package codebind.example.sqliteoperations;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    EditText Name, Pass , updateold, updatenew, delete;
    myDbAdapter helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Name= (EditText) findViewById(R.id.editName);
        Pass= (EditText) findViewById(R.id.editPass);
        updateold= (EditText) findViewById(R.id.editText3);
        updatenew= (EditText) findViewById(R.id.editText5);
        delete = (EditText) findViewById(R.id.editText6);

        helper = new myDbAdapter(this);
    }
    public void addUser(View view)
    {
        String t1 = Name.getText().toString();
        String t2 = Pass.getText().toString();
        if(t1.isEmpty() || t2.isEmpty())
        {
            Message.message(getApplicationContext(),"Enter Both Name and Password");
        }
        else
        {
            long id = helper.insertData(t1,t2);
            if(id<=0)
            {
                Message.message(getApplicationContext(),"Insertion Unsuccessful");
                Name.setText("");
                Pass.setText("");
            } else
            {
                Message.message(getApplicationContext(),"Insertion Successful");
                Name.setText("");
                Pass.setText("");
            }
        }
    }

    public void viewdata(View view)
    {
        String data = helper.getData();
        Message.message(this,data);
    }

    public void update( View view)
    {
        String u1 = updateold.getText().toString();
        String u2 = updatenew.getText().toString();
        if(u1.isEmpty() || u2.isEmpty())
        {
            Message.message(getApplicationContext(),"Enter Data");
        }
        else
        {
            int a= helper.updateName( u1, u2);
            if(a<=0)
            {
                Message.message(getApplicationContext(),"Unsuccessful");
                updateold.setText("");
                updatenew.setText("");
            } else {
                Message.message(getApplicationContext(),"Updated");
                updateold.setText("");
                updatenew.setText("");
            }
        }

    }
    public void delete( View view)
    {
        String uname = delete.getText().toString();
        if(uname.isEmpty())
        {
            Message.message(getApplicationContext(),"Enter Data");
        }
        else{
            int a= helper.delete(uname);
            if(a<=0)
            {
                Message.message(getApplicationContext(),"Unsuccessful");
                delete.setText("");
            }
            else
            {
                Message.message(this, "DELETED");
                delete.setText("");
            }
        }
    }
}


The following code is in myDbAdapter.java

package codebind.example.sqliteoperations;

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


public class myDbAdapter {
    myDbHelper myhelper;
    public myDbAdapter(Context context)
    {
        myhelper = new myDbHelper(context);
    }

    public long insertData(String name, String pass)
    {
        SQLiteDatabase dbb = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.NAME, name);
        contentValues.put(myDbHelper.MyPASSWORD, pass);
        long id = dbb.insert(myDbHelper.TABLE_NAME, null , contentValues);
        return id;
    }

    public String getData()
    {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        String[] columns = {myDbHelper.UID,myDbHelper.NAME,myDbHelper.MyPASSWORD};
        Cursor cursor =db.query(myDbHelper.TABLE_NAME,columns,null,null,null,null,null);
        StringBuffer buffer= new StringBuffer();
        while (cursor.moveToNext())
        {
            int cid =cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
            String name =cursor.getString(cursor.getColumnIndex(myDbHelper.NAME));
            String  password =cursor.getString(cursor.getColumnIndex(myDbHelper.MyPASSWORD));
            buffer.append(cid+ "   " + name + "   " + password +" \n");
        }
        return buffer.toString();
    }

    public  int delete(String uname)
    {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        String[] whereArgs ={uname};

        int count =db.delete(myDbHelper.TABLE_NAME ,myDbHelper.NAME+" = ?",whereArgs);
        return  count;
    }

    public int updateName(String oldName , String newName)
    {
        SQLiteDatabase db = myhelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(myDbHelper.NAME,newName);
        String[] whereArgs= {oldName};
        int count =db.update(myDbHelper.TABLE_NAME,contentValues, myDbHelper.NAME+" = ?",whereArgs );
        return count;
    }

    static class myDbHelper extends SQLiteOpenHelper
    {
        private static final String DATABASE_NAME = "myDatabase";    // Database Name
        private static final String TABLE_NAME = "myTable";   // Table Name
        private static final int DATABASE_Version = 1;    // Database Version
        private static final String UID="_id";     // Column I (Primary Key)
        private static final String NAME = "Name";    //Column II
        private static final String MyPASSWORD= "Password";    // Column III
        private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
                " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255) ,"+ MyPASSWORD+" VARCHAR(225));";
        private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
        private Context context;

        public myDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_Version);
            this.context=context;
        }

        public void onCreate(SQLiteDatabase db) {

            try {
                db.execSQL(CREATE_TABLE);
            } catch (Exception e) {
                Message.message(context,""+e);
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                Message.message(context,"OnUpgrade");
                db.execSQL(DROP_TABLE);
                onCreate(db);
            }catch (Exception e) {
                Message.message(context,""+e);
            }
        }
    }
}


The following code is in Message.java

package codebind.example.sqliteoperations;

import android.content.Context;
import android.widget.Toast;

public class Message {
    public static void message(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }
}


The following code is in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4B1414">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="12dp"
        android:gravity="center"
        android:text="Student Name"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <EditText
        android:id="@+id/editName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:gravity="center_vertical|center"
        android:hint="Enter Name"
        android:inputType="textPersonName"
        android:textColor="#FFFFFF"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editName"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="13dp"
        android:gravity="center"
        android:hint="Enter Password"
        android:text="Parent Number"
        android:textColor="#FFFFFF"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button"
        android:layout_alignEnd="@+id/button4"
        android:layout_alignRight="@+id/button4"
        android:layout_alignBottom="@+id/button"
        android:onClick="viewdata"
        android:text="View Data"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editPass"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="28dp"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="23dp"
        android:onClick="addUser"
        android:text="Add User"
        android:textSize="18sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:layout_alignStart="@+id/button4"
        android:layout_alignLeft="@+id/button4"
        android:layout_marginTop="13dp"
        android:onClick="update"
        android:text="Update"
        android:textStyle="normal|bold" />

    <EditText
        android:id="@+id/editText6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button4"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_toStartOf="@+id/button2"
        android:layout_toLeftOf="@+id/button2"
        android:ems="10"
        android:freezesText="false"
        android:hint="Enter Name to Delete Data"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="21dp"
        android:layout_marginRight="21dp"
        android:layout_marginBottom="41dp"
        android:onClick="delete"
        android:text="Delete"
        android:textStyle="normal|bold"
        tools:ignore="RelativeOverlap" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="7dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="47dp"
        android:ems="10"
        android:hint="Current Name"
        android:inputType="textPersonName"
        android:textColor="#FFFFFF"
        android:textSize="14sp"
        android:textStyle="bold|italic" />

    <EditText
        android:id="@+id/editPass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="11dp"
        android:ems="10"
        android:gravity="center_vertical|center"
        android:hint="Enter Number"
        android:inputType="textPassword"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:textStyle="normal|bold" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/editText3"
        android:layout_alignLeft="@+id/editText3"
        android:layout_alignTop="@+id/button3"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="New Name"
        android:inputType="textPersonName"
        android:textSize="14sp"
        android:textStyle="bold|italic" />

</RelativeLayout>


Thanks you for all your help!
AnswerRe: Transfer data from SQL database to populate spinner Pin
Richard MacCutchan21-Feb-20 6:10
mveRichard MacCutchan21-Feb-20 6:10 
GeneralRe: Transfer data from SQL database to populate spinner Pin
Member 1475169121-Feb-20 7:38
Member 1475169121-Feb-20 7:38 
GeneralRe: Transfer data from SQL database to populate spinner Pin
Richard MacCutchan21-Feb-20 22:08
mveRichard MacCutchan21-Feb-20 22:08 
GeneralRe: Transfer data from SQL database to populate spinner Pin
Member 1475169122-Feb-20 14:38
Member 1475169122-Feb-20 14:38 
QuestionJson Manager Project (Need Critique/Possible Tips) Pin
Member 147385357-Feb-20 16:20
Member 147385357-Feb-20 16:20 
AnswerRe: Json Manager Project (Need Critique/Possible Tips) Pin
Richard MacCutchan7-Feb-20 22:06
mveRichard MacCutchan7-Feb-20 22:06 
GeneralRe: Json Manager Project (Need Critique/Possible Tips) Pin
Member 147385358-Feb-20 3:56
Member 147385358-Feb-20 3:56 
QuestionHow to optimize the time complexity of counting a word Pin
neelayak7-Feb-20 3:28
neelayak7-Feb-20 3:28 
AnswerRe: How to optimize the time complexity of counting a word Pin
Richard MacCutchan7-Feb-20 4:08
mveRichard MacCutchan7-Feb-20 4:08 
GeneralRe: How to optimize the time complexity of counting a word Pin
neelayak7-Feb-20 4:21
neelayak7-Feb-20 4:21 
GeneralRe: How to optimize the time complexity of counting a word Pin
Richard MacCutchan7-Feb-20 4:41
mveRichard MacCutchan7-Feb-20 4:41 
QuestionMessage Closed Pin
20-Jan-20 14:21
bekagaw65320-Jan-20 14:21 
JokeJ Pin
Member 1471420112-Jan-20 9:03
Member 1471420112-Jan-20 9:03 
SuggestionRe: Java task zigzag Pin
Richard MacCutchan12-Jan-20 22:11
mveRichard MacCutchan12-Jan-20 22:11 
AnswerRe: Java task zigzag Pin
ZurdoDev13-Jan-20 1:05
professionalZurdoDev13-Jan-20 1:05 
GeneralRe: Java task zigzag Pin
Richard MacCutchan13-Jan-20 1:33
mveRichard MacCutchan13-Jan-20 1:33 
Questionbus reservation and ticketting system Pin
Member 1469715023-Dec-19 10:20
Member 1469715023-Dec-19 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.