0% found this document useful (0 votes)
60 views5 pages

Practical NO - 26

This document outlines the implementation of an Android application that performs asynchronous tasks using SQLite. It includes XML layout files for the user interface, Java code for the main activity handling data insertion, retrieval, and database reset operations, as well as a custom adapter for displaying data in a list view. The application utilizes AsyncTask for background processing to ensure smooth user experience while interacting with the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Practical NO - 26

This document outlines the implementation of an Android application that performs asynchronous tasks using SQLite. It includes XML layout files for the user interface, Java code for the main activity handling data insertion, retrieval, and database reset operations, as well as a custom adapter for displaying data in a list view. The application utilizes AsyncTask for background processing to ensure smooth user experience while interacting with the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical NO.26 : Perform Async Task using SQLite.

• activity_main.xml

<RelativeLayout android:layout_height="wrap_content"

xmlns:android="[Link] android:layout_below="@id/button_add"
apk/res/android" android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
xmlns:tools="[Link] android:text="Show Data"/>
ols" <Button
android:layout_width="match_parent" android:id="@+id/button_reset_db"
android:layout_height="match_parent" android:layout_width="wrap_content"
tools:context=".MainActivity"> android:layout_height="wrap_content"
<EditText
android:id="@+id/editText" android:layout_below="@id/button_show_d
android:layout_width="match_parent" ata"
android:layout_height="wrap_content" android:layout_centerHorizontal="true"
android:hint="Enter data" android:layout_marginTop="16dp"
android:layout_margin="16dp"/> android:text="Reset DB"/>
<Button <ListView
android:id="@+id/button_add" android:id="@+id/listView"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_below="@id/editText"
android:layout_centerHorizontal="true" android:layout_below="@id/button_reset_db
android:layout_marginTop="16dp" "
android:text="Add Data"/> android:layout_marginTop="16dp"/>
<Button </RelativeLayout>
android:id="@+id/button_show_data"
android:layout_width="wrap_content"

• [Link]

package [Link]; private Button buttonAdd,


buttonShowData, buttonResetDB;
import [Link]; private ListView listView;
import [Link]; private DatabaseHelper databaseHelper;
import [Link]; private DataAdapter dataAdapter;
import [Link]; private ArrayList<String> dataList;
import [Link];
import [Link]; @Override
import [Link]; protected void onCreate(Bundle
savedInstanceState) {
import [Link](savedInstanceState);
[Link]; setContentView([Link].activity_main);

import [Link]; editText = findViewById([Link]);


buttonAdd =
public class MainActivity extends findViewById([Link].button_add);
AppCompatActivity { buttonShowData =
private EditText editText; findViewById([Link].button_show_data);
buttonResetDB =
findViewById([Link].button_reset_db); doInBackground(String... params) {
listView = findViewById([Link]); return
[Link](params[0]);
databaseHelper = new }
DatabaseHelper(this);
dataList = new ArrayList<>(); @Override
dataAdapter = new DataAdapter(this, protected void onPostExecute(Boolean
dataList); result) {
[Link](dataAdapter); if (result) {
[Link]([Link],
// Add Data Button "Data inserted successfully",
[Link](new Toast.LENGTH_SHORT).show();
[Link]() { [Link]("");
@Override } else {
public void onClick(View v) { [Link]([Link],
String data = "Failed to insert data",
[Link]().toString().trim(); Toast.LENGTH_SHORT).show();
}
if (![Link]()) { }
new }
InsertDataTask().execute(data);
} else { // Fetch Data Task
private class FetchDataTask extends
[Link]([Link], "Please AsyncTask<Void, Void, ArrayList<String>>
enter data", Toast.LENGTH_SHORT).show(); {
} @Override
} protected ArrayList<String>
}); doInBackground(Void... voids) {
return [Link]();
// Show Data Button }

[Link](new @Override
[Link]() { protected void
@Override onPostExecute(ArrayList<String> result) {
public void onClick(View v) { if (result != null && ![Link]())
new FetchDataTask().execute(); {
} [Link]();
}); [Link](result);

// Reset DB Button [Link]();


[Link](new } else {
[Link]() { [Link]([Link],
@Override "No data found",
public void onClick(View v) { Toast.LENGTH_SHORT).show();
new }
ResetDatabaseTask().execute(); }
} }
});
} // Reset Database Task
private class ResetDatabaseTask extends
// Insert Data Task AsyncTask<Void, Void, Boolean> {
private class InsertDataTask extends @Override
AsyncTask<String, Void, Boolean> { protected Boolean
@Override doInBackground(Void... voids) {
protected Boolean return [Link]();
} } else {
[Link]([Link],
@Override "Failed to reset database",
protected void onPostExecute(Boolean Toast.LENGTH_SHORT).show();
result) { }
if (result) { }
[Link]([Link], }
"Database reset successfully", }
Toast.LENGTH_SHORT).show();
[Link]();

[Link]();

• [Link]

package [Link]; context) {


super(context, DATABASE_NAME,
import [Link]; null, DATABASE_VERSION);
import [Link]; }
import [Link];
import @Override
[Link]; public void onCreate(SQLiteDatabase
import db) {
[Link]
er; [Link](CREATE_TABLE_QUERY)
;
import [Link]; }

public class DatabaseHelper extends @Override


SQLiteOpenHelper { public void
onUpgrade(SQLiteDatabase db, int
private static final String oldVersion, int newVersion) {
DATABASE_NAME = "my_database"; [Link]("DROP TABLE IF
private static final int EXISTS " + TABLE_NAME);
DATABASE_VERSION = 1; onCreate(db);
}
private static final String
TABLE_NAME = "my_table"; public boolean insertData(String data)
private static final String {
COLUMN_ID = "id"; SQLiteDatabase db =
private static final String [Link]();
COLUMN_DATA = "data"; ContentValues values = new
ContentValues();
private static final String [Link](COLUMN_DATA, data);
CREATE_TABLE_QUERY = long result =
"CREATE TABLE " + [Link](TABLE_NAME, null, values);
TABLE_NAME + " (" + return result != -1;
COLUMN_ID + " }
INTEGER PRIMARY KEY
AUTOINCREMENT," + public ArrayList<String> getAllData()
COLUMN_DATA + " {
TEXT)"; ArrayList<String> dataList = new
ArrayList<>();
public DatabaseHelper(Context SQLiteDatabase db =
[Link](); return dataList;
Cursor cursor = }
[Link]("SELECT * FROM " +
TABLE_NAME, null); public boolean resetDatabase() {
SQLiteDatabase db =
if ([Link]()) { [Link]();
do { [Link]("DELETE FROM " +
String data = TABLE_NAME);
[Link](1); // Get data from [Link]("VACUUM"); //
column 1 Optional to optimize DB size
[Link](data); return true;
} while ([Link]()); }
} }
[Link]();

• [Link]

package [Link];
@Override
import [Link]; public View getView(int position, View
import [Link]; convertView, ViewGroup parent) {
import [Link]; String data = getItem(position);
import [Link];
import [Link]; if (convertView == null) {
import [Link]; convertView =
[Link](getContext()).inflate([Link].list_
item, parent, false);
import [Link];
}

public class DataAdapter extends


TextView textView =
ArrayAdapter<String> {
[Link]([Link]);
public DataAdapter(Context context,
[Link](data);
ArrayList<String> dataList) {
return convertView;
super(context, 0, dataList);
}
}
}
• List_item.xml

<TextView
xmlns:android="[Link]
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="16sp"
android:textColor="#000000"/>
Output:

You might also like