Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Working with Databases In today’s lesson, we spent a lot of time looking at SQLite databases, tools, and the SQL language. As such, we have just enough time to start our Lesson 6 application that uses SQLite. Today, we’ll build the main activities, and layouts needed for our application, while our next lesson will provide the remaining code. Let’s begin with a new Eclipse project, with the following settings: Project name: A2L56_Database Create new project in workspace should be selected. Build Target: Android 4.0.3 (or any other version you’d like to work with 2.1 or higher) Application Name: A2L56_Database Package name: lastname.firstname. database (substitute lastname.firstname with your last name and first name – no spaces, apostrophes or dashes.) Create Activity should be checked, and DatabaseActivity should be the text for it. Min SDK Version: 15 (If using Android OS other than 15, use the correct SDK version here). Click Finish. Let’s begin, as we always do, with the user interface. Open the main.xml file, from the Res/Layout folder. We’re adding three buttons to the layout. This screen will be our main menu, and allow us to view a list of items in our SQLite table as ListView, or via a Cursor. In addition to that, we’ll add a “Add New” button to allow to add records to the database. 1 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:layout_margin="15dp" android:orientation="vertical" > <Button android:id="@+id/btnShow" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="Show Data" > </Button> <Button android:id="@+id/btnShowList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="Show List" > </Button> <Button android:id="@+id/btnAddNew" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="Add New" > </Button> </LinearLayout> Now, let’s create the rest of the XML UI files that we’ll need for this application. The first will be called by our “Show Data” activity, and will be a simple cursor outputting information as text. Add a new UI file to your project (we’ve done this before, so I’m leaving out the details of how to create a layout XML file). The new file should be called data_cursor.xml, and contain a vertical LinearLayout. To that, add a TextView. See code below for the complete XML of the UI. 2 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/txtOut" android:layout_width="fill_parent" android:layout_height="fill_parent" > </TextView> </LinearLayout> Save and close data_cursor.xml. Now, let’s create the UI that displays our data in a ListView. At the top of the ListView, we’ll add two buttons. The first will allow us to add a record (take us to the activity where we can add a record), and the second will take us back to the main menu (same as pressing the back button on the device). Create a new UI file, and call it data_list.xml. Make the UI look like this: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" > <Button android:id="@+id/btnGoMain" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="Main Menu" /> <Button android:id="@+id/btnGoNew" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_margin="5dp" android:text="Add New" /> </LinearLayout> <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1.0" /> </LinearLayout> 3 Save and close the data_list.xml file. Now, we need to create a row that will be used by the ListView. The row.xml file will allow us to define which fields we want to see on each row of the ListView. Create a new layout XML file called row.xml, and add the following code: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/txtID" android:layout_width="30dip" android:layout_height="40dip" android:textSize="15dip" /> <TextView android:id="@+id/txtAuthor" android:layout_width="200dip" android:layout_height="wrap_content" android:gravity="left" android:textStyle="bold" /> <TextView android:id="@+id/txtYearBorn" android:layout_width="50dip" android:layout_height="wrap_content" android:gravity="right" android:textStyle="bold" /> </LinearLayout> The above layout creates three textviews, that we will populate with data from our SQLite database. Then each row in our ListView will use this type of row. Save and close row.xml. Our last UI file is the Add / Edit screen. Create a new UI file called add_edit_data.xml, and add the following code: 4 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:layout_margin="15dp" android:orientation="vertical" > <TextView android:id="@+id/AuID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visible="gone" > </TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Author:" > </TextView> <EditText android:id="@+id/txtAuthor" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:text="" > </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Year Born:" > </TextView> <EditText android:id="@+id/txtYearBorn" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:text="" > </EditText> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/btnInsert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Insert" > </Button> <Button android:id="@+id/btnUpdate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Update" > </Button> <Button android:id="@+id/btnDelete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete" > </Button> </LinearLayout> </LinearLayout> 5 The end result will look like this: Ok, all of the UI files are done! We’re going to wrap up this session by creating the rest of the class (java) files, and linking up our three buttons on the main.xml interface on our main activity. Create the following three activities. Each one should extend Activity, except for DataListActivity which extends ListActivity, and have a basic onCreate event. Link up the appropriate user interface to each using the setContentView method. Below is the code for each of the new classes. DataCursorActivity package tushinsky.alex.database; import android.app.Activity; import android.os.Bundle; public class DataCursorActivity extends Activity { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.data_cursor); } 6 DataListActivity package tushinsky.alex.database; import android.app.Activity; import android.os.Bundle; public class DataListActivity extends ListActivity { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.data_list); } AddEditDataActivity package tushinsky.alex.database; import android.app.Activity; import android.os.Bundle; public class AddEditDataActivity extends Activity { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_edit_data); } The last class will be our SQL database helper class, and we’ll add that during lesson 6. Add the new activities to your Android Manifest file in order to register them for use. Remember that if you don’t do this step, your app will crash every time it’s launched and one of these new activities is requested. 7 <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".DatabaseActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".DataCursorActivity"></activity> <activity android:name=".DataListActivity"></activity> <activity android:name=".AddEditDataActivity"></activity> </application> Now, open DatabaseActivity.java for editing. We’re going to add our three button click events, and wire them up to open the correct activity when each of the buttons is selected. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button btnShowData = (Button) findViewById(R.id.btnShow); btnShowData.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { finish(); startActivity(new Intent(getBaseContext(), DataCursorActivity.class)); } }); final Button btnShowList = (Button) findViewById(R.id.btnShowList); btnShowList.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { finish(); startActivity(new Intent(getBaseContext(), DataListActivity.class)); } }); final Button btnAddNew = (Button) findViewById(R.id.btnAddNew); btnAddNew.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent myIntent = new Intent(v.getContext(), AddEditDataActivity.class); Bundle b = new Bundle(); b.putLong("id", 0); myIntent.putExtras(b); finish(); startActivity(myIntent); } }); } 8 For btnShowData and btnShowList, things are fairly simple. We simply call a new activity, which opens to the appropriate screen. On the last button, btnAddNew, we launch the activity a bit differently. That’s because we also pass in a parameter to the activity (“id” = 0). This will be used by the AddEditDataActivity to determine if it should create a new record, or update an existing one. Now, open DataListActivity, and add the following click events to its onCreate method. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.data_list); final Button btnMain = (Button) findViewById(R.id.btnGoMain); btnMain.setOnClickListener(new View.OnClickListener() { }); public void onClick(View v) { finish(); startActivity(new Intent(getBaseContext(), DatabaseActivity.class)); } final Button btnGoNew = (Button) findViewById(R.id.btnGoNew); btnGoNew.setOnClickListener(new View.OnClickListener() { }); public void onClick(View v) { Intent myIntent = new Intent(v.getContext(), AddEditDataActivity.class); Bundle b = new Bundle(); b.putLong("id", 0); myIntent.putExtras(b); finish(); startActivity(myIntent); } } As a final step, add the following code to each of the three created classes. This method will be triggered whenever the user selects the back button on their device. It will let them naturally navigate to the main menu each time. @Override public void onBackPressed() { startActivity(new Intent(getBaseContext(), DatabaseActivity.class)); return; } This method should be the last method in DataCursorActivity, DataListActivity, and AddEditDataActivity. Save all of your files, and run your project. You should be able to navigate between screens without much issue. 9