Lab6 Sqlite Part 2

Lab6 03ListView

Now, we shall add a ListView so as to list/show the Sqlite data inside a ListView UI component

Step1 : Create another activity called ListViewActivity

              Right clicked on app >new>Activity>Empty Activity.  Name it as ListViewActivity

Step 2 : Drag a ListView container into the ListViewActivity layout and name it as list.


Lab6 04ListItems

Step 1: To try out ListView  to list static item like Item1 ,Item 2 and Item 3

Let’s add the following codes to ListViewActivity.java   

ListView listView; //#1


inside onCreate method
//#2 Bind java and xml

  listView = (ListView) findViewById(R.id.list);

  //#3 Defined Array values to show in ListView

  ArrayList<String> listItems = new ArrayList<String>();

  //#4 Adding items to arrayList

  listItems.add("Item 1");
  listItems.add("Item 2");
  listItems.add("Item 3");


  //#5 create ArrayAdapter with listItems

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, listItems);

    //#6 Assign adapter to ListView, show the items in the listView
  listView.setAdapter(adapter);



Step 2: Edit  MainActivity.java

Add the following codes for clickListener for Button btnViewData
Remember to declare  Button btnViewData, Bind it to the XML component  R.id.btnViewData

inside onCreate method
//#6  ClickListener for btnViewData

  btnViewData.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) { 

  //move from MainActivity page to ListView Page

  Intent intent = new Intent(MainActivity.this, ListViewActivity.class);
  startActivity(intent);


    }

});

  //========btnViewData=========




Test it out


Lab6 05ListSqliteItems

Step 1: Modify SqlLiteHelper.java
 
Add a method readData to read data from SQLite 
and store inside 3 arraylists, listName,listID and listTel  

public void readData(SQLiteDatabase db, String sqlStatement, ArrayList<String> listName,ArrayList<String> listID, ArrayList<String> listTel)
{
Cursor cursor = db.rawQuery(sqlStatement, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
//Add each field of record to listName, listID,listTel
listName.add(cursor.getString(cursor.getColumnIndex("Name")));
listID.add(""+cursor.getInt(cursor.getColumnIndex("recId")));
listTel.add(cursor.getString(cursor.getColumnIndex("Tel")));
Log.i("Check it out",cursor.getString(cursor.getColumnIndex("Name"))+"" );
} while (cursor.moveToNext());
}
// close db connection

db.close();
}

Step 2: Modify ListViewActivity.java to List data from the sqlite file TelDirectory.db and Table name TelDirList


Place the codes in the correct sections  Here are the partial codes:


class variables
//#2 Get data from TelDirectory.db
SQLiteDatabase db ;
SqlLiteHelper sqlh = new SqlLiteHelper();
//#3 declare 3 ArrayList to contain listID,listTel, and listName

ArrayList<String> listID = new ArrayList<>();
ArrayList<String> listTel = new ArrayList<>();
ArrayList<String> listName = new ArrayList<>();


inside onCreate method and paste it after the line
listItems.add("Item 3");
  
//Get data from TelDirectory.db
db = openOrCreateDatabase("TelDirectory.db", MODE_PRIVATE, null);

// Select All data from table TelDirList
String sqlStatement = "SELECT  * FROM  TelDirList";

//readData from SQLite and save results in listName,listID and listTel
sqlh.readData(db,sqlStatement,listName,listID,listTel);


To show the listName on ListView Change the ArrayAdpater from
//#5 create ArrayAdapter with listItems

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, listItems);

To 
//#5 create ArrayAdapter with listItems

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, listName);



Lab6 06ListViewListener

Modify ListViewActivity.java 

Step 1:  Add a listener for ListView


// ListView Item Click Listener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


}
});

inside the onItemClick method add the following codes
// ListView Clicked item index
int itemPosition = position;

// ListView Clicked item value
String name = (String) listView.getItemAtPosition(itemPosition);
String recId = listID.get(itemPosition);
String tel = listTel.get(itemPosition);

// Show Alert

Toast.makeText(getApplicationContext(),itemPosition+" ID:"+recId +" Name:"+name+" Tel: "+tel, Toast.LENGTH_LONG).show();

Test it out


Lab6 07PassValues from one activity (page) to another activity (page)

Step 1:  Create another Activity called UpdateDataActivity.java for
              Update and Delete sqlite data

Right clicked on app >new>Activity>Empty Activity.  Name it as UpdateDataActivity


Step 2: Click on the activity_update_data.xml layout

create the layout shown below



























Step 3: Modify ListViewActivity.java

               To move from ListViewActivity page to UpdateDataActivity page and
              and pass the data name,recId,tel to UpdateDataActivity page

Add the codes below inside the method onItemClick of ClickListener of ListView

//Move from ListViewActivity page to UpdateDataActivity page

  Intent nextPage = new Intent(ListViewActivity.this, UpdateDataActivity.class);

  
  //and pass the data name,recId,tel to UpdateDataActivity page

  nextPage.putExtra("name",name);
  nextPage.putExtra("recId",recId);
  nextPage.putExtra("tel",tel);

  //start nextPage UpdateDataActivity

  startActivity(nextPage);



Lab6 08 delete update Layout

Step 1: Modify UpdateDataActivity.java
Add the following codes to the onCreate method of UpdateDataActivity.java

//Get data from previous page ListViewActivity

  Intent intent = getIntent();
  String name = intent.getStringExtra("name"); //Receiving the values from previous Page

  final String recId = intent.getStringExtra("recId"); //Receiving the values from previous Page

  String tel = intent.getStringExtra("tel"); //Receiving the values from previous Page

  

//binding

  tvStatus = (TextView) findViewById(R.id.tvStatus);
  etName = (EditText) findViewById(R.id.etName);
  etTel = (EditText) findViewById(R.id.etTelNum);

 

  //set text on EditText for etName and etTel

  etName.setText(name);
  etTel.setText(tel);



Test it out


Lab6 09 delete update action
Modify UpdateDataActivity.java to enable Delete and Update data from Sqlite data
Here are some codes to help you

class variable
SQLiteDatabase  db;
SqlLiteHelper sqlh = new SqlLiteHelper();



db = openOrCreateDatabase("TelDirectory.db", MODE_PRIVATE, null);


String sqlStatement = "Update TelDirList set Tel= '" + tel + "',name = '" + name + "' where recId = '"+ recId +"'"  ;


String sqlStatement = "delete from TelDirList where recId = '"+ recId +"'";


Test it out

pw: solnsoln

No comments:

Post a Comment

Note: only a member of this blog may post a comment.