gpt4 book ai didi

java - 在单独的类中加载 JSON 数组列表并在另一个 Activity 中加载

转载 作者:行者123 更新时间:2023-12-01 09:06:12 25 4
gpt4 key购买 nike

我正在尝试从 JSON 加载一些项目,在使用一个 Activity 时,我能够获取并解析 JSON 并将其加载到 ListView 中。但是,我想使用 LoadJSON.class 来加载 JSON,然后该 Activity 可以调用传递的 json 并将其显示在该 Activity 的 ListView 中。

这是我尝试过的:

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 类别

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);

}
}

);
}

现在,我知道 JSON 是从 SongsManager 加载的,但它只是没有显示在 Activity 类的 ListView 中。谁能帮忙,并表明我做错了什么?谢谢

最佳答案

我能够通过实现 Parcelable 将列表发送到接收 Activity 来解决此问题。

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

从 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);

然后在Activity B中接收:

Bundle extras = getIntent().getExtras();
if (extras != null) {
songPosition = extras.getInt("songPosition");
trackList = extras.getParcelableArrayList("mylist");
}

关于java - 在单独的类中加载 JSON 数组列表并在另一个 Activity 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com