【问题标题】:Display Image after listitemclicked单击列表项后显示图像
【发布时间】:2012-05-14 02:31:27
【问题描述】:

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

这是我创建的班级

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: length=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 Method) 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 Method)

【问题讨论】:

  • 我没有看到任何关于在此处显示图像的代码...
  • 你能解释一下你想在哪里显示图像。
  • 我希望在您单击列表数组中的参数后将图片拉出全屏。例如,当我单击封面时,它会全屏拉出图片封面

标签: java android imageview listactivity


【解决方案1】:

我假设您正在显示名称列在 classes 变量中的活动中的图像。

确保列出的所有类也包含在 androidManifest.xml 中,并带有正确的 &lt;activity /&gt;tag 和正确的包声明!

你对变量和包名的使用是误导性的,但我认为你的应用程序包是protocols.NashCountry,所以在清单里面应该有类似这样的一行:

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

对于您要调用的每个活动类。

你也应该尝试改变

e.printStackTrace();

进入

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

编辑:为了让你的后续活动全屏显示,你需要在你的manifest中设置android:theme属性:

<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 extras:

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

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

编辑:您的 CoverSheet 活动类应如下所示:

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 方法也应该变得更简单!无需动态检索类,因此您也可以从 Menu 活动中完全删除 classes 数组变量。

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

【讨论】:

  • 我创建的每个班级都有一个活动。数组中的每个参数也需要一个吗?我会尽快改变另一个。请参阅我上面的评论,看看它是否能更好地阐明我在寻找什么。再次感谢您的帮助
  • startActivity(firstIntent); 调用表明,您正在尝试以 Class.forName("protocols.NashCounty."+ Menu) 的名称启动类的新 Activity 扩展。这里Menu 是一个局部变量,它包含来自classes[] 数组的值,所以是的,您也应该定义这些类:protocols.NashCountry.CoverSheetprotocols.NashCountry.TreatmentProtocols、...
  • 谢谢,我想我知道了。 But how do I get it to pull up the picture associated when the value within the array is selected?很抱歉没有完全理解。
  • 您可以将extras 传递给您的IntentfirstIntent.putExtra("my.image.location", "the location of the image you want to show"); 然后在您刚刚开始的活动中,您通过final String imageLocation = getIntent().getStringExtra("my.image.location"); 提取extra
  • 好的,谢谢,我会告诉你进展如何。也感谢您的快速响应!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 2019-03-05
相关资源
最近更新 更多