ViewPager 上的 SimpleCursorAdapter
如何使用 SimpleCursorAdapter 获取 ListView 以在 ViewPager 中显示?单独的寻呼机工作得很好,ListView 在 ListActivity 中工作正常,但我不知道如何使其在 Activity 中工作(也许这就是问题)。
public class Player extends Activity {
private ViewPager pager;
private static int NUMERO_DE_TELAS = 3;
private AdaptadorDeTelas adaptador;
//Para debug apagar no final
final String BABAS = "babas";
ContentResolver mContentResolver;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Adaptador para criar o viewpager
adaptador = new AdaptadorDeTelas();
pager = (ViewPager)findViewById(R.id.pageradapter);
pager.setAdapter(adaptador);
pager.setCurrentItem(1);
}
private class AdaptadorDeTelas extends PagerAdapter{
@Override
public int getCount(){
return NUMERO_DE_TELAS;
}
@Override
public Object instantiateItem(View collection, int position){
View layout;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == 0){
layout = inflater.inflate(R.layout.layout_zero, null);
}else if(position == 1){
layout = inflater.inflate(R.layout.layout_um, null);
}else{
Uri todasMusicas = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//Seleciona as tabelas a serem retornadas do content audio.media
String[] projection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE
};
//Pega todas as músicas no aparelho
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor;
cursor = Player.this.managedQuery(
todasMusicas,
projection,
selection,
null,
MediaStore.Audio.Media.TITLE + " ASC");
String[] from = new String []{MediaStore.Audio.Media.TITLE};
int[] to = new int[] {R.id.textView1};
View v = (View) findViewById(R.layout.layout_dois);
listView = (ListView) findViewById(R.id.listView1);
SimpleCursorAdapter meuAdaptador = new SimpleCursorAdapter(Player.this, R.layout.layout_dois, cursor, from, to);
listView.setAdapter(meuAdaptador);
layout = listView;;
}
((ViewPager)collection).addView((View) layout, 0);
return layout;
}
@Override
public void destroyItem(View collection, int position, Object view){
((ViewPager) collection).removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object){
return view==((View)object);
}
@Override
public void finishUpdate(View arg0){}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1){}
@Override
public Parcelable saveState(){
return null;
}
@Override
public void startUpdate(View arg0){}
}
我正在尝试在 ViewPager 的一个页面中显示 SD 卡中的音乐列表,提前感谢任何帮助我的人。
Ps:抱歉,代码中的注释中的英语和葡萄牙语不好。
这是 LogCat 中显示的错误:
01-15 16:48:41.623: W/dalvikvm(270): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 16:48:41.653: E/AndroidRuntime(270): FATAL EXCEPTION: main
01-15 16:48:41.653: E/AndroidRuntime(270): java.lang.NullPointerException
01-15 16:48:41.653: E/AndroidRuntime(270): at br.com.babas.player.Player$AdaptadorDeTelas.instantiateItem(Player.java:113)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.View.dispatchAttachedToWindow(View.java:6016)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1116)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewRoot.performTraversals(ViewRoot.java:727)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.os.Looper.loop(Looper.java:123)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 16:48:41.653: E/AndroidRuntime(270): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:48:41.653: E/AndroidRuntime(270): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:48:41.653: E/AndroidRuntime(270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:48:41.653: E/AndroidRuntime(270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:48:41.653: E/AndroidRuntime(270): at dalvik.system.NativeStart.main(Native Method)
How do I get a ListView using a SimpleCursorAdapter to show in a ViewPager? The Pager alone works just fine and the ListView works ok in a ListActivity, but I dont know how to make it works in an Activity (maybe it's the problem).
public class Player extends Activity {
private ViewPager pager;
private static int NUMERO_DE_TELAS = 3;
private AdaptadorDeTelas adaptador;
//Para debug apagar no final
final String BABAS = "babas";
ContentResolver mContentResolver;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Adaptador para criar o viewpager
adaptador = new AdaptadorDeTelas();
pager = (ViewPager)findViewById(R.id.pageradapter);
pager.setAdapter(adaptador);
pager.setCurrentItem(1);
}
private class AdaptadorDeTelas extends PagerAdapter{
@Override
public int getCount(){
return NUMERO_DE_TELAS;
}
@Override
public Object instantiateItem(View collection, int position){
View layout;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == 0){
layout = inflater.inflate(R.layout.layout_zero, null);
}else if(position == 1){
layout = inflater.inflate(R.layout.layout_um, null);
}else{
Uri todasMusicas = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//Seleciona as tabelas a serem retornadas do content audio.media
String[] projection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE
};
//Pega todas as músicas no aparelho
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor;
cursor = Player.this.managedQuery(
todasMusicas,
projection,
selection,
null,
MediaStore.Audio.Media.TITLE + " ASC");
String[] from = new String []{MediaStore.Audio.Media.TITLE};
int[] to = new int[] {R.id.textView1};
View v = (View) findViewById(R.layout.layout_dois);
listView = (ListView) findViewById(R.id.listView1);
SimpleCursorAdapter meuAdaptador = new SimpleCursorAdapter(Player.this, R.layout.layout_dois, cursor, from, to);
listView.setAdapter(meuAdaptador);
layout = listView;;
}
((ViewPager)collection).addView((View) layout, 0);
return layout;
}
@Override
public void destroyItem(View collection, int position, Object view){
((ViewPager) collection).removeView((View)view);
}
@Override
public boolean isViewFromObject(View view, Object object){
return view==((View)object);
}
@Override
public void finishUpdate(View arg0){}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1){}
@Override
public Parcelable saveState(){
return null;
}
@Override
public void startUpdate(View arg0){}
}
I'm trying to show a list of the musics in the sd card in one of the pages of the ViewPager, thanks in advance for anyone who helpme out.
Ps: Sorry for the bad english and for the portuguese in the comments in the code.
This is the error that shows in the LogCat:
01-15 16:48:41.623: W/dalvikvm(270): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 16:48:41.653: E/AndroidRuntime(270): FATAL EXCEPTION: main
01-15 16:48:41.653: E/AndroidRuntime(270): java.lang.NullPointerException
01-15 16:48:41.653: E/AndroidRuntime(270): at br.com.babas.player.Player$AdaptadorDeTelas.instantiateItem(Player.java:113)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:321)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.support.v4.view.ViewPager.populate(ViewPager.java:441)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.View.dispatchAttachedToWindow(View.java:6016)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1116)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1121)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewRoot.performTraversals(ViewRoot.java:727)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.os.Looper.loop(Looper.java:123)
01-15 16:48:41.653: E/AndroidRuntime(270): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 16:48:41.653: E/AndroidRuntime(270): at java.lang.reflect.Method.invokeNative(Native Method)
01-15 16:48:41.653: E/AndroidRuntime(270): at java.lang.reflect.Method.invoke(Method.java:521)
01-15 16:48:41.653: E/AndroidRuntime(270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 16:48:41.653: E/AndroidRuntime(270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 16:48:41.653: E/AndroidRuntime(270): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我看到的一个问题是,在引用某个视图时,您得到了错误的 id,因为您向它传递了一个布局标识符:
它下面的列表视图也将为空。我假设这是在“layout_dois”布局中?
看起来你应该将其更改为:
Well, one issue I can see is that you've got the wrong id when referencing some view, as you've passed it a layout identifier:
Your listview just below it will also be null. I'm assuming this is in the "layout_dois" layout?
Kinda looks like you should just change it so that it reads:
它非常简单。
在 xml 中创建一个 listView 对象。
在代码/活动文件中将其实例化为:
ListView myListView = (ListView)findViewById(R.id.YOUR_LIST_VIEW_ID_IN_XML )
现在为其制作一个适配器。
如果您想学习一些关于光标适配器的教程,请检查:
http://android-support-akkilis.blogspot .com/2011/11/simple-cursor-adapter-made-easy.html
现在将相同的适配器设置为您的列表:
myListView.setAdapter(new MyCursorAdapter)。
Its quite simple.
Make a listView object in your xml.
Instantiate it in your code/activity file as:
ListView myListView = (ListView)findViewById(R.id.YOUR_LIST_VIEW_ID_IN_XML)
Now make an adapter for it.
If you wanna take some tutorial for cursor adapter, check:
http://android-support-akkilis.blogspot.com/2011/11/simple-cursor-adapter-made-easy.html
Now set the same adapter to your list as
myListView.setAdapter(new MyCursorAdapter).