我对此很有用。也许我正在做一些非常愚蠢的事情。
我的问题是:有一种方法可以在片段方法中获取活动方法吗?
public class MyMusic extends Fragment {
private static final int MY_PERMISSION_REQUEST = 1;
SongScreen songScreen = new SongScreen();
static ArrayList<Song> songList = new ArrayList<Song>();;
ListView songView;
String songTitle, songArtist, durationSong, songAlbum;
Song currentSong;
int position;
public MyMusic()
{}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_music, container, false);
//List View
songView = (ListView) view.findViewById(R.id.my_music);
songList = new ArrayList<Song>();
if (ActivityCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
if(ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
android.Manifest.permission.READ_EXTERNAL_STORAGE)){
ActivityCompat.requestPermissions(getActivity(),
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
}else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
}
} else {
accessfiles();
}
return view;
}
public void accessfiles(){
// songList = new ArrayList<Song>();
Cursor song = getMusic();
SongAdapter songAdt = new SongAdapter(this.getContext(), songList);
songView.setAdapter(songAdt);
//When you click on the item pass to a new fragment with all the info
songView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String songTitle = ((TextView) view.findViewById(R.id.titleSong)).getText().toString();
String songArtist = ((TextView) view.findViewById(R.id.artistSong)).getText().toString();
String durationSong = ((TextView) view.findViewById(R.id.songDuration)).getText().toString();
Song currentSong = songList.get(position);
String songAlbum = currentSong.getAlbum();
passingToScreen(songTitle,songArtist,durationSong,songAlbum);
//Path have the path of the song
String path = currentSong.getPathSong();
((MainActivity)getActivity()).playerStart(path ,currentSong);
}});
}
private void passingToScreen(String songTitle, String songArtist, String durationSong, String songAlbum) {
Bundle bundle = new Bundle();
bundle.putString("songTitle", songTitle);
bundle.putString("songArtist", songArtist);
bundle.putString("durationSong", durationSong);
bundle.putString("songAlbum", songAlbum);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
songScreen.setArguments(bundle);
fragmentTransaction.replace(R.id.layoutCM,
songScreen,
songScreen.getTag()).commit();
}
public Cursor getMusic(){
Context context = getContextOfApplication();
ContentResolver contentResolver = context.getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songcursor = contentResolver.query(songUri, null, null, null, null);
if (songcursor != null && songcursor.moveToFirst()){
int songId = songcursor.getColumnIndex(MediaStore.Audio.Media._ID);
int songTitle = songcursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist = songcursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int songAlbum = songcursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int songDuration = songcursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
int pathSong = songcursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do{
Long currentId = songcursor.getLong(songId);
String currentTitle = songcursor.getString(songTitle);
String currentArtist = songcursor.getString(songArtist);
String currentAlbum = songcursor.getString(songAlbum);
Long currentDuration = songcursor.getLong(songDuration);
String currentPath = songcursor.getString(pathSong);
songList.add(new Song(currentId, currentTitle, currentArtist, currentDuration, currentAlbum, currentPath ));
}
while (songcursor.moveToNext());
}
return songcursor;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case MY_PERMISSION_REQUEST: {
if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
accessfiles();
}
}
}
}
}
public void getNext(Song currentSong) {
if (songList.contains(currentSong))
{
int position1 = songList.indexOf(currentSong);
if(position1 > songList.size())
{
String nextSong = songList.get(0).getPathSong();
Song next = songList.get(0);
((MainActivity)getActivity()).playerStart(nextSong, next);
}else{
String nextSong = songList.get(position1+1).getPathSong();
Song next = songList.get(position1 + 1);
((MainActivity)getActivity()).playerStart(nextSong, next);
}
}
}
public void getPrevious(Song currentSong) {
int position = (int) (currentSong.getID() - 1);
if(position < 0)
{
String previousSong = songList.get(songList.size()).getPathSong();
Song previous = songList.get(songList.size());
((MainActivity)getActivity()).playerStart(previousSong, previous);
}else{
String previousSong = songList.get(position).getPathSong();
Song previous = songList.get(songList.size());
((MainActivity)getActivity()).playerStart(previousSong, previous);
}
}
}
这就是我想要做的。但我知道我不能做&#34; ((MainActivity)getActivity()).playerStart(previousSong, previous);
&#34;但是我可以把我的电流和路径带到我的主要活动中吗?
提前谢谢
答案 0 :(得分:0)
两种方式:
1.使用EvenBus。 https://github.com/greenrobot/EventBus
在你的片段中:
public AppCompatActivity mActivity;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mActivity = (BaseActivity) context;
}
并致电:
((MainActivity)mActivity).playerStart(previousSong, previous);