我尝试使用CursorLoader在设备上构建本地文件的音乐浏览器/播放器。我使用的是Xamarin android,c#。我使用的是ViewPager,它位于片段内(不是FragmentActivity,因为片段是导航抽屉的一部分)。
我在使用listView.Adapter时遇到了问题,因为我无法获得该活动,因此会用于显示歌曲。在活动中直接使用此listview适配器时,它可以正常工作(我能够看到歌曲列表),但是当尝试在使用PagerAdapter的ViewPager(在片段中的所有内容)中使用它时。我无法在listView.Adapter = new SongsAdapter( this ,cursor,songsList)中获取活动。您是否可以指导我,以便能够将活动传递给适配器或让我知道我是否做了一般的错误,这使得它无法实现?
谢谢。感谢您的帮助:)
于连
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);
}
}
}
}
答案 0 :(得分:1)
我能够通过以下方式解决问题:
将 this.context = context添加到SamplePager适配器的构造函数
public class SamplePagerAdapter:PagerAdapter { 活动背景; 列表项=新列表();
public SamplePagerAdapter(Activity context) : base()
{
this.context = context;
items.Add("PLaylists");
items.Add("Songs");
items.Add("Albums");
items.Add("Artists");
items.Add("Genres");
}
...
可选(如果需要则不是):我将SamplePagerAdapter与另一个文件分开,我可以使用Android.App而不是Android.Support.V4.App;
答案 1 :(得分:0)
您可以添加容器的上下文,它将解决问题:
listView.Adapter = new SongsAdapter (container.Context,cursor,songsList);