Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleCursorAdapter on a ViewPager

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)
like image 290
Alex Pinheiro Avatar asked Nov 04 '22 07:11

Alex Pinheiro


1 Answers

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:

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

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:

View v = inflater.inflate(R.layout.layout_dois, null);
listView = (ListView) v.findViewById(R.id.listView1);
like image 106
Alex Curran Avatar answered Nov 18 '22 15:11

Alex Curran