-3
我是Android新手,嘗試編碼以顯示從API獲取的海報,問題出現在運行後,然後它只顯示一張海報。請給我一些建議如何在圖像視圖中顯示所有圖像
1.poster_image.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poster"
android:layout_width="500dp"
android:layout_height="60dp" />
2.MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> moviesList = new ArrayList<>();
ImageView mImageView;
String url = "https://api.themoviedb.org/3/movie/popular?api_key=(MY API KEY)";
String image_url = "http://image.tmdb.org/t/p/w185";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poster_image);
mImageView = (ImageView) findViewById(R.id.poster);
// Fetch Data from API
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject movies = jsonArray.getJSONObject(i);
String poster = movies.getString("poster_path");
moviesList.add(poster);
Log.i("IS", String.valueOf(moviesList));
}
for (int j = 0; j < moviesList.size(); j++) {
Picasso.with(getApplicationContext())
.load(image_url + moviesList.get(j))
.into(mImageView);
Log.i("I", image_url + moviesList.get(j));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Wrong");
}
});
requestQueue.add(jsonObjectRequest);
}
}
如果使用ListView,該怎麼辦?我需要一個適配器嗎?謝謝 – Ricky
不使用listView使用RecyclerView創建自定義回收站適配器 –
使用listview或recyclerview(首選)與適配器來顯示圖像瀏覽列表,幾乎他們的每個例子都顯示與圖像列表,因此它會很容易找到 – yanivtwin