java - 单击列表项后显示图像

标签 java android imageview listactivity

所以我搜索了又搜索,但没有找到像我正在寻找的东西。我有几个 ListActivities,我可以单击一个并转到下一个列表和下一个列表等...我想要的是,当我单击相应的列表项时,我希望它显示 res 文件夹中的图像。我整个下午都被困在这个问题上,但我找不到任何有效的方法。任何帮助都会很棒。示例 当选择 CoverSheet 时,它会检索 cover_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); 
        } 
    } 
} 

这是我创建的类(class)

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 布局,名为 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>

下面是我的迷你节

<activity android:name=".activity.CoverSheet"  
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> 
</application>
</manifest>

我误解了你之前所说的内容,对此我表示歉意,我认为我开始理解它......我想。

下面是我尝试运行代码时出现的 logcat 错误:

05-18 17:15:26.221: E/AndroidRuntime(618): 致命异常:main 05-18 17:15:26.221:E/AndroidRuntime(618):java.lang.ArrayIndexOutOfBoundsException:长度= 1;索引=3 05-18 17:15:26.221:E/AndroidRuntime(618):在protocols.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: E/AndroidRuntime(618): 在 android.os.Handler.handleCallback(Handler.java:605) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 android.os.Handler.dispatchMessage(Handler.java:92) 05-18 17:15:26.221:E/AndroidRuntime(618):在android.os.Looper.loop(Looper.java:137) 05-18 17:15:26.221:E/AndroidRuntime(618):在android.app.ActivityThread.main(ActivityThread.java:4424) 05-18 17:15:26.221:E/AndroidRuntime(618):在java.lang.reflect.Method.invokeNative( native 方法) 05-18 17:15:26.221:E/AndroidRuntime(618):在java.lang.reflect.Method.invoke(Method.java:511) 05-18 17:15:26.221: E/AndroidRuntime(618): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-18 17:15:26.221:E/AndroidRuntime(618):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-18 17:15:26.221: E/AndroidRuntime(618): at dalvik.system.NativeStart.main( native 方法)

最佳答案

我假设您正在显示名称列在 classes 中的 Activity 中的图像。多变的。

确保列出的所有类也包含在 androidManifest.xml 中具有适当的<activity />标签并带有正确的包声明!

您使用大小写变量和包名称具有误导性,但我认为您的应用程序包是 protocols.NashCountry ,所以 list 中应该有类似这样的一行:

<activity android:name=".activity.CoverSheet" android:label="@string/app_name" />

对于您要调用的每个 Activity 类。

您还应该尝试更改

e.printStackTrace();

进入

Log.e("SAMPLE", "ClassNotFoundException ", e);

编辑:为了让您的后续 Activity 全屏显示,您需要设置android:theme list 中的属性:

<activity android:name=".activity.CoverSheet" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />
<activity android:name=".activity.TreatmentProtocols" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" />

下面的代码展示了如何处理Intent附加内容:

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.coversheet, R.drawable.img2, R.drawable.img3, 
        R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(Menu.this, 
                android.R.layout.simple_list_item_1, classes));
    }

    @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);
        }
    }
}

最后,onCreate类中的方法应根据 extra 显示正确的图像。提供给他们:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_img);
    final int imgResource = getIntent().getIntExtra(Menu.EXTRA_PICTURE, 0);
    final ImageView img = (ImageView)findViewById(R.id.img);
    img.setImageResource(imgResource);
}

请注意,如果您只需要根据列表项点击显示不同的图像,则不需要单独的 Activity ,也不必按名称访问它们:您应该使用单个 Activity ,并通过启动 Intent 时将图像的 id 作为额外参数。

编辑:您的CoverSheet Activity 类应该如下所示:

public class CoverSheet extends Activity
{
    @Override
    protected void onCreate(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);
    }
}

很简单,无需扩展ListActivity ,因为您不想处理那里的任何列表,所以您只想显示图片。

其他变量对于这种情况也是没有用的!

编辑 2:您的 onListItemClick方法也应该变得更简单!不需要动态检索类,因此可以完全删除 classes来自 Menu 的数组变量 Activity 也是如此。

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
    super.onListItemClick(l, v, position, id);
    try
    {
        Intent firstIntent = new Intent(Menu.this, CoverSheet.class);
        firstIntent.putExtra(EXTRA_PICTURE, images[position]);
        startActivity(firstIntent);
    }
    catch (Exception e)
    {
        Log.e("SAMPLE", "Exception ", e);
    }
}

关于java - 单击列表项后显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10576918/

相关文章:

java - 安卓 :how to fixed size of image view programatically?

java - 使用 spring 将少数服务重定向到 https

java - 如何断言断言的数量(或强制所有成员都已在断言中进行测试的其他方式)?

Android:来自 Android Box Remote 的按键事件

android: layout: 如何让我的对话框用 XML 填满屏幕

在 targetSdkVersion 8 上运行 AsyncTask 时 Android 崩溃

java - 使用自定义 key 和 IV 在 Android 和 .Net 中进行 AES 128 加密

java - JPA - 如何使用实体继承中的 criteriaBuilder.construct 填充 DTO

java - 无法保存 Eclipse .java 和 .xml 文件中的更改

android - 从 Assets 中获取位图,然后将其加载到后台