问题::我试图将我的mediaplayer方法(位于适配器内部)合并到我的片段(我想在其中放置我的mediaplayer对象)。
为什么?-因为我希望适配器不包含任何方法/逻辑。它应该仅膨胀视图,而不能增加其他任何内容。
我试图将我的MediaPlayer方法(位于适配器内部)移到片段中,但是当我尝试在片段(应该播放并停止播放歌曲)中找到playB和stopB时,它给了我nullException。我查看了我的XML文件(我的playB + stopB ID所在的位置),一切正常。
我的适配器:
public class ListViewAdapter extends BaseAdapter {
//Create variables
MediaPlayer mediaPlayer;
Song currentSong;
int layout;
ArrayList<Song> arrayList;
Context context;
//Constructor
public ListViewAdapter(int layout, ArrayList<Song> arrayList, Context context) {
this.layout = layout;
this.arrayList = arrayList;
this.context = context;
}
//ViewHolder class holding my views
private class Viewholder {
TextView artistTxt, songNameTxt;
ImageView playB, stopB;
CircleImageView artistImg;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
//Create viewholder variable
final Viewholder viewholder;
//Check if view is null
if (view == null) {
//Create new ViewHolder object
viewholder = new Viewholder();
//Inflate my view
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.listview_customlayout, null);
//Find my view id's
viewholder.artistImg = view.findViewById(R.id.artistImgBackgroundDetail);
viewholder.artistTxt = view.findViewById(R.id.artistTxt);
viewholder.songNameTxt = view.findViewById(R.id.songNameTxt);
viewholder.playB = view.findViewById(R.id.playB);
viewholder.stopB = view.findViewById(R.id.stopB);
//Set my view to viewholder
view.setTag(viewholder);
} else {
viewholder = (Viewholder) view.getTag();
}
//Assign song to my arraylist
final Song song = arrayList.get(position);
//Set my views to their resources
viewholder.artistImg.setImageResource(song.getArtistImg());
viewholder.artistTxt.setText(song.getArtist());
viewholder.songNameTxt.setText(song.getSongName());
return view;
如您所见-非常简单的适配器,我可以在其中修改自定义列表视图。
这是我的片段类,我无法找到视图ID:**
MediaPlayer mediaPlayer;
Song currentSong;
int position;
ImageView playB, stopB;
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
//Actionbar
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Song list");
//Find my listview
songListView = view.findViewById(R.id.songListView);
playB = getActivity().findViewById(R.id.playB);
stopB = getActivity().findViewById(R.id.stopB);
//create a new arraylist object
arrayList = new ArrayList<>();
adapter = new ListViewAdapter(R.layout.listview_customlayout, arrayList, getActivity());
//Set my listview to my custom adapter
songListView.setAdapter(adapter);
//Click on a specific song from my list
songListView.setOnItemClickListener(new ListViewClickListener());
final Song song = arrayList.get(position);
//get all songs
mediaPlayer = MediaPlayer.create(getActivity(), song.getSong());
//Play button click performed
playB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Checks if my current song is null and creates a new song
if (currentSong == null) {
mediaPlayer = MediaPlayer.create(getActivity(), song.getSong());
}
//if mediaplayer is not null and my current song is not equal to the new song i clicked on
if (mediaPlayer != null && currentSong != song) {
//resets the mediaplayer and creates a new song from the position in the list
mediaPlayer.reset();
mediaPlayer = MediaPlayer.create(getActivity(), song.getSong());
playB.setImageResource(R.drawable.play_orange);
mediaPlayer.start();
playB.setImageResource(R.drawable.pause_orange);
} else {
mediaPlayer.pause();
playB.setImageResource(R.drawable.play_orange);
}
//check if current song is null or the newly clicked song is equal to my current song
//if true then assign the newly clicked song as my CURRENT one
//--so it doesnt play the same song for every single one
if (currentSong == null || song != currentSong) {
currentSong = song;
}
}
});
//Stop song when click performed
stopB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//stops my current song and make it null
if (currentSong != null) {
mediaPlayer.stop();
mediaPlayer.release();
currentSong = null;
playB.setImageResource(R.drawable.play_orange);
}
}
});
//return my view
return view;
我想让我的ListViewAdapter
只能使视图膨胀 ,这意味着我不得包含功能。
任何建议都会被我视为要尽力而为:)
如果您需要其他信息,请告诉我。我会很乐意尽力弄清楚。
谢谢。
答案 0 :(得分:1)
只需尝试一下 建立界面
ClickUpdate.java
public interface ClickUpdate {
void getClicks();
}
内部
ListViewAdapter.java
public static ClickUpdate clickupadte;
public static void setonConnectionListener(ClickUpdate clicklisten) {
ListViewAdapter.clickupadte = clicklisten
}
也可以在listviewadapter中单击stopB
viewholder.stopB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickupadte.getClicks();
}
});
然后在里面
片段类 内部oncreateview初始化
ListViewAdapter.setonConnectionListener(this);
还实现了ClickUpdate,然后在片段类内部生成了一个名为getClicks的覆盖方法,我认为在该方法中给出
if (currentSong != null) {
mediaPlayer.stop();
mediaPlayer.release();
currentSong = null;
//playB.setImageResource(R.drawable.play_orange);//this will cause null point error,if occured to avoid the error pass viewholder.playB inside clickupadte.getClicks(); function
}
答案 1 :(得分:0)
在MVVM模式中,应该将适配器与视图而不是视图模型关联。因此,您可能要做的是使适配器成为片段类的内部类。如果您具有mediaPlayer全局变量,则可以在适配器和片段中使用它的相同实例(如果要查找的话)。
希望这会有所帮助!