在 Xamarin Android Webview 中将 WebHistory 项数组转换为字符串数组

Convert WebHistory Item array to String Array in Xamarin Android Webview

我有一个 activity 应该显示用户访问过的 URL 列表,我使用 WebViewHistory 项目逻辑获取历史记录并使用 ListView 数组适配器将数据填充到我的布局中。除了 WebViewHistory 项是以这种人类无法理解的机器语言显示的。例如,一个列表的历史记录为 p7@5561337。我只需要可用于将 WebViewHistoryItem 数组转换为人类可读字符串数组的代码

  class History : AppCompatActivity 
    {
        ListView listView;
        WebView mywebview;
         protected override void OnCreate(Bundle savedInstanceState)
        {
         mywebview = Internet.webview;
            WebHistoryItem[] webHistories = new WebHistoryItem[size];
             int size = webviewHistory.Size;
            for(int i = 0; i <size; i++)
            {
                webHistories[i] = webviewHistory.GetItemAtIndex(i);
            }
     //Locate listview in Resource Layout
           histView = (ListView)FindViewById<ListView>(Resource.Id.mainlistview);
      //populate webview history item into listview
           histView.Adapter = new ArrayAdapter(this, 
            Android.Resource.Layout.SimpleListItem1,webHistories);
        }
    
    }

我在之前的 Activity 中将 webview 定义为静态和 public 让我在这个 activity 中访问它的实例,我如何将 webhistories 数组转换为 string非常感谢您的帮助

i got something, I can get the Title of the current url via this method string[] my=new string[] { webviewHistory.CurrentItem.Title } ;, Can we reason it out from here?

是的。

Am using this but am still getting the storage pointer of the urls, please help string[] histo = new string[] { webviewHistory.GetItemAtIndex(0).Url.ToString()};

您可以使用List来包含历史记录。我创建了一个名为 VisitHistroy 的模型。并在列表视图中同时显示标题和 url,就像这个 GIF。

这是我的完整代码。

     [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        EditText editText1;
        WebView webView1;
        ListView listView1;
        Button button1;
        public delegate void OnLinkSelectedHandler(string url);
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

             editText1= FindViewById<EditText>(Resource.Id.editText1);
             webView1 = FindViewById<WebView>(Resource.Id.webView1);
             listView1 = FindViewById<ListView>(Resource.Id.listView1);
             button1= FindViewById<Button>(Resource.Id.button1);
            button1.Click += Button1_Click;
           
            webView1.LoadUrl("http://whosebug.com");
            webView1.Settings.JavaScriptEnabled = true;
            webView1.SetWebViewClient(new MyWebviewClient(this.ISVistedSoLink));
        }

        private void Button1_Click(object sender, System.EventArgs e)
        {
            // throw new System.NotImplementedException();

            WebBackForwardList wbfl = webView1.CopyBackForwardList();
            int size = wbfl.Size;
            var webHistories = new WebHistoryItem[wbfl.Size];
            List<VisitHistroy> strs = new List<VisitHistroy>();
          

            for (int i = 0; i < size; i++)
            {
                //  histo = new string[] { wbfl.GetItemAtIndex(0).Url.ToString() };

                strs.Add(new VisitHistroy() { Title = wbfl.GetItemAtIndex(i).Title.ToString(), Url = wbfl.GetItemAtIndex(i).Url.ToString() });
                
            }
            //SimpleAdapter(Context? context, IList<IDictionary<string, object>>? data, int resource, string[]? from, int[]? to);
            listView1.Adapter = new MyAdapter(this, strs);
        }

        public void ISVistedSoLink(string url)
        {
            editText1.Text = url;
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    internal class MyAdapter : BaseAdapter<VisitHistroy>
    {
        private MainActivity mainActivity;
        private List<VisitHistroy> strs;

        public MyAdapter(MainActivity mainActivity, List<VisitHistroy> strs):base()
        {
            this.mainActivity = mainActivity;
            this.strs = strs;
        }

        public override VisitHistroy this[int position] => strs[position];

        public override int Count => strs.Count;

      

        public override long GetItemId(int position)
        {
           return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = strs[position];
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = mainActivity.LayoutInflater.Inflate(Resource.Layout.Itemlayout, null);
            view.FindViewById<TextView>(Resource.Id.itemTitle).Text = item.Title;
            view.FindViewById<TextView>(Resource.Id.itemurl).Text = item.Url;
           
            return view;
        }
    }

    public class VisitHistroy
    {
        public string Title { get; set; }
        public string Url { get; set; }
    }
}

这是自定义项。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textStyle="bold"
        android:text="@string/abc_font_family_title_material"
        android:id="@+id/itemTitle"/>
     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/abc_font_family_title_material"
        android:id="@+id/itemurl"/>
</LinearLayout>

这里是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Get"
        android:id="@+id/button1"/>
    <android.webkit.WebView
        android:layout_width="match_parent"
        android:layout_height="350dip"
        android:id="@+id/webView1"
         />
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="200dip"/>
</LinearLayout>