gpt4 book ai didi

android - ViewPager 上的 SimpleCursorAdapter

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:31 29 4
gpt4 key购买 nike

如何使用 SimpleCursorAdapter 获取 ListView 以在 ViewPager 中显示? Pager 单独工作得很好,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)

最佳答案

好吧,我看到的一个问题是您在引用某个 View 时得到了错误的 id,因为您已经向它传递了布局标识符:

View v = (View) findViewById(R.layout.layout_dois);

它下方的 ListView 也将为空。我假设这是在“layout_dois”布局中?

看起来你应该把它改成这样:

View v = inflater.inflate(R.layout.layout_dois, null);
listView = (ListView) v.findViewById(R.id.listView1);

关于android - ViewPager 上的 SimpleCursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8863934/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com