Java 片段中的Android ListView不';t显示值

Java 片段中的Android ListView不';t显示值,java,android,Java,Android,我正在制作一个音乐应用程序,其中UI被分割成碎片。 它建立在TabbedActivty项目上。 所以我的问题是: 在MainActivity中,我正在将音乐文件从存储器加载到阵列中 该数组被传递到助手类中。 然后片段加载该数组(该数组也可以工作) 我可以设置适配器,但是listview仍然是空的,即使数组有值。 我尝试了很多代码,但到目前为止没有任何效果。 但我没有收到任何错误消息 我的主要活动是: package needforbleed.com.music; public class Ma

我正在制作一个音乐应用程序,其中UI被分割成碎片。 它建立在TabbedActivty项目上。 所以我的问题是: 在MainActivity中,我正在将音乐文件从存储器加载到阵列中 该数组被传递到助手类中。 然后片段加载该数组(该数组也可以工作) 我可以设置适配器,但是listview仍然是空的,即使数组有值。 我尝试了很多代码,但到目前为止没有任何效果。 但我没有收到任何错误消息

我的主要活动是:

package needforbleed.com.music;

public class MainActivity extends AppCompatActivity {
    //vars
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private ArrayList<Song> songList;
    private ListView songView;
    private SongAdapter  songAdt;
    Context c;

    //methods
    @Override
    public void onAttachFragment(android.app.Fragment fragment) {
        super.onAttachFragment(fragment);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        songList=new ArrayList<Song>();

        getSongList();



        c=this;

        Collections.sort(songList, new Comparator<Song>() {
            public int compare(Song a, Song b) {
                return a.getName().compareTo(b.getName());
            }
        });

        Helper.setSongs(songList);


      /*  for(int x=0;x<songList.size();x++)
        {



        }*/

       /* for(int x=0;x<songList.size();x++) {
            Toast toast = Toast.makeText(this,songList.get(x).getName(),Toast.LENGTH_SHORT);
            toast.show();

        }*///I used that loop to check songList for Content. It has content so that's not the error.
    }

    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        return view;
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
           switch (position)
           {
               case 0:return Player.newInstance(0,"Player");
               case 1:return Artists.newInstance(1,"Artists");
               case 2:return Albums.newInstance(2,"Albums");
               case 3:return Titles.newInstance(3,"Titles");
           }

           return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Player";
                case 1:
                    return "Artists";
                case 2:
                    return "Albums";
                case 3:
                    return "Titles";
            }
            return null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);

       /*songView=Helper.getLv();
        songAdt=new SongAdapter(this,songView.getId(),songList);
        songView.setAdapter(songAdt);
    */
    }

    public void getSongList() {
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);

        if(musicCursor!=null && musicCursor.moveToFirst()){
            //get columns
            int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);

            int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);

            int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);

           int albumColumn=musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                String thisAlbum = musicCursor.getString(albumColumn);
                Song s=new Song();
                s.setID(thisId);
                s.setName(thisTitle);
                s.setArtist(thisArtist);
                s.setAlbum(thisAlbum);

                songList.add(s);
            }
            while (musicCursor.moveToNext());
        }
    }
}
包需要bleed.com.music;
公共类MainActivity扩展了AppCompatActivity{
//瓦尔斯
私人部门SPAGERAAdapter MSECTIONSPAGERAAdapter;
私有视图寻呼机mViewPager;
私人ArrayList歌曲列表;
私有列表视图;
私有SongAdapter songAdt;
上下文c;
//方法
@凌驾
公共碎片(android.app.Fragment){
超级片段(片段);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//活动的主要部分。
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout TabLayout=(TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mviewpage);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Snackbar.make(查看“替换为您自己的操作”,Snackbar.LENGTH\u LONG)
.setAction(“Action”,null).show();
}
});
songList=newarraylist();
getSongList();
c=这个;
Collections.sort(songList,newcomparator(){
公共整数比较(歌曲a、歌曲b){
返回a.getName().compareTo(b.getName());
}
});
Helper.setSongs(歌曲列表);

/*对于(int x=0;x,在片段返回
视图中创建视图,而不是

inflater.inflate(R.layout.fragment_titles, container, false);

onCreateView
方法中,返回新视图

应该是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_titles, container, false);
    lv=(ListView)view.findViewById(R.id.lv_t);

    sa=new SongAdapter(this.getContext(),lv.getId(),Helper.getSongs());
    lv.setAdapter(sa);

    return view;
}

您还可以添加标题列表xml吗?哎呀,我错过了!对不起!我将编辑问题您只在帮助程序中使用歌曲的Arraylist对吗?同时发布您的彩色xml文件我正在将内容从Mainactivity中的media.storage加载到一个数组中。该数组将传递到帮助程序数组中。然后该数组将由片段使用。数组本身具有val非常感谢。这很有效。但我真的不明白为什么。写“充气机。充气(R.layout.fragment_titles,container,false);”不是一样吗?有什么区别吗?@Needforbleed每次调用
充气
都会创建新视图,即当您返回
充气机。充气()
您创建了新的ViewInstance谢谢。简单易懂的解释。我将您的答案标记为所需答案!
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="needforbleed.com.music.Titles">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="523dp"
        android:id="@+id/lv_t"
        android:layout_gravity="left|top"
        android:smoothScrollbar="true" />
</FrameLayout>
package needforbleed.com.music;

public class Helper {
    private static SongAdapter s;
    private static ArrayList<Song> songs;
    private static ListView lv;

    public static ListView getLv() {
        return lv;
    }

    public static void setLv(ListView lv) {
        lv = lv;
    }

    public static ArrayList<Song> getSongs() {
        return songs;
    }

    public static void setSongs(ArrayList<Song> songs) {
        Helper.songs = songs;
    }

    public static SongAdapter getS() {
        return s;
    }

    public static void setS(SongAdapter s) {
        Helper.s = s;
    }
}
package needforbleed.com.music;

public class SongAdapter extends ArrayAdapter<Song> {

    public SongAdapter(Context context, int listviewid) {
        super(context, listviewid);
    }

    public SongAdapter(Context context, int resource, ArrayList<Song> tracks) {
        super(context, resource, tracks);
    }

    @Override
    public Song getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.title_list,parent,false);
        }

        Song p = getItem(position);

        if (p != null) {
            TextView tt1 = (TextView) v.findViewById(R.id.song_title);
            TextView tt2 = (TextView) v.findViewById(R.id.song_artist);
            TextView tt3 = (TextView) v.findViewById(R.id.song_album);

            if (tt1 != null) {
                tt1.setText(p.getName());
            }

            if (tt2 != null) {
                tt2.setText(p.getArtist());
            }

            if (tt3 != null) {
                tt3.setText(p.getAlbum());
            }
        }

        return v;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="songPicked"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:id="@+id/song_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        android:textSize="22sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/song_artist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/song_album"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="18sp" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#f08320</color>
    <color name="colorPrimaryDark">#d76e0e</color>
    <color name="colorAccent">#f08320</color>
    <color name="tab_color">#ffffff</color>
    <color name="nav_bar">#f08320</color>

</resources>
inflater.inflate(R.layout.fragment_titles, container, false);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_titles, container, false);
    lv=(ListView)view.findViewById(R.id.lv_t);

    sa=new SongAdapter(this.getContext(),lv.getId(),Helper.getSongs());
    lv.setAdapter(sa);

    return view;
}