In this part of my Android Development Tutorial, I cover numerous topics. This tutorial started in part 18 of this series and you should start watching there to understand everything.
I cover the following : FragmentManager, Creating list items for a ListView, ListFragment, Android Adapters, ArrayAdapter, Generating items from an ArrayList for a ListView and more. All of the code follows the video, but it is also available as a complete package here.
If you like videos like this, it helps to tell Google+ with a click here [googleplusone]
Code From the Video
list_item_contact.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <CheckBox android:id="@+id/contact_contacted_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_alignParentRight="true" android:padding="4dp" /> <TextView android:id="@+id/contact_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@id/contact_contacted_checkbox" android:padding="4dp" android:text="@string/list_item_contact_name" android:textStyle="bold" /> <TextView android:id="@+id/contact_street" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/contact_name" android:layout_toLeftOf="@id/contact_contacted_checkbox" android:padding="4dp" android:text="@string/list_item_contact_street" /> </RelativeLayout>
ContactListActivity.java
package com.newthinktank.censusapp; //We will use the android.support.v4.app.Fragment //support library so our app runs on older versions //of Android import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; //Change Activity to FragmentActivity // The FragmentManager ads Fragments to an Activity's view public class ContactListActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_census_app); FragmentManager fragManager = getSupportFragmentManager(); // Check if the FragmentManager knows about the Fragment // id we refer to Fragment theFragment = fragManager.findFragmentById(R.id.fragmentContainer); // Check if the Fragment was found if(theFragment == null){ // If the Fragment wasn't found then we must create it // We change this from ContactFragment, which we used // in CensusApp theFragment = new FragmentContactList(); // Creates and commits the Fragment transaction // Fragment transactions add, attach, detach, replace // and remove Fragments. // add() gets the location to place the Fragment into and // the Fragment itself. fragManager.beginTransaction() .add(R.id.fragmentContainer, theFragment) .commit(); } } }
FragmentContactList.java
package com.newthinktank.censusapp; import java.util.ArrayList; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.TextView; // The ListFragment displays a list of items in a // ListView, by binding to our ArrayList using an // ArrayAdapter in this situation. public class FragmentContactList extends ListFragment { // Stores the list of Contacts private ArrayList<Contact> contactList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Change the title for the current Activity getActivity().setTitle(R.string.fragment_contact_list_title); // Get the ArrayList from AllContacts contactList = AllContacts.get(getActivity()).getContactList(); ContactAdapter contactAdapter = new ContactAdapter(contactList); // Provides the data for the ListView by setting the Adapter setListAdapter(contactAdapter); } private class ContactAdapter extends ArrayAdapter<Contact> { public ContactAdapter(ArrayList<Contact> contacts) { // An Adapter acts as a bridge between an AdapterView and the // data for that view. The Adapter also makes a View for each // item in the data set. (Each list item in our ListView) // The constructor gets a Context so it so it can use the // resource being the simple_list_item and the ArrayList // android.R.layout.simple_list_item_1 is a predefined // layout provided by Android that stands in as a default super(getActivity(), android.R.layout.simple_list_item_1, contacts); } // getView is called each time it needs to display a new list item // on the screen because of scrolling for example. // The Adapter is asked for the new list row and getView provides // it. // position represents the position in the Array from which we will // be pulling data. // convertView is a pre-created list item that will be reconfigured // in the code that follows. // ViewGroup is our ListView @Override public View getView(int position, View convertView, ViewGroup parent) { // Check if this is a recycled list item and if not we inflate it if(convertView == null){ convertView = getActivity().getLayoutInflater() .inflate(R.layout.list_item_contact, null); } // Find the right data to put in the list item Contact theContact = getItem(position); // Put the right data into the right components TextView contactNameTextView = (TextView)convertView.findViewById(R.id.contact_name); contactNameTextView.setText(theContact.getName()); TextView streetTextView = (TextView)convertView.findViewById(R.id.contact_street); streetTextView.setText(theContact.getStreetAddress()); CheckBox contactedCheckBox = (CheckBox)convertView.findViewById(R.id.contact_contacted_checkbox); contactedCheckBox.setChecked(theContact.getContacted()); // Return the finished list item for display return convertView; } } }
activity_census_app.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- This will be the container for our fragments --> </FrameLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Census App</string> <string name="action_settings">Settings</string> <string name="contact_name_hint">Enter Name</string> <string name="contact_phone_hint">Enter Phone Number</string> <string name="contact_street_hint">Enter Street</string> <string name="contact_city_hint">Enter City</string> <!-- NEW --> <string name="contact_contacted_checkbox">Contacted</string> <string name="fragment_contact_list_title">Contacts</string> <string name="list_item_contact_name">Name</string> <string name="list_item_contact_street">Street</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.newthinktank.censusapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <!-- NEW --> <activity android:name="com.newthinktank.censusapp.ContactListActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- END OF NEW --> <activity android:name="com.newthinktank.censusapp.CensusApp" android:label="@string/app_name" > <!-- <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> --> </activity> </application> </manifest>
Tnx a Bunch,
Looking forward to the final app 🙂
There are many many more apps coming 🙂
Could u please tell in advance which topics u will be covering in the following tutorials? Would be nice if u could do that…
Really great job though ….. your tutorials are my main source of learning apart from the android developers site itself
Thanking you
Thank you 🙂 I’ll see if I can narrow them down in the next video. Thank you for the input.
And is there any way u could do a few tutorials on sync adapters and GCM and multi-threading later on …. would be great
Yes I will cover those topics ASAP
Hey, Derek! Nice tutorial.. but I seem to have trouble with the app.. It loads the ContactListActivity but it only shows one contact with the name and 2 more rows without a name or street.. but it does have a checkbox.. what could be the problem? 😕
Hey, Did you try importing and running the complete package I have here. If that doesn’t run I’ll take a further look. If it does, check your code using diffnow.com.