首页 > 解决方案 > 如何从从以前的活动中获取内容的数组从列表转换为字符串

问题描述

我试图在单击后转换列表中的特定项目,它转换为字符串,然后将该信息发送回 MainActivity。将信息转换为字符串时遇到问题。

 namespace GPS2
{
[Activity(Label = "@string/GPSHistory")]
public class GPSHistory : ListActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        // Create your application here
        // Draw controls
        // SetContentView(Resource.Layout.GPSHistoryLayout);



        var strGPSAnswer = Intent.Extras.GetStringArrayList("Answer") ?? new string[0];
        ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, strGPSAnswer);

        ListView.TextFilterEnabled = true;

        ListView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args)
        {

            //Confirmation that it works.
            Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).Show();

            //Prepare string and send back to first page.
            var i = new Intent(this, typeof(MainActivity));
            i.PutExtra("string", strGPSAnswer);
            StartActivity(i);

        };
    }
}
}

标签: c#androidxamarinandroid-5.0-lollipop

解决方案


根据 Android Intent 文档 intent extras

putStringArrayListExtra

名称应该是:

向意图添加扩展数据。名称必须包含包前缀,例如应用程序 com.android.contacts 将使用类似“com.android.contacts.ShowAll”的名称

在您的代码中,看起来“答案”是不包括包前缀的键。

我本可以在评论中添加这个,但没有足够的积分。


推荐阅读