0

I am trying to load some items from JSON, I am able to get and parse the JSON and load it up in listview when using one activity. However, I want to use a LoadJSON.class to load the JSON, and then the activity can call the json passed and show it in the listview in that activity.

Here is what I have tried:

SongsManager.class

public class SongsManager {

private String TAG = SongsManager.class.getSimpleName();
private static final String API_URL = "http://xxxxxxxxxxxxx.com/jame/mp3/songlist.json";
private List<SolTracks> solTracksList;
private ProgressDialog pDialog;
private final Activity activity;

public SongsManager(Activity activity) {
    this.activity = activity;

    solTracksList = new ArrayList<>();
    pDialog = new ProgressDialog(activity);

    fetchSongs();
}

private void fetchSongs() {
    pDialog.setMessage("Fetching Playlist...");
    pDialog.show();

    // Volley's json array request object
    JsonArrayRequest req = new JsonArrayRequest(API_URL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, "Responser = " + response.toString());
                    pDialog.hide();

                    if (response.length() > 0) {
                        // looping through json and adding to movies list
                        for (int i = 0; i < response.length(); i++) {
                            try {
                                JSONObject movieObj = response.getJSONObject(i);

                                String songTitle = movieObj.getString("title");
                                String songId = movieObj.getString("id");
                                String streamUrl = movieObj.getString("stream_url");

                                SolTracks m = new SolTracks(songTitle, songId, streamUrl);

                                solTracksList.add(m);
                                Collections.sort(solTracksList, new TrackComparator());


                            } catch (JSONException e) {
                                Log.e(TAG, "JSON Parsing error: " + e.getMessage());
                            }
                        }
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Server Error: " + error.getMessage());
            pDialog.hide();
            Snackbar snackbar = Snackbar
                    .make(activity.findViewById(android.R.id.content), "PLEASE CHECK YOUR INTERNET", Snackbar.LENGTH_LONG)
                    .setAction("DISMISS", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                        }
                    });
            // Changing snackbar background
            snackbar.getView().setBackgroundColor(ContextCompat.getColor(activity, R.color.colorPrimary));

            // Changing message text color
            snackbar.setActionTextColor(Color.YELLOW);

            // Changing action button text color
            View sbView = snackbar.getView();
            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.WHITE);

            snackbar.show();

        }
    });

    req.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);

}


public List<SolTracks> getList() {
    return solTracksList;
}

Activity class

public class TheMain1 extends AppCompatActivity {

private SwipeRefreshLayout swipeRefreshLayout;
private String TAG = TheMain1.class.getSimpleName();
private static final String API_URL = "http://xxxxxxxxxxx.com/jame/mp3/songlist.json";
private ListView listView;
private SolTracksAdapter adapter;
private ProgressDialog pDialog;
private List<SolTracks> songslist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    listView = (ListView) findViewById(R.id.track_list_view);

    songslist = new ArrayList<>();
    SongsManager songsManager = new SongsManager(this);
    songslist = songsManager.getList();
    adapter = new SolTracksAdapter(this, songslist);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()

                                    {
                                        @Override
                                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                            SolTracks track = songslist.get(position);

                                            final String stream_url = track.stream_url;
                                            final String id_url = track.id;

                                            Intent intent = new Intent(TheMain1.this, PlayerActivity.class);
                                            intent.putExtra("songPosition", position);
                                            intent.putExtra("streamUrl", stream_url);
                                            startActivity(intent);

                                        }
                                    }

    );
}

As it is right now, I know the JSON is loaded from SongsManager, but its just not displaying in the listview of the Activity class. Can anyone help, and show what I'm doing wrong? Thanks

1
  • all you need is communication from your songManger to activity so apply this. thinks of fragment as SongManager and send list to activity from SongManager when list is constructed Commented Dec 21, 2016 at 17:13

1 Answer 1

1

I was able to fix this by implementing Parcelable to send the list to the receiving activity.

public class SolTracks implements Parcelable {
    public String title;
    public String id;
    public String stream_url;
}

Sending the list from the Activity A:

Intent intent = new Intent(TheMain.this, PlayerActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", solTracksList);
intent.putExtras(bundle);
intent.putExtra("songPosition", position);
startActivity(intent);

and then receiving in Activity B:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    songPosition = extras.getInt("songPosition");
    trackList = extras.getParcelableArrayList("mylist");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.