Sie sind auf Seite 1von 5

---------------------------------------ALL METHOD (Only Java Class)

(Main)--------------------------------
package android.bscit.atmiya.database_demo;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

import static android.database.sqlite.SQLiteDatabase.*;

public class AllMethod extends SQLiteOpenHelper {

private static final int DB_VERSION=1;


private static final String DB_NAME="stud";
private static final String TABLE_NAME="bio";
private static final String KEY_ID="Id";
private static final String KEY_NAME="Name";
private static final String KEY_CITY="City";
SQLiteDatabase db;
String s;

public AllMethod(@Nullable Context context) {


super(context, DB_NAME,null,DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

s = " CREATE TABLE " + TABLE_NAME + " ( " + KEY_ID + " INTEGER PRIMARY KEY
AUTOINCREMENT, " + KEY_NAME + " TEXT, " + KEY_CITY + " TEXT );";
db.execSQL(s);
}

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

public void insert_data(String nm,String ct){


SQLiteDatabase db= this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(KEY_NAME,nm);
cv.put(KEY_CITY,ct);
db.insert(TABLE_NAME,null,cv);
db.close();
}
public Cursor getData(){
SQLiteDatabase db1 = this.getWritableDatabase();
Cursor c= db1.rawQuery("select "+ KEY_ID +" as _id, * from
"+TABLE_NAME,null);
return c;
}

}
----------------------------------------
MainActivity(1)--------------------------------------------

package android.bscit.atmiya.database_demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button btnin;
EditText etnm,etcy;
SQLiteDatabase db;
AllMethod al;
String a,b;

@SuppressLint("WrongConstant")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnin=findViewById(R.id.btnin);

etnm=findViewById(R.id.etname);
etcy=findViewById(R.id.etcity);

al = new AllMethod(MainActivity.this);

btnin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//
db=openOrCreateDatabase("student.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
//al.onCreate(db);
a=etnm.getText().toString();
b=etcy.getText().toString();
al.insert_data(a,b);

Toast.makeText(getApplicationContext(),"Record
Inserted",Toast.LENGTH_LONG).show();
}
});

}
}
----------------------------------------------------------XML File
(1.1)------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/etname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="107dp"
android:layout_marginEnd="0dp"
android:ems="10"
android:inputType="textPersonName" />

<EditText
android:id="@+id/etcity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="241dp"
android:ems="10"
android:inputType="textPersonName" />

<Button
android:id="@+id/btnin"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/etcity"
android:layout_alignBottom="@+id/etcity"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="-70dp"
android:text="Insert"
android:textSize="18sp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="9dp"
android:layout_marginTop="52dp"
android:text="Name : "
android:textSize="24sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="183dp"
android:text="City :"
android:textSize="24sp" />

</RelativeLayout>

----------------------------------------------------
ShowRecord.java(2)--------------------------------------

package android.bscit.atmiya.database_demo;

import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ShowRecord extends AppCompatActivity {

ListView lvrecord;
SimpleCursorAdapter sca;
AllMethod al;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_record);

lvrecord = findViewById(R.id.lvrecord);
bindData();
}

public void bindData(){


al=new AllMethod(this);
Cursor c=al.getData();
sca = new SimpleCursorAdapter(getApplicationContext(),
android.R.layout.simple_list_item_2,
c,
new String[]{"Name","City"},
new int[]{android.R.id.text1,android.R.id.text2},
0);
lvrecord.setAdapter(sca);
}
}
-----------------------------------------
ShowData.xml(2.1)----------------------------------------------

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShowRecord">

<ListView
android:id="@+id/lvrecord"
style="@style/Widget.AppCompat.ListView.DropDown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="1dp" />
</RelativeLayout>