So i’ve searched and searched and I’ve found nothing like I’m looking. I have several ListActivities and I can click one and go to the next list and next list etc… What I want is when i click on the appropriate list item i want it to display an image from the res folder. I have been stuck on this all afternoon and nothing is working that i can find. Any help would be great. Example When CoverSheet is selected it retrieves cover_sheet.png.
Thanks James
The entry point of the application: Menu.java
public class Menu extends ListActivity
{
public static final String EXTRA_PICTURE = "extra.picture.toshow";
String classes[] = { "CoverSheet",// "TreatmentProtocols", "EMSProcedures",
// "DrugList", "EMSPolicies", "EMSTriageAndDestinationPlan",
// "Appendices"
};
//in this array you store the drawable resource ids to show.
//you can change it to hold urls or other identifiers.
int images[] = { R.drawable.cover_sheet, };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,
android.R.layout.simple_list_item_1, classes));
}
protected void onCreate1(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
final int imgResource = getIntent().getIntExtra(Menu.EXTRA_PICTURE, 0);
final ImageView img = (ImageView)findViewById(R.id.img);
img.setImageResource(imgResource); }
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String Menu = classes[position];
try
{
Class firstClass = Class.forName("protocols.NashCounty." + Menu);
Intent firstIntent = new Intent(Menu.this, firstClass);
firstIntent.putExtra(EXTRA_PICTURE, images[position]);
startActivity(firstIntent);
}
catch (ClassNotFoundException e)
{
Log.e("SAMPLE", "ClassNotFoundException ", e);
}
catch (Exception e)
{
Log.e("SAMPLE", "Exception ", e);
}
}
}
This is my Class i’ve created
public class CoverSheet extends ListActivity
{
public static final String EXTRA_PICTURE = "extra.picture.toshow";
String classes[] = { "CoverSheet", };
// in this array you store the drawable resource ids to show.
// you can change it to hold urls or other identifiers.
int images[] = { R.drawable.cover_sheet };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
final int imgResource = getIntent().getIntExtra(
CoverSheet.EXTRA_PICTURE, 0);
final ImageView img = (ImageView) findViewById(R.id.img);
img.setImageResource(imgResource);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String coversheet = classes[position];
try
{
Class firstClass = Class.forName("protocols.NashCounty."+classes);
Intent firstIntent = new Intent(CoverSheet.this, firstClass);
firstIntent.putExtra(EXTRA_PICTURE, images[position]);
startActivity(firstIntent);
}
catch (ClassNotFoundException e)
{
Log.e("SAMPLE", "ClassNotFoundException ", e);
}
catch (Exception e)
{
Log.e("SAMPLE", "Exception ", e);
}
}
}
Below is my xml layout l named imageview
<?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">
<ImageView android:id="@+id/img"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/cover_sheet" />
</LinearLayout>
and below is my minifest
<activity android:name=".activity.CoverSheet"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
</application>
</manifest>
i was misunderstanding what you were saying earlier and i apologize for that i think im starting to undestand it…i think.
below is my logcat error when trying to run the code:
05-18 17:15:26.221: E/AndroidRuntime(618): FATAL EXCEPTION: main
05-18 17:15:26.221: E/AndroidRuntime(618): java.lang.ArrayIndexOutOfBoundsException: length=1; index=3
05-18 17:15:26.221: E/AndroidRuntime(618): at protocols.NashCounty.Menu.onListItemClick(Menu.java:40)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.widget.AbsListView$1.run(AbsListView.java:3168)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.os.Handler.handleCallback(Handler.java:605)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.os.Handler.dispatchMessage(Handler.java:92)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.os.Looper.loop(Looper.java:137)
05-18 17:15:26.221: E/AndroidRuntime(618): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-18 17:15:26.221: E/AndroidRuntime(618): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 17:15:26.221: E/AndroidRuntime(618): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 17:15:26.221: E/AndroidRuntime(618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 17:15:26.221: E/AndroidRuntime(618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 17:15:26.221: E/AndroidRuntime(618): at dalvik.system.NativeStart.main(Native Method)
I assume you are showing images in the activities whose names are listed inside the
classesvariable.Make sure all the classes listed there are also included in the
androidManifest.xmlwith a proper<activity />tag and with the correct package declaration!Your use of casing the variables and package names is misleading, but I think your application package is
protocols.NashCountry, so inside the manifest there should be a line similar to this:for each activity class you want to call.
You should also try to change the
into
Edit: To make your subsequent activities to be displayed in full screen, you need to set
android:themeattribute in your manifest:the code below shows how to deal with
Intentextras:And finally, the
onCreatemethods in your classes should display the proper image based on theextraprovided to them:Please note, that if you just need to display different images based on list item clicks, you don’t need separate activities, and you don’t have to access them by name: you should use a single activity, and pass the id of the image to it as an extra parameter when starting the intent.
Edit: Your
CoverSheetactivity class should look like this:It is very simple, no need to extend
ListActivity, since you don’t want to deal with any list there, you just want to display a picture.Other variables are also useless for this scenario!
Edit 2: Your
onListItemClickmethod should get simpler too! There is no need for dynamically retrieve classes, so you can completely remove theclassesarray variable from yourMenuactivity as well.