Differences

This shows you the differences between two versions of the page.

Link to this comparison view

osp:lectures:lecture-internals [2016/10/05 11:40]
laura.gheorghe created
osp:lectures:lecture-internals [2016/11/06 18:47] (current)
laura.gheorghe [Lecture]
Line 8: Line 8:
  
  
-  *{{:​osp:​lectures:​lecture-2.pdf | Lecture Slides ​- Part 1}}+  *{{:​osp:​lectures:​lecture-internals.pdf | Lecture Slides}} 
 +  *{{:​osp:​lectures:​3.internals_notes.pdf | Lecture Notes}}
  
-{{url>​http://​ocw.cs.pub.ro/​courses/​_media/​osp/​lectures/​lecture-2.pdf}}+{{url>​http://​ocw.cs.pub.ro/​courses/​_media/​osp/​lectures/​lecture-internals.pdf}}
  
- 
-  *{{:​osp:​lectures:​lecture-2-extra.pdf | Lecture Slides - Part 2}} 
- 
-{{url>​http://​ocw.cs.pub.ro/​courses/​_media/​osp/​lectures/​lecture-2-extra.pdf}} 
 ==== Practical ==== ==== Practical ====
  
Line 26: Line 23:
 {{:​osp:​media:​lab2.zip|}} {{:​osp:​media:​lab2.zip|}}
  
-=== Task 1 - Launching an Activity ===+=== Task 1 - Launching an Activity ​(3p) ===
  
-Create a new project and add a button to the main activity layout. Get a reference to this button in the onCreate method in MainActivity. Add a new Activity to the project. You can add a new activity ​automatically ​by right clicking on the project and selecting **New > Activity > Blank Activity** from the menu. This will start a wizard similar to the one at application creation.+Create a new project and add a button to the main activity layout. Get a reference to this button in the onCreate method in MainActivity. Add a new Activity to the project. You can automatically ​add a new activity by right clicking on the project and selecting **New > Activity > Blank Activity** from the menu. This will start a wizard similar to the one at application creation.
  
-Back in MainActivity,​ add code to the onClick of the OnClickListener so that the new activity gets started. To start a new Activity you have to create an intent first which will explicitly take two parameters: a Context and the Class of the second activity. A context represents an interface to the current state of the application ​/ object. It allows access to application specific resources and operations such as launching an activity or starting a service. In order to obtain the context you can use a reference to the current instance of the MainActivity class.+Back in MainActivity,​ add code to the onClick of the OnClickListener so that the new activity gets started. To start a new Activity you have to create an intent first which will explicitly take two parameters: a Context and the Class of the second activity. A context represents an interface to the current state of the application. It allows access to application specific resources and operations such as launching an activity or starting a service. In order to obtain the context you can use a reference to the current instance of the MainActivity class.
  
 <​code>​ <​code>​
Line 45: Line 42:
 </​code>​ </​code>​
  
-Before launching the new activity, it must be declared in AndroidManifest.xml file. If you create ​the activity ​manually, you will have to add a similar line to the following one to the manifest file. If you created the activity through the wizard, ​it should have been added automatically.+Before launching the new activity, it must be declared in the AndroidManifest.xml file. If you manually created ​the activity , you will have to add a similar line to the following one to the manifest file. If you created the activity through the wizard, ​the line should have been added automatically.
  
 <​code>​ <​code>​
Line 52: Line 49:
 </​code>​ </​code>​
  
-If you created the Activity manually, ​the onCreate of the second activity make sure you call the onCreate of the super class and use setContentView to the layout of the second activity for which you will have to create an empty layout.+If you created the Activity manually, ​in the onCreate of the second activity make sure you call the onCreate of the super class and use setContentView to the layout of the second activity for which you will have to create an empty layout.
  
 <​code>​ <​code>​
Line 59: Line 56:
 </​code>​ </​code>​
  
-Intents can also contain a dictionary of data (called Intent extras). Add an EditText box to your main activity layout and a TextView to your second activity ​layout. When clicking the button, get the text from the EditText elementadd it to the intent. ​+Intents can also contain a dictionary of data (called Intent extras). Add an EditText box to your main activity layout and a TextView to your second activity ​layout. When clicking the button, get the text from the EditText element ​and add it to the intent. ​
  
 <​code>​ <​code>​
Line 68: Line 65:
  
  
-=== Task 2 - Lists and Adapters ===+=== Task 2 - Lists and Adapters ​(4p) ===
  
 For this task, you will have to unpack the lab archive and copy the three files into the current project. Copy **MyMenuItem.java** and **MenuAdapter.java** in the same directory where the activity files reside. Copy **list_item.xml** in **res/​layout**. For this task, you will have to unpack the lab archive and copy the three files into the current project. Copy **MyMenuItem.java** and **MenuAdapter.java** in the same directory where the activity files reside. Copy **list_item.xml** in **res/​layout**.
Line 103: Line 100:
 </​code>​ </​code>​
  
-=== Task 3 - Data Storage ===+=== Task 3 - Data Storage ​(3p) ===
  
 We want to provide a method for persistently storing menu items within the application,​ while also allowing the user to add new ones. For this, modify the behavior of the first EditText (from the first activity) such that its content will not be sent through the intent to the second activity. We will use it for adding the name of a new item. Add other two EditText elements, one for the description and the other one for the price of new items. Also add a new Button, with the "​Add"​ label. When the user clicks it, we want to take the contents of the three EditText elements and add a new item to a database. The second activity will query this database in order to list available items. We want to provide a method for persistently storing menu items within the application,​ while also allowing the user to add new ones. For this, modify the behavior of the first EditText (from the first activity) such that its content will not be sent through the intent to the second activity. We will use it for adding the name of a new item. Add other two EditText elements, one for the description and the other one for the price of new items. Also add a new Button, with the "​Add"​ label. When the user clicks it, we want to take the contents of the three EditText elements and add a new item to a database. The second activity will query this database in order to list available items.
Line 245: Line 242:
 </​code>​ </​code>​
  
-=== Bonus: Task 4 - Using Fragments === 
- 
-Having saved items in the database, we want to add a search feature. For this, the first step is to add a new EditText and a Button to the main Activity. Upon clicking the Button, the text from the EditText element should be sent to a newly created Activity which will use it as the name of an item for which to search within the database. Searching within the database can be done using the query method of the previously created ContentProvider and passing the selection and selectionArgs arguments: 
- 
-<​code>​ 
-String[] columns = {"​name",​ "​desc",​ "​price"​};​ 
-Cursor cursor = getContentResolver().query(Uri.parse(ItemsProvider.tableUri),​ columns, 
-                                           "​name = ?", new String[] {name}, null); 
-</​code>​ 
- 
-If the cursor does not return any results, the Activity should display a fragment with a message informing the user that no results could be found. ​ 
- 
-If the cursor returns a result, the Activity should display a fragment which shows, besides the given item name, its description and price (obtained from the cursor). 
- 
-When extending the Fragment class, the method which should be overriden is **onCreateView**:​ 
- 
-<​code>​ 
-public class EmptyFragment extends Fragment { 
-    ​ 
-    @Override 
-    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
-        View rootView = inflater.inflate(R.layout.fragment_layout_xml,​ container, false); 
-        // you can get a reference to the views within the layout 
-        // by calling rootView.findViewById(R.id.view_id);​ 
-        return rootView; 
-    } 
-} 
-</​code>​ 
- 
-In order to add a fragment to the newly created Activity, first make sure that you add an //​android:​id//​ to the root ViewGroup of the Activity'​s layout. Then, when you want to make a new fragment that will occupy the entire space of the Activity'​s UI, you can use the FragmentManager and do a new FragmentTransaction:​ 
- 
-<​code>​ 
-getFragmentManager() 
-    .beginTransaction() 
-    .replace(R.id.activity_root_view_group,​ new MyFragment()) 
-    .commit(); 
-</​code>​ 
- 
-The most easy way in which you can pass data from an Activity to a Fragment would be to create a constructor for the Fragment with the required data. However, we strongly recommend against doing so since if you put the Activity with the Fragment still drawn in the background and then re-enter the app (using the recent apps for example), the framework will try to call the Fragment'​s default constructor (with no parameters) so your data will not be available. As such, the recommended way in which you should pass data to a Fragment is using a Bundle object. 
- 
-From the Activity, after creating a new Fragment instance create a new Bundle and set the needed data within the Bundle: 
-<​code>​ 
-Fragment f = new MyFragment();​ 
- 
-Bundle b = new Bundle(); 
-b.putString("​key",​ value); 
- 
-f.setArguments(b);​ 
-</​code>​ 
- 
-Then, in the Fragment'​s onCreateView method you can access data stored in the Bundle: 
-<​code>​ 
-@Override 
-public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
-    ... 
-    Bundle b = getArguments();​ 
-    String value = b.getString("​key"​);​ 
-    ... 
-} 
-</​code>​ 
osp/lectures/lecture-internals.1475656807.txt.gz · Last modified: 2016/10/05 11:40 by laura.gheorghe
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0