1

Hello I am making music player and in main activity i have 3 fragments and below fragment i have a linear layout which will show name and image of current song.

now problem is that in my 1st fragment list view is not getting populated because getview is not getting called.

logcat is showing no errors but its not getting populated.

MainActivity.java

package com.example.scrolltabs;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.example.scrolltabs.FragmentA;
import com.example.scrolltabs.FragmentB;
import com.example.scrolltabs.FragmentC;


public class MainActivity extends FragmentActivity {

    ViewPager viewpager=null;

    static ArrayList<String> data ;
    static ArrayList<String> artist ;
    static ArrayList<String> album ;
    static ArrayList<String> title ;
    static ArrayList<String> displayname ;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_main);

        data = new ArrayList<String>();
        artist = new ArrayList<String>();
        album = new ArrayList<String>();
        title = new ArrayList<String>();
        displayname = new ArrayList<String>();


        getMyCursor();

        viewpager=(ViewPager) findViewById(R.id.pager);

        FragmentManager fm=getSupportFragmentManager();

        viewpager.setAdapter(new MyAdapter(fm));
    }

    public static ArrayList<String> getDataArrayList()
    {
        return data;
    }

    public static ArrayList<String> getArtistArrayList()
    {
        return artist;
    }

    public static ArrayList<String> getAlbumArrayList()
    {
        return album;
    }

    public static ArrayList<String> getTitleArrayList()
    {
        return title;
    }

    public static ArrayList<String> getDisplayNameArrayList()
    {
        return displayname;
    }


    public Cursor getMyCursor()
    {
        Log.v("JAY","sav pela");

        /*  String[] columns = { android.provider.MediaStore.Audio.Albums._ID,
                android.provider.MediaStore.Audio.Albums.ALBUM };

        Cursor cursor = getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, columns, null,
                null, null);

        if (cursor.moveToFirst()) {
            do {



        String[] column = { MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.MIME_TYPE,
                MediaStore.Audio.Media.ALBUM,
                MediaStore.Audio.Media.ARTIST,
        };

        String where = android.provider.MediaStore.Audio.Media.ALBUM + "=?";



        String orderBy = android.provider.MediaStore.Audio.Media.TITLE;

        Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                column,null,null, orderBy);

        int songcount=0;
        if (cursor.moveToFirst()) {
            do {
                songcount++;

                String s_title =cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                title.add(s_title);

                String s_displayname=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                displayname.add(s_displayname);

                String s_data=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                data.add(s_data);

                String s_album=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                album.add(s_album);

                String s_artist=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                artist.add(s_artist);




            } while (cursor.moveToNext());
        }

        Log.v("JAY",songcount+"");

        return cursor;
    }



}

class MyAdapter extends FragmentStatePagerAdapter
{

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        Fragment fragment=null;

        if(arg0==0)
        {
            fragment= new FragmentA();
        }
        else if(arg0==1)
        {
            fragment=new FragmentB();
        }
        else if(arg0==2)
        {
            fragment=new FragmentC();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(position==0)
        {
            return "All Songs";
        }
        else if(position==1)
        {
            return "Tab2"; 
        }
        else if(position==2)
        {
            return "Tab3";
        }
        return null;
    }
}

activity_main.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

     >

   <android.support.v4.view.ViewPager 
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/pager"
        android:layout_weight="2"
        >

    <android.support.v4.view.PagerTitleStrip
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="#000000"
        android:id="@+id/title"
        android:layout_gravity="top"
        android:paddingTop="20dp"
        android:paddingBottom="4dp"
        android:textColor="#FFFFFF"

       >

    </android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:background="#5edfe5"
        android:clickable="true"
        android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:contentDescription="Albumart"
        android:src="@android:drawable/ic_media_play" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>

</LinearLayout>

FragmentA.java

package com.example.scrolltabs;

import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentA extends ListFragment {

    View view=null;



    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootview = inflater.inflate(R.layout.fragmentlayout1,container,false);
        view=rootview;
        return rootview;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        Log.v("JAY","onactivitycreated");

        ListView listview = (ListView) view.findViewById(android.R.id.list) ;

        MyListAdapter adapter = new MyListAdapter(getActivity().getApplicationContext(),R.layout.fragmentlayout1);

        listview.setAdapter(adapter);

    }

}

class MyListAdapter extends ArrayAdapter<String>{

    ArrayList<String> data ;
    ArrayList<String> artist ;
    ArrayList<String> album ;
    ArrayList<String> title ;
    ArrayList<String> displayname ;

    public MyListAdapter(Context applicationContext, int fragmentlayout1) {
        // TODO Auto-generated constructor stub
        super(applicationContext, fragmentlayout1);
        Log.v("JAY","in adapter");
        data=com.example.scrolltabs.MainActivity.getDataArrayList();
        artist=com.example.scrolltabs.MainActivity.getArtistArrayList();
        album=com.example.scrolltabs.MainActivity.getAlbumArrayList();
        title=com.example.scrolltabs.MainActivity.getTitleArrayList();
        displayname=com.example.scrolltabs.MainActivity.getDisplayNameArrayList();



    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.v("JAY","in get view");
        LayoutInflater inf = (LayoutInflater) convertView.getContext().getSystemService(convertView.getContext().LAYOUT_INFLATER_SERVICE);
        View row = inf.inflate(R.layout.peritem, parent, false);

        TextView title_tv = (TextView) row.findViewById(R.id.titletextview);
        TextView artist_tv = (TextView) row.findViewById(R.id.artisttextview);

        title_tv.setText(title.get(position));
        artist_tv.setText(artist.get(position));



        return row;

    }

}

fragmentlayout1.xml fragment 1's layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>

</LinearLayout>

per_item.xml (layout for each item in list in fragment 1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titletextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/artisttextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>
ldeathnote
  • 11
  • 4
  • Log.v("JAY", cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))+ " - " + cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))); is not executing – sunil Aug 19 '14 at 12:54
  • getCount() returns zero. The number you return in getCount() is the times the getView() will be called and i thing ArrayList data ; ArrayList artist ; ArrayList album ; ArrayList title ; ArrayList displayname ; are having zero size.does songs are present in your phone ? – sunil Aug 19 '14 at 12:57
  • see this also http://stackoverflow.com/q/16250292/3222876 – sunil Aug 19 '14 at 13:01

1 Answers1

0

When you initialize MyListAdapter, you are using this constructor. Second argument is resource id:

The resource ID for a layout file containing a TextView to use when instantiating views. But you don't need this, because you inflate item view manually:

View row = inf.inflate(R.layout.peritem, parent, false); 

Try to use this:

super(context, 0, data);

Upd. Try this in MyListAdapter constructor:

super(context, 0);

Upd. 2 try setListAdapter(adapter) instead of listview.setAdapter(adapter);

Upd. 3 From Android docs:

You must use ListFragment.setListAdapter() to associate the list with an adapter. Do not directly call ListView.setAdapter() or else important initialization will be skipped.