Skip to content

Latest commit

 

History

History
276 lines (259 loc) · 9.29 KB

File metadata and controls

276 lines (259 loc) · 9.29 KB

Practical 5: Design a simple to-do list using SQLite.

activity_main.xml

  <&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

<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="ty.practical5.MainActivity"> <ListView android:id="@+id/lvData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btnAdd" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" /> <EditText android:id="@+id/txtItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignTop="@+id/btnAdd" android:layout_toLeftOf="@+id/btnAdd" android:layout_toStartOf="@+id/btnAdd" android:hint="Enter a New Item" android:inputType="textMultiLine" />

<Button android:id="@+id/btnAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:onClick="AddItem" android:text="Add Item" /> </RelativeLayout> activity_task_details.xml <?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="ty.practical5.TaskDetails"> <Button android:id="@+id/btnUpdate" PREPARED BY SARVEPREET SINGH => [email protected] android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/txtData" android:layout_marginStart="48dp" android:layout_marginTop="50dp" android:onClick="Update" android:text="Update" /> <Button android:id="@+id/btnDelete"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/txtData" android:layout_alignTop="@+id/btnUpdate" android:layout_marginEnd="13dp" android:onClick="Delete" android:text="Delete" /> <EditText android:id="@+id/txtData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignStart="@+id/btnUpdate" android:layout_marginStart="19dp" android:layout_marginTop="57dp" android:ems="10" android:inputType="textMultiLine" /> </RelativeLayout>

toDoDatabaseHelper.java

import android.content.ContentValues;

import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; public class toDoDatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "todoList.db"; private static final int DATABASE_VERSION = 1; public toDoDatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String query = "CREATE TABLE ToDo (task TEXT)"; db.execSQL(query); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS ToDo"); onCreate(db); } public void addTask(String item){ ContentValues values = new ContentValues(); values.put("task", item); SQLiteDatabase db = getWritableDatabase(); db.insert("ToDo", null, values); db.close(); } //Delete a product from the database public void deleteTask(String item){ SQLiteDatabase db = this.getWritableDatabase(); db.execSQL("DELETE FROM ToDo where task='"+item+"'"); } public void updateTask(String oldvalue, String newvalue){ try { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("task", newvalue); db.update("ToDo", contentValues, "task='"+oldvalue +"'", null);

db.close(); } catch (Exception e) { e.printStackTrace(); } } public ArrayList<String> getAllTasks() { ArrayList<String> contactList = new ArrayList<String>(); String selectQuery = "SELECT * FROM ToDo"; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { contactList.add(cursor.getString(0)); } while (cursor.moveToNext()); } return contactList; } public ArrayList<String> getTaskByItem(int item) { ArrayList<String> contactList = new ArrayList<String>(); String selectQuery = "SELECT * FROM ToDo where task=" +item; SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { contactList.add(cursor.getString(1)); } return contactList; } } PREPARED BY SARVEPREET SINGH => [email protected] MainActivity.java

import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ArrayList<String> items; private ArrayAdapter<String> itemsAdapter; private ListView lvData; private toDoDatabaseHelper dbAccess; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbAccess = new toDoDatabaseHelper(this); lvData = (ListView) findViewById(R.id.lvData); items = new ArrayList<String>(); readItems(); itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); lvData.setAdapter(itemsAdapter); lvData.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, TaskDetails.class); intent.putExtra("data",

lvData.getItemAtPosition(position).toString()); startActivity(intent); // Refresh the adapter refreshListView(); } }); } public void AddItem(View v) { EditText txtItem = (EditText) findViewById(R.id.txtItem); String itemText = txtItem.getText().toString(); itemsAdapter.add(itemText); txtItem.setText(""); dbAccess.addTask(itemText); } public void readItems() { try { items = new ArrayList<String>(dbAccess.getAllTasks()); } catch (Exception e) { items = new ArrayList<String>(); } }

public void refreshListView() { itemsAdapter.notifyDataSetChanged(); } } TaskDetails.java package ty.practical5; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;

import android.view.MenuItem; import android.view.View; import android.widget.EditText; public class TaskDetails extends AppCompatActivity { private toDoDatabaseHelper dbAccess; String oldvalue=""; EditText txtData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_task_details); //code to enable the back button getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); //code to fetch the selected list item data in the previous activity dbAccess = new toDoDatabaseHelper(this); Intent intent = getIntent(); oldvalue = intent.getStringExtra("data"); txtData = (EditText) findViewById(R.id.txtData); txtData.setText(intent.getStringExtra("data")); } //code for delete button to delete the task protected void Delete(View v) { dbAccess.deleteTask(txtData.getText().toString()); Intent intent = new Intent(TaskDetails.this,MainActivity.class) ; intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } //code for delete button to update the task protected void Update(View v) {

dbAccess.updateTask(oldvalue, txtData.getText().toString()); Intent intent = new Intent(TaskDetails.this,MainActivity.class) ; intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } //code to close the current activity and move to the previous @Override public boolean onOptionsItemSelected(MenuItem item) { finish(); return super.onOptionsItemSelected(item); } }

Output