Получить изображения с помощью JSON Array
Я пытаюсь установить все изображения в папке с Picasso, получая JSON из названий изображений, но я не могу заставить его работать, я пробовал несколько способов (некоторые из них показаны ниже). Я думаю, что проблема заключается в моем способе установки ссылок.
[GridView Activity]
public class tits extends MainActivity {
public static final String url = "http://www.(link).com/";
FrameLayout contentFrameLayout;
TextView tata;
public GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
contentFrameLayout.removeAllViewsInLayout();
getLayoutInflater().inflate(R.layout.activity_tits, contentFrameLayout);
tata = findViewById(R.id.titview);
gridView = (GridView) findViewById(R.id.greedView);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//parse json data
try {
JSONArray ar = new JSONArray(response);
for (int i = 0; i < ar.length(); i++) {
JSONObject child = ar.getJSONObject(i);
Log.i("log_tag", "picture:" + child.getString("picture"));
// 1
final String timbs = "\"" + url + String.valueOf(child.getString("picture")) + "\", ";
tata.append(timbs);
//Textview for me to see the string ouput
String[] stim = {timbs};
GridView gridView = (GridView) findViewById(R.id.greedView);
GridviewAdapter gridAdapter = new GridviewAdapter(getBaseContext(), stim);
gridView.setAdapter(gridAdapter);
//
// 2
final String[] timbs = {"\"" + url + String.valueOf(child.getString("picture")) + "\", "};
String oss = timbs.toString();
//Textview for me to see the string ouput
tata.append(oss);
GridView gridView = (GridView) findViewById(R.id.greedView);
GridviewAdapter gridAdapter = new GridviewAdapter(getBaseContext(), timbs);
gridView.setAdapter(gridAdapter);
//
// 3
final String timbs = "\"" + url + String.valueOf(child.getString("picture")) + "\", ";
//This time I tried to use the Textview to get the String Value
tata.append(timbs);
String[] stim = {tata.getText().toString()};
Toast.makeText(getBaseContext(), stim.toString(), Toast.LENGTH_LONG).show();
GridView gridView = (GridView) findViewById(R.id.greedView);
GridviewAdapter gridAdapter = new GridviewAdapter(getBaseContext(), stim);
gridView.setAdapter(gridAdapter);
//
//4
final String stime = "\"" + url + child.getString("picture") + "\", ";
tata.setText(stime);
String[] stim = {stime};
GridView gridView = (GridView) findViewById(R.id.greedView);
GridviewAdapter gridAdapter = new GridviewAdapter(getBaseContext(), stim);
gridView.setAdapter(gridAdapter);
//
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse (VolleyError error){
Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
};
[Мой адаптер]
public class GridviewAdapter extends BaseAdapter {
private Context context;
private String[] items;
public GridviewAdapter(Context context, String[] items){
super();
this.context = context;
this.items = items;
}
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return items[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = null;
if (convertView == null) {
img = new ImageView(context);
convertView = img;
img.setPadding(5, 5, 5, 5);
} else {
img = (ImageView) convertView;
}
Picasso.with(context)
.load(items[position])
.placeholder(R.drawable.aa)
.resize(200, 200)
.into(img);
return convertView;
}
};
1 ответ
Попробуйте это, загрузив изображения в gridview с помощью Picasso. https://seegatesite.com/android-tutorial-display-image-to-android-gridview-from-url-using-picasso/