Launch the Stub Activity on List Click
Now, we need to arrange to display this activity when the user clicks on a LunchList list item, instead of flipping to the original detail form tab in LunchList.
First, we need to add DetailForm to the AndroidManifest.xml file, so it is recognized by the system as being an available activity. Change the manifest to look like the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="apt.tutorial"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false"
/>
<application android:label="@string/app_name">
<activity android:name=".LunchList"
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=".DetailForm">
</activity>
</application>
</manifest>
Notice the second <activity> element, referencing the DetailForm class.
Also note that it does not need an <intent-filter>, since we will be launching it ourselves rather than expecting the system to launch it for us. Then, we need to start this activity when the list item is clicked. That is handled by our onListClick listener object. So, replace our current implementation with the following:
private AdapterView.OnItemClickListener onListClick=new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
Intent i=new Intent(LunchList.this, DetailForm.class);
startActivity(i);
}
};
Here we create an Intent that points to our DetailForm and call startActivity() on that Intent. Y ou will need to add an import for android.content.Intent to LunchList.
At this point, you should be able to recompile and reinstall the application.
If you run it and click on an item in the list, it will open up the empty DetailForm. From there, you can click the BACK button to return to the main LunchList activity.
First, we need to add DetailForm to the AndroidManifest.xml file, so it is recognized by the system as being an available activity. Change the manifest to look like the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="apt.tutorial"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false"
/>
<application android:label="@string/app_name">
<activity android:name=".LunchList"
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=".DetailForm">
</activity>
</application>
</manifest>
Notice the second <activity> element, referencing the DetailForm class.
Also note that it does not need an <intent-filter>, since we will be launching it ourselves rather than expecting the system to launch it for us. Then, we need to start this activity when the list item is clicked. That is handled by our onListClick listener object. So, replace our current implementation with the following:
private AdapterView.OnItemClickListener onListClick=new
AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
Intent i=new Intent(LunchList.this, DetailForm.class);
startActivity(i);
}
};
Here we create an Intent that points to our DetailForm and call startActivity() on that Intent. Y ou will need to add an import for android.content.Intent to LunchList.
At this point, you should be able to recompile and reinstall the application.
If you run it and click on an item in the list, it will open up the empty DetailForm. From there, you can click the BACK button to return to the main LunchList activity.
No comments:
Post a Comment