将字符串数组传递给另一个 activity 以与离子一起使用
Passing Array of Strings to another activity to use with ion
我有 ListView
并且我使用 ion 库在其中的 rows
中加载图像,当我点击任何 row
时,我会在row
喜欢 TextView
并使用 Intent
在 activity 中显示
发件人Activity
// 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);
接收者Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
title = extras.getString(NewsAdapter.TITLE);
article = extras.getString(NewsAdapter.ARTICLE);
}
现在我在 ListView
中的每个 row
中都有不同的 ImageView
,我想在 activity 中显示该图像,并使用 ion 库,所以我有 String
的 Array
命名为 thumbUrl
包含我想在 row
和 activity
中显示的 urls
。
发件人中的离子 Activity
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);
所以我尝试使用 Bundle
和 Intent
发送 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);
并像这样接收它:
接收器中的离子 Activity
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.access0(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)
那么现在该怎么办?
对于这个麻烦的问题很抱歉,但我只是一个初学者,这是我第一次处理这样的应用程序。
您应该传递与被点击行相关的图像的 url。不要传递整个数组:
//In the Sender:
passData.putExtra(IMG, thumbUrl[position]);
//In the Receiver
String img = extras.getString(NewsAdapter.IMG);
我有 ListView
并且我使用 ion 库在其中的 rows
中加载图像,当我点击任何 row
时,我会在row
喜欢 TextView
并使用 Intent
发件人Activity
// 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);
接收者Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
title = extras.getString(NewsAdapter.TITLE);
article = extras.getString(NewsAdapter.ARTICLE);
}
现在我在 ListView
中的每个 row
中都有不同的 ImageView
,我想在 activity 中显示该图像,并使用 ion 库,所以我有 String
的 Array
命名为 thumbUrl
包含我想在 row
和 activity
中显示的 urls
。
发件人中的离子 Activity
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);
所以我尝试使用 Bundle
和 Intent
发送 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);
并像这样接收它:
接收器中的离子 Activity
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.access0(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)
那么现在该怎么办?
对于这个麻烦的问题很抱歉,但我只是一个初学者,这是我第一次处理这样的应用程序。
您应该传递与被点击行相关的图像的 url。不要传递整个数组:
//In the Sender:
passData.putExtra(IMG, thumbUrl[position]);
//In the Receiver
String img = extras.getString(NewsAdapter.IMG);