我想制作导航抽屉中的列表视图。在我的应用程序中,我从URL获取JSON数据并将其设置为导航抽屉的ListView。我想制作一个动态的导航抽屉,其中的内容将被刷新(如播放列表)。 但问题是列表视图没有填充。它总是在抽屉中的空白列表。我把我的抽屉放在一个片段中,即它只适用于那个片段。
public class MusicFragment extends Fragment {
private static final String jsonUrl ="http://api.androidhive.info/json/movies.json";
public MusicFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
serviceIntent = new Intent(getActivity(), MusicPlayerService.class);
} catch (Exception e){
e.printStackTrace();
Toast.makeText(MyApplication.getAppcontext(), e.getClass().getName()+" "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
image_url = "http://square.github.io/picasso/static/sample.png";
musicFragmentView = inflater.inflate(R.layout.fragment_music,container,false);
albumArt = (ImageView)musicFragmentView.findViewById(R.id.music_albumArt_netView);
loadImageToView(image_url,albumArt);
musicUrl = "https://s3-ap-northeast-1.amazonaws.com/audiopraf2/02+-+3G+-+Khalbali+%5BMP3Khan%5D.mp3";
playPauseButton = (ImageButton) musicFragmentView.findViewById(R.id.playPauseButton);
nextTrackButton = (ImageButton) musicFragmentView.findViewById(R.id.nextTrackButton);
if(boolMusicPlaying)
playPauseButton.setBackgroundResource(R.mipmap.pause);
else
playPauseButton.setBackgroundResource(R.mipmap.play);
Toast.makeText(getContext(), "bool value "+boolMusicPlaying, Toast.LENGTH_SHORT).show();
setlistener();
drawerLayout = (DrawerLayout)musicFragmentView.findViewById(R.id.drawer_in_music) ;
playlistListView = (ListView)drawerLayout.findViewById(R.id.listView_playlist);
return musicFragmentView;
}
public void hideDialog(){
if (progressDialog!=null);
progressDialog.dismiss();
progressDialog=null;
}
private void loadImageToView(String url, final ImageView holder)
{
imageLoader = VolleySingleton.getVolleyInstance().getImageLoader();
imageLoader.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
holder.setImageBitmap(response.getBitmap());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
holder.setBackground(null);
}
}
}
@Override
public void onErrorResponse(VolleyError error) {
Log.e(new String(), "Image Load Error: " + error.getMessage());
}
});
}
void setlistener(){
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
playPauseButtonClick();
}
});
}
void playPauseButtonClick(){
if(!boolMusicPlaying){
playPauseButton.setBackgroundResource(R.mipmap.pause);
streamMusic();
boolMusicPlaying = true;
}
else{
playPauseButton.setBackgroundResource(R.mipmap.play);
stopMusic();
boolMusicPlaying = false;
}
Toast.makeText(getContext(), "bool value "+boolMusicPlaying, Toast.LENGTH_SHORT).show();
}
void streamMusic(){
//code to start music
serviceIntent.putExtra("musicUrl",musicUrl);
try{
getActivity().startService(serviceIntent);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MyApplication.getAppcontext(), e.getClass().getName()+" "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
boolMusicPlaying = true;
}
void stopMusic(){
//code to stop music
try{
getActivity().stopService(serviceIntent);
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MyApplication.getAppcontext(), e.getClass().getName()+" "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
boolMusicPlaying = false;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("loading...");
progressDialog.show();
adapter = new PlaylistItemAdapter(getActivity(),playlistItemsArrayList);
//create jsonvolley request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(jsonUrl, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hideDialog();
//parsing Json
for(int i=0;i<response.length();i++){
try {
JSONObject jsonObject = response.getJSONObject(i);
PlaylistItems playlistItem = new PlaylistItems();
playlistItem.setTitle(jsonObject.getString("title"));
playlistItem.setAlbum(jsonObject.getString("rating"));
Toast.makeText(getContext(), ""+jsonObject.getString("title"), Toast.LENGTH_SHORT).show();
playlistItem.setArtist(jsonObject.getString("releaseYear"));
playlistItem.setImage(jsonObject.getString("image"));
//add to array
playlistItemsArrayList.add(playlistItem);
}catch (JSONException e){
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
playlistListView.setAdapter(adapter);
VolleySingleton.getVolleyInstance().addToRequestQueue(jsonArrayRequest);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}