【问题标题】:Passing Array of Strings to another activity to use with ion将字符串数组传递给另一个活动以与 ion 一起使用
【发布时间】:2015-08-20 21:57:41
【问题描述】:

我有ListView 并且我使用ion 库在其中的rows 中加载图像,当我点击任何row 时,我会获取row 上的数据,例如TextView 并显示它在活动中使用Intent

发件人活动

// Keys
public static String TITLE = "title";
public static String ARTICLE = "article";
public static String IMG = "img";


Intent passData = new Intent(getContext(), ArticleScreen.class);
       passData.putExtra(TITLE, tv.getText());
       passData.putExtra(ARTICLE, tv2.getText());
       getContext().startActivity(passData);

接收者活动

Bundle extras = getIntent().getExtras();

    if (extras != null) {

        title = extras.getString(NewsAdapter.TITLE);
        article = extras.getString(NewsAdapter.ARTICLE);

    }

现在我在ListView 中的每个row 中都有不同的ImageView,我想使用ion 库在带有文本的活动中显示该图像,所以我有ArrayString 命名thumbUrl 包含我想在rowactivity 中显示的urls

发送者活动中的离子

ImageView iv = (ImageView) v.findViewById(R.id.mThumb);

    Ion.with(getContext()).load(thumbUrl[position])
            .withBitmap()
            .placeholder(R.drawable.ic_settings_black_24dp)
            .error(R.drawable.ic_info_outline_black_24dp)
            .intoImageView(iv);

所以我尝试使用BundleIntent 像这样发送thumbUrl Array

Bundle b = new Bundle();
       b.putStringArray(IMG, thumbUrl);

       Intent passData = new Intent(getContext(), ArticleScreen.class);
       passData.putExtra(TITLE, tv.getText());
       passData.putExtra(ARTICLE, tv2.getText());
       passData.putExtras(b);
       getContext().startActivity(passData);

并像这样接收它:

受体活动中的离子

private String[] img;

Bundle extras = getIntent().getExtras();

    if (extras != null) {

        title = extras.getString(NewsAdapter.TITLE);
        article = extras.getString(NewsAdapter.ARTICLE);
        img = extras.getStringArray(NewsAdapter.IMG);

    }

    ImageView iv = (ImageView) findViewById(R.id.articleImg);

    Ion.with(this).load(Arrays.toString(img))
            .withBitmap()
            .placeholder(R.drawable.ic_settings_black_24dp)
            .error(R.drawable.ic_info_outline_black_24dp)
            .intoImageView(iv);

但它给了我这个例外

 08-20 23:51:23.841  28725-28725/com.mEmoZz.App W/Bundle﹕ Key img expected String[] but value was a java.lang.Integer.  The default value <null> was returned.
 08-20 23:51:23.843  28725-28725/com.mEmoZz.App W/Bundle﹕ Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String[]
        at android.os.BaseBundle.getStringArray(BaseBundle.java:1258)
        at com.mEmoZz.App.ArticleScreen.onCreate(ArticleScreen.java:36)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
        at android.app.ActivityThread.access$900(ActivityThread.java:154)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5291)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

那么现在该怎么办?

抱歉,我只是一个初学者,这是我第一次处理这样的应用程序。

【问题讨论】:

  • 第二个活动是否需要整个数组?还是该数组中只有一项?
  • 所有我需要的是,当我点击ListView 中的任何row 时,我得到了图像并在另一个activity 中显示它,是的,我每次都需要一个项目指点击的ListView位置。
  • 然后将该项目作为常规字符串传递。不要传递整个数组。
  • 如果你的意思是传递array 中每个元素的位置,我不知道该怎么做

标签: java android arrays android-intent android-ion


【解决方案1】:

您应该传递与被单击的行相关的图像的 url。不要传递整个数组:

//In the Sender:
passData.putExtra(IMG, thumbUrl[position]);

//In the Receiver
String img = extras.getString(NewsAdapter.IMG);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多