片段中 ViewPager 中的 Listview 适配器 - 如何获取 Activity

Listview adapter in a ViewPager in a fragment - How to get the Activity

我正在尝试使用 CursorLoader 在设备上构建本地文件的音乐 browser/player。我正在使用 Xamarin android,c#。我正在使用 Fragment 中的 ViewPager(不是 FragmentActivity,因为该片段是 Navigation Drawer 的一部分)。

我在使用 listView.Adapter 时遇到了问题,因为我无法获得 Activity。当直接在 activity 中使用此列表视图适配器时,它工作正常(我能够看到歌曲列表),但是当尝试在使用 PagerAdapter 的 ViewPager 中使用它时(所有这些都在片段中).我无法在 listView.Adapter = new SongsAdapter (this,cursor,songsList) 中获取 Activity。您能否指导我以便能够将 activity 传递给适配器,或者让我知道我是否做错了一般情况导致无法实现?

谢谢。感谢您的帮助:)

朱利安

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.Support.V4.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.V4.View;

using Android.Provider;
using Android.Database;
using Android.Net;


namespace project
{
    public class MyDeviceFragment : Fragment
    {
        private SlidingTabScrollView mSlidingTabScrollView;
        private ViewPager mViewPager;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.slidingtabfragment, container, false);
        }

        public override void OnViewCreated(View view, Bundle savedInstanceState)
        {
            var activity = Activity;
            mSlidingTabScrollView = view.FindViewById<SlidingTabScrollView>(Resource.Id.sliding_tabs);
            mViewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
            mViewPager.Adapter = new SamplePagerAdapter();

            mSlidingTabScrollView.ViewPager = mViewPager;
        }

        public class SamplePagerAdapter : PagerAdapter
        {
            List<string> items = new List<string>();

            public SamplePagerAdapter() : base()
            {
                items.Add("PLaylists");
                items.Add("Songs");
                items.Add("Albums");
                items.Add("Artists");
                items.Add("Genres");
            }

            public override int Count
            {
                get { return items.Count; }
            }

            public override bool IsViewFromObject(View view, Java.Lang.Object obj)
            {
                return view == obj;
            }

            public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
            {
                View view = null;

                switch (position) {
                case 1:
                    ListView listView;
                    view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.testinglistactivity, container, false);
                    listView = view.FindViewById<ListView> (Resource.Id.List);

                    List<Song> songsList;

                    var uri = MediaStore.Audio.Media.ExternalContentUri;
                    string[] projection = {
                        MediaStore.Audio.Media.InterfaceConsts.Id,
                        MediaStore.Audio.Media.InterfaceConsts.AlbumId,
                        MediaStore.Audio.Media.InterfaceConsts.Title,
                        MediaStore.Audio.Media.InterfaceConsts.Artist,
                    };
                    var loader = new CursorLoader (container.Context, uri, projection, null, null, null);
                    var cursor = (ICursor)loader.LoadInBackground ();
                    songsList = new List<Song> ();
                    if (cursor.MoveToFirst ()) {
                        do {
                            songsList.Add (new Song {
                                Id = cursor.GetLong (cursor.GetColumnIndex (projection [0])),
                                AlbumId = cursor.GetString (cursor.GetColumnIndex (projection [1])),
                                Title = cursor.GetString (cursor.GetColumnIndex (projection [2])),
                                Artist = cursor.GetString (cursor.GetColumnIndex (projection [3]))
                            });
                        } while (cursor.MoveToNext ());
                    }

                    listView.Adapter = new SongsAdapter (this,cursor,songsList);//***********HERE is the issue*********************
                    break;
                default:
                    view = LayoutInflater.From (container.Context).Inflate (Resource.Layout.pager_item, container, false);
                    int pos = position + 1;
                    TextView txtTitle = view.FindViewById<TextView> (Resource.Id.item_title);

                    txtTitle.Text = pos.ToString ();
                    break;
                }
                container.AddView (view);
                return view;
            }

            public string GetHeaderTitle (int position)
            {
                return items[position];
            }

            public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object obj)
            {
                container.RemoveView((View)obj);
            }
        }
    }
}

开头声明:ParentActivity mParentActivity; 在你的 onCreate() 添加:mParentActivity =(ParentActivity) Activity;

现在您可以使用 mParentActivity 访问您的 parent activity。 对于您的问题,现在您将执行:

listView.Adapter = new SongsAdapter (mParentActivity, cursor, songsList);

我通过执行以下操作解决了问题:

  1. 将 Activity 作为参数传递给: mViewPager.Adapter = new MyDeviceAdapter(Activity);
  2. this.context = 上下文添加到 SamplePager 适配器的构造函数中

    public class SamplePagerAdapter:PagerAdapter { Activity 上下文; 列表项 = new List();

        public SamplePagerAdapter(Activity context) : base()
        {
            this.context = context;
            items.Add("PLaylists");
            items.Add("Songs");
            items.Add("Albums");
            items.Add("Artists");
            items.Add("Genres");
        }
    

    ...

  3. 可选(如果需要,则不是 sur):我确实在另一个文件中分离了 SamplePagerAdapter,在那里我可以使用 Android.App 而不是 Android.Support.V4.App;

你可以添加容器的上下文,它会解决问题:

 listView.Adapter = new SongsAdapter (container.Context,cursor,songsList);