recyclerView保持空白

时间:2018-02-11 12:26:31

标签: android xml android-recyclerview android-mediaplayer

在我的应用程序中我试图获取每张专辑的曲目,但它在recyclerView中没有显示任何内容。虽然,我只是尝试使用textView在Activity.i上显示的活动我无法找到错误。请给我一个解决方案。我确实花了时间在stackoverFlow但是找不到解决方案。当我点击列表它应该给我但它没有显示任何内容。

 public class albumsongAdapter extends RecyclerView.Adapter<albumsongAdapter.ViewHolder> {

        ArrayList<albumInfo>albumSongList=new ArrayList<>();
        Context context;

        public albumsongAdapter(ArrayList<albumInfo> albumSongList, Context context) {
            this.albumSongList = albumSongList;
            this.context = context;
        }

        @Override
        public albumsongAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View myView = LayoutInflater.from(context).inflate(R.layout.songsalbum,parent,false);
            return new ViewHolder(myView);
        }

        @Override
        public void onBindViewHolder(albumsongAdapter.ViewHolder holder, int position) {
            final albumInfo a = albumSongList.get(position);
            holder.song.setText(albumSongList.get(position).getX());
        }

        @Override
        public int getItemCount() {
            return albumSongList.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView song;
            public ViewHolder(View itemView) {
                super(itemView);
                song=(TextView)itemView.findViewById(R.id.songs);
            }
        }
    }


public class albumSongs extends AppCompatActivity {

    ArrayList<albumInfo>albumSongList=new ArrayList<>();
    RecyclerView recyclerView2;
    public albumsongAdapter albumsongAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_album_songs);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("try");
        recyclerView2 = (RecyclerView) findViewById(R.id.recyclerView2);
        recyclerView2.setHasFixedSize(true);
        albumsongAdapter = new albumsongAdapter(albumSongList,this);
        SearchView searchView=(SearchView)findViewById(R.id.search);
        recyclerView2.setAdapter(albumsongAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView2.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView2.setLayoutManager(linearLayoutManager);
        recyclerView2.addItemDecoration(dividerItemDecoration);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

public class albumInfo{
    private String id;
    private String songs;
    private String noOfSongs;
    private String artist;
    private String title;
    private String id1;
    private String name1;
    private String name;
    private String type;
    private String x;
    public albumInfo(String id, String name, String artist2, String nr,String name1,String x) {
        this.id=id;
        this.artist=name;
        this.songs=artist2;
        this.noOfSongs=nr;
        //this.id1=id2;
        this.name=name;
        this.name1=name1;
        this.x=x;
    }
    public albumInfo(String x) {
        this.x=x;
    }
    public String getX() {
        return x;
    }

    public String getId() {
        return id;
    }
    public String getName1() {
        return name1;
    }

    public String getSongs() {
        return songs;
    }

    public String getNoOfSongs() {
        return noOfSongs;
    }

    public String getArtist() {
        return artist;
    }

    public String getId1() {
        return id1;
    }

    public String getTitle() {
        return title;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }
}

public void getSongsPerAlbum(String AlbumId) {
    final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    final String track_id = MediaStore.Audio.Media._ID;
    final String track_no = MediaStore.Audio.Media.TRACK;
    final String track_name = MediaStore.Audio.Media.TITLE;
    final String artist = MediaStore.Audio.Media.ARTIST;
    final String duration = MediaStore.Audio.Media.DURATION;
    final String album = MediaStore.Audio.Media.ALBUM;
    final String albumid = MediaStore.Audio.Media.ALBUM_ID;
    final String composer = MediaStore.Audio.Media.COMPOSER;
    final String year = MediaStore.Audio.Media.YEAR;
    final String path = MediaStore.Audio.Media.DATA;
    final String date_added = MediaStore.Audio.Media.DATE_ADDED;
    String[] columns = new String[]{
            track_id,
            track_no,
            track_name,
            artist,
            duration,
            album,
            albumid,
            composer,
            year,
            path,
            date_added
    };
    // Get all tracks if no album id
    Cursor csr;
    if (AlbumId == null || AlbumId.length() < 1) {
        csr = getActivity().getContentResolver().query(uri, columns,null,null,track_no);
    } else {
        // Doesn't work think media has to be scanned first before albums are generated
        csr = getActivity().getContentResolver().query(uri, columns,album + "=?",new String[]{AlbumId},track_no);
    }

    for (String s: csr.getColumnNames()
            ) {
        Log.d("SONGCOLUMN","Column = " + s);
    }
    while (csr.moveToNext()) {
        String id = (csr.getString(csr.getColumnIndex(album)));
        Log.d("SONG","Name of Song is " + csr.getString(csr.getColumnIndex(track_name)) +
                " from Album " + csr.getString(csr.getColumnIndex(album))
        );
        s=new albumInfo(id);
        albumSongList.add(s);
    }
    csr.close();
 //  recyclerView2.setAdapter(albumsongAdapter);
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.murarilal.atry.albumSongs"
   >
   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/recyclerView2"
       android:paddingTop="?android:attr/actionBarSize"
       />
   <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       android:minHeight="?attr/actionBarSize"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="?attr/colorPrimary"
       >
   </android.support.v7.widget.Toolbar>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="50dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="shape of you"
        android:textSize="25sp"
        android:textStyle="bold|italic"
        android:id="@+id/songs"/>
</android.support.v7.widget.CardView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

你还没有在任何地方给你getSongsPerAlbum打电话。你的arrayList是空的。