0% found this document useful (0 votes)
8 views4 pages

Hritik 26

The document contains an Android XML layout and Java code for a customer details application. The layout includes fields for entering customer information and buttons for inserting and searching data in a SQLite database. The Java code handles database operations, including creating a table, inserting data, and retrieving customer details based on an ID search.

Uploaded by

sanketgithub10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Hritik 26

The document contains an Android XML layout and Java code for a customer details application. The layout includes fields for entering customer information and buttons for inserting and searching data in a SQLite database. The Java code handles database operations, including creating a table, inserting data, and retrieving customer details based on an ID search.

Uploaded by

sanketgithub10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Insert Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView"
android:gravity="center"
android:textSize="20dp"
android:textColor="#000000"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ID"
android:id="@+id/editid"
android:layout_below="@+id/textView"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/editname"
android:layout_below="@+id/editid"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Mobile No."
android:id="@+id/editmobile"
android:layout_below="@+id/editname"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:lines="3"
android:id="@+id/editaddress"
android:layout_below="@+id/editmobile"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Pin Code"
android:id="@+id/editpincode"
android:layout_below="@+id/editaddress"/>
<Button
android:text="Insert Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editpincode"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
<TextView
android:text="Search Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:id="@+id/textView1"
android:gravity="center"
android:textSize="20dp"
android:layout_below="@+id/button"
android:textColor="#000000"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter ID"
android:id="@+id/editsearchid"
android:layout_below="@+id/textView1"/>
<Button
android:text="Search Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editsearchid"
android:layout_centerHorizontal="true"
android:id="@+id/button1" />
</RelativeLayout>

java file

package com.example.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
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 {
SQLiteDatabase sqLiteDatabaseObj;
EditText editTextID, editTextName, editMobileNo, editAddress, editPincode,
editSearchid;
String cid, cname, cmobile, caddress, cpincode, sql_query, sid;
Button EnterData, SearchData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EnterData = (Button) findViewById(R.id.button);
SearchData = (Button) findViewById(R.id.button1);
editTextID = (EditText) findViewById(R.id.editid);
editTextName = (EditText) findViewById(R.id.editname);
editMobileNo = (EditText) findViewById(R.id.editmobile);
editAddress = (EditText) findViewById(R.id.editaddress);
editPincode = (EditText) findViewById(R.id.editpincode);
editSearchid = (EditText) findViewById(R.id.editsearchid);

EnterData.setOnClickListener(new View.OnClickListener() {
@Override

public void onClick(View view) {


sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase",
Context.MODE_PRIVATE, null);
sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS AndroidJSonTable(id
INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, cid VARCHAR, name VARCHAR,
mobile VARCHAR, address VARCHAR, pincode VARCHAR);");
cid = editTextID.getText().toString();
cname = editTextName.getText().toString();
cmobile = editMobileNo.getText().toString();
caddress = editAddress.getText().toString();
cpincode = editPincode.getText().toString();
sql_query = "INSERT INTO AndroidJSonTable (cid, name, mobile, address, pincode)
VALUES('" + cid + "', '" + cname + "', '" + cmobile + "', '" + caddress + "', '" + cpincode + "');";
sqLiteDatabaseObj.execSQL(sql_query);
Toast.makeText(getApplicationContext(), "Data Inserted Successfully",
Toast.LENGTH_LONG).show();
}
});
SearchData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sid = editSearchid.getText().toString();
Cursor cursor = sqLiteDatabaseObj.rawQuery("select * from AndroidJSonTable where
cid=" + sid + "", null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
String cid = cursor.getString(1);
String name = cursor.getString(2);
String mob = cursor.getString(3);
String addr = cursor.getString(4);
String pcode = cursor.getString(5);
buffer.append(cid + " " + name + " " + mob + " " + addr + " " + pcode + "\n");
Toast.makeText(getApplicationContext(), buffer,
Toast.LENGTH_LONG).show();
}
}
});
}
}

You might also like