0
我使用JSON請求文本,並把它與自定義適配器列表視圖,這是我到目前爲止的代碼..爪哇 - 使用畢加索爲獲得圖像從URL以JSON - androidstudio
private class getMovies extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(alrehabfilms.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
http sh = new http();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
// Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray moviesp90 = jsonObj.getJSONArray("moviesalrehab");
// looping through All Contacts
for (int i = 0; i < moviesp90.length(); i++) {
JSONObject c = moviesp90.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String actors = c.getString("actors");
String tandp = c.getString("prizeandtime");
String img = c.getString("img"); //here is for the image
// Phone node is JSON Object
JSONObject pref = c.getJSONObject("pref");
String imrating = pref.getString("imrating");
String genre = pref.getString("genre");
String guide = pref.getString("guide");
String director = pref.getString("director");
// tmp hash map for single contact
HashMap<String, String> movie = new HashMap<>();
// adding each child node to HashMap key => value
movie.put("id", id);
movie.put("name", name);
movie.put("actors", actors);
movie.put("prizeandtime", tandp);
if(img != null && !img.equalsIgnoreCase("")) Picasso.with(alrehabfilms.this).load(img).into(imggg); //here idk how to put like movie.put("img", Picasso code)
movie.put("imrating", imrating);
movie.put("genre", genre);
movie.put("guide", guide);
movie.put("director", director);
// adding contact to contact list
alrehabmovieList.add(movie);
}
} catch (final JSONException e) {
// Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"An error happened, please try again!",
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
// Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Failed to retrieve data, please check your internet connection",
Toast.LENGTH_LONG)
.show();
AlertDialog.Builder adb = new AlertDialog.Builder(alrehabfilms.this);
adb.setTitle("No internet connection");
adb.setMessage("Would you like to try to gather data again?");
adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new getMovies().execute();
} });
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
} });
adb.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
alrehabfilms.this, alrehabmovieList,
R.layout.imgtest, new String[]{"name", "actors",
"prizeandtime", "img", "imrating", "genre","guide", "director"}, new int[]{R.id.movieName,
R.id.movieActors, R.id.moviePrizeAndTime, R.id.imggg//imageeview, R.id.movieRating, R.id.movieGenre,R.id.pGuid, R.id.dir});
lv.setAdapter(adapter);
}
}
}
正如你看到這裏,我把這個線的圖像
String img = c.getString("img"); //here is for the image
我在這裏的問題是如何把它以這種方式
movie.put("img", THECODEIWANT);
,而不是這個,
if(img != null && !img.equalsIgnoreCase(""))
Picasso.with(alrehabfilms.this).load(img).into(imggg);
在此先感謝
嗯,我想根據它的電影是在JSON文件中把該圖像在ListView ..如果我把你的方式,我不會上市,因爲它應該是。你有什麼想法如何把它? – John
我的意思是,當它真的需要時加載圖像呢?如果我是你,當調用getView方法時,我會在自定義列表適配器中加載帶有Picasso的圖像。 – steve
你能告訴我一個簡單的例子嗎? – John