所以我搜索和搜索,我没有找到任何像我正在寻找的东西。我有几个 ListActivities,我可以单击一个并转到下一个列表和下一个列表等...我想要的是当我单击相应的列表项时,我希望它显示 res 文件夹中的图像。我整个下午都被困在这个问题上,我找不到任何工作。任何帮助都会很棒。示例何时选择覆盖物,它检索封面_sheet.png。谢谢詹姆斯
应用程序的入口点: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);
}
}
}
这是我创建的班级
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);
}
}
}
下面是我的 xml 布局 l 名为 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>
下面是我的minifest
<activity android:name=".activity.CoverSheet"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
</application>
</manifest>
我误解了你之前说的内容,对此我深表歉意,我想我开始不理解它了……我想。
以下是我尝试运行代码时出现的 logcat 错误:
05-18 17:15:26.221:E/AndroidRuntime(618):致命异常:主要 05-18 17:15:26.221:E/AndroidRuntime(618):java.lang.ArrayIndexOutOfBoundsException:长度=1;index=3 05-18 17:15:26.221: E/AndroidRuntime(618): 在协议.NashCounty.Menu.onListItemClick(Menu.java:40) 05-18 17:15:26.221: E/AndroidRuntime(618):在 android.app.ListActivity$2.onItemClick(ListActivity.java:319) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AdapterView.performItemClick(AdapterView.java:292) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AbsListView.performItemClick(AbsListView.java:1058) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AbsListView $PerformClick.run(AbsListView.java:2514) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.widget.AbsListView$1.run(AbsListView.java:3168) 05-18 17:15: 26.221: