如何使用recycleview从手机添加音乐数据?

时间:2019-07-03 15:56:17

标签: java android-studio

我试图创建一个活动名称SongPage,并尝试从手机的外部存储中检索我的音乐数据,并将其存储在回收视图中,以便可以在SongPage活动中显示。我该怎么办?

我曾尝试过在Recycle-view上观看视频以及他们如何在其中添加内容,但是我最近观看的是他们将硬编码的歌曲详细信息添加到recycle-view中。

//RecycleViewAdapter.java///

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.Viewholder> {
private static final String TAG = "RecycleViewAdapter";

private ArrayList<String> songList = new ArrayList<>();
private Context context;
private Song song;

public RecycleViewAdapter(Context context, ArrayList<String> songList ) {
    this.context = context;

    this.songList = songList;
}


//Infate the view

@Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = 


LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent,false);
    return new Viewholder(view);
}

//Important as it change according to it layout
@Override
public void onBindViewHolder(Viewholder viewholder, final int position)
{
    Log.d(TAG, "onBindViewHolder: called.");



    /*Glide.with(mContext)
            .asBitmap()
            .load(mImages.get(position))
            .into(viewholder.image);

    viewholder.imageName.setText(mImageNames.get(position));

    viewholder.parentlayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: You are now listining to "+ mImageNames.get(position));

            Toast.makeText(mContext, mImageNames.get(position), Toast.LENGTH_SHORT).show();
        }
    });*/
}

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

public class Viewholder extends RecyclerView.ViewHolder {

    private TextView Titleofsong, DurationofSong;
    private ImageView image, Play;

    public Viewholder(View itemView)
    {
        super(itemView);
        Titleofsong = (TextView)itemView.findViewById(R.id.TitleofSong);
        DurationofSong = (TextView)itemView.findViewById(R.id.Durationsong);
        image = (ImageView)itemView.findViewById(R.id.Image);
        Play = (ImageView) itemView.findViewById(R.id.play);
    }
}
//SongPage.java///

public class SongPage extends AppCompatActivity {
    private static final String TAG = "SongPage";
    private String[] itemAll;
    RecyclerView recyclerView;
    RecycleViewAdapter viewAdapter;

    //vars

    private ArrayList<String> songList = new ArrayList<>();
    private Context Context;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    Intent GoToHome = new Intent(SongPage.this, Home.class);
                    startActivity(GoToHome);
                    return true;

                case R.id.navigation_Song:
                    Intent GoToSong = new Intent(SongPage.this, SongPage.class);
                    startActivity(GoToSong);
                    return true;

                case R.id.navigation_Playlist:
                    Intent GoToPlaylist = new Intent(SongPage.this, PlaylistPage.class);
                    startActivity(GoToPlaylist);
                    return true;
            }
            return false;
        }
    };

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

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        Menu menu = navigation.getMenu();
        MenuItem menuItem = menu.getItem(1);
        menuItem.setChecked(true);
        Log.d(TAG, "onCreate: Started");
        intImageBitmaps();
        appExternalStoragePermission();

    }

    private void intImageBitmaps() {
        Log.d(TAG, "intImageBitmaps: Preparing Bitmaps");

    }

    private void initRecycleView() {
        Log.d(TAG, "initRecycleView: Init RecycleViewer");
        RecyclerView recyclerView = findViewById(R.id.Recycle);
        RecycleViewAdapter adapter = new RecycleViewAdapter(Context,songList);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    //Reading From Phone
    public void appExternalStoragePermission() {
        Dexter.withActivity(this)
                .withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        displayAudioSong();
                    }

                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {
                    }

                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                    }
                }).check();
    }

    public ArrayList<File> ReadOnlyFile(File file) {
        ArrayList<File> arrayList = new ArrayList<>();
        File[] allFiles = file.listFiles();
        for (File indiviualFile : allFiles) {
            if (indiviualFile.isDirectory() && !indiviualFile.isHidden()) {
                arrayList.addAll(ReadOnlyFile(indiviualFile));
            } else {
                if (indiviualFile.getName().endsWith(".mp3") || indiviualFile.getName().endsWith(".aac") || indiviualFile.getName().endsWith(".wav") || indiviualFile.getName().endsWith(".wma")) {
                    arrayList.add(indiviualFile);
                }
            }
        }
        return arrayList;
    }

    private void displayAudioSong()
    {
        final ArrayList<File> audioSongs = ReadOnlyFile(Environment.getExternalStorageDirectory());
        itemAll = new String[audioSongs.size()];

        for (int songCounter = 0; songCounter<audioSongs.size(); songCounter++)
        {
            itemAll[songCounter]=audioSongs.get(songCounter).getName();

        }
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SongPage.class, recyclerView, this );

    }
}

0 个答案:

没有答案