I have a ListView onto which I would like to load image from url where images names are an ID.jpg (eg: 1.jpg / 2.jpg / 3.jpg.. etc). The IDs are from the database, however the images loaded into my ListView for all rows is one image (1.jpg).
txtDate.setText(Request_Date.get(position));
txtTime.setText(Request_Time.get(position));
//ivImage.setImageResource(imgid[position]); <-- this works from drawable and not from database
Picasso.with(context).load(url_poster + request_eventID + ".jpg").into(ivImage);
The request_eventID are IDs from DB, this is my problem, it only returns 1 (and not the rest of the IDs) so therefore only 1.jpg is loaded into the ListView on all rows.
EDIT:
How I retrieve IDs
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("read_columns");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String request_date = jsonChildNode.optString("Date");
String request_time = jsonChildNode.optString("Time");
request_eventID = jsonChildNode.optString("EID");
arrRequest_Date.add(request_date);
arrRequest_Time.add(request_time);
arrRequest_EID.add(request_eventID);
Note that I can see all IDs if I do a System.out.println("IDs" +request_eventID)
Ok, just as I suspected, I played around with the code a bit and indeed get(position) was what I needed on the IDs.
Solution:
Picasso.with(context).load(url_poster + Request_EID.get(position) + ".jpg").into(ivImage);
Thank you all for responding.
Related
Hello below is my image ulrs thats comes from backend services. The images are separated with comma(,). Please let me know how can i separate each images and set them to imageView.
Below is the url
{"data":[{"title":"Manali","organizer":"SINGH","image":",
Img_5c3ff5642013e20190117_084929.jpg,
Img_5c3ff564204b720190117_085027.jpg,
Img_5c3ff564206abPhoto_1547695381805.png,
Img_5c3ff56420e2f20190117_084853.jpg,",
"date1":"21 Apr 2019",
"date2":"24 Jan 2019",
"time1":"10:30AM",
"time2":"2:00PM",
"address":"VASANT VIHAR D-BLOCK MARKET NEW DELHI",
"city":"Delhi",
"state":"Delhi",
"price":"7000"}
]
}
Thanks in advance
TextUtils.split(",", fullImageString); will give you the images in a string array, and depending on how to use them you can show the images fetching the values from what you created. It is not possible to show multiple images in one ImageView however you can show multiple images with GridView, ListView or RecyclerView that holds multiple ImageView references, with using an adapter and such. If these are image URLs, use this library to efficiently process each URL and display them on image views, while working with list adapters.
To extract the urls, you would do the following :
try {
JSONObject jsonObject = new JSONObject(resultFromWebservice);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject innerObj = jsonArray.getJSONObject(i);
String imgURLs = innerObj.getString("image");
ArrayList urlList= new ArrayList(Arrays.asList(imgURLs.split(",")));
for (int j = 0; j < urlList.size(); j++) {
Log.d("Extracted URLs", "myMethod: " + urlList.get(j));
// You can set the imageView urls from here
}
}
} catch (JSONException e) {
e.printStackTrace();
}
I have a project with a TabLayout + ViewPager to scroll through two different fragments (one shows data happening currently and the other shows all data).
In the onCreateView's of both of these I call the same ASyncTask class with the only difference being the params changing.
FetchItems asyncTask = new FetchItems(getContext(), this);
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"someParams");
The JSON result is then processed here:
try {
LinkedList<LeagueItem> leagues = new LinkedList<>();
LeagueItem newLeague;
//For every league in the array
for (int i = 0; i < jsonBody.length(); i++) {
newLeague = new LeagueItem();
//Hold the league games
JSONObject jsonLeagueInfo = jsonBody.getJSONObject(i);
newLeague.setLeagueName(jsonLeagueInfo.getString("name"));
JSONArray leagueMatches = jsonLeagueInfo.getJSONArray("matches");
//For every match in that league
for(int j = 0;j<leagueMatches.length();j++){
JSONObject matchInformation = leagueMatches.getJSONObject(j);
JSONArray teamsArray = matchInformation.getJSONArray("teams");
MatchItem currentMatch = new MatchItem();
//For both teams in the match
for(int k=0;k<2;k++){
JSONObject teamInfo = teamsArray.getJSONObject(k);
String name = teamInfo.getString("name");
String logoUrl = teamInfo.getString("logo");
JSONObject scores = teamInfo.getJSONObject("results");
String runningScore = scores.getString("runningscore");
TeamItem currentTeam = new TeamItem(shortName, logoUrl, Integer.parseInt(runningScore), homeNum);
currentMatch.addTeam(currentTeam);
}
newLeague.addMatch(currentMatch);
}
leagues.add(newLeague);
}
return leagues;
}
I'm finding that both objects returned have crossover data which shouldn't be there. Both of the parent objects are correct in that they add the correct number of league items, however every league contains pretty much all the data that I'm iterating over. Am I missing something huge here? I thought that by calling executeOnExecuter I would be getting two completely separate threads with different objects.
Thanks.
Well I'm new to Android. I'm getting a JSON string from a remote URL.
[{"key":"myString1","val":"myValue1"},{"key":"myString2","val":"myValue2"},{"key":"myString3","val":"myValue3"},{"key":"myString4","val":"myValue4"},{"key":"myString5","val":"myValue5"}]
I just need to parse this JSON string & display all key-val pair. I tried something like below from one of the tutorial.
JSONArray jArray = new JSONArray(str);
json = jArray.getJSONObject(0); //This will take first pair.
But I don't know the syntax for iterating through whole json object. Any help would be appreciated. Thanks in Advance.
There's nothing special in it. You do it like iterating any other array.
Let's say you have two String arrays to be filled with values: String[] mKey, mValue
Reading from JSON array will be like:
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
mKey[i] = object.getString("key");
mValue[i] = object.getString("val");
}
I have tried to fetch all album IDs from facebook using the following code.
But only first 25 albums were returned. Did I miss something?
response = facebook.request("me/albums");
json = Util.parseJson(response);
albums = json.getJSONArray("data");
for (int i =0; i < albums.length(); i++) {
JSONObject album = albums.getJSONObject(i);
String album_name = album.getString("name");
String album_id = album.getString("id");
}
But only first 25 albums were returned. Did I miss something?
Yes – that 25 is the default limit parameter value for requests like this.
Either use the paging links to retrieve the next 25 and so on, or set a higher limit yourself.
https://developers.facebook.com/docs/reference/api/pagination/
I use following ways to get the rest of album list.
next_page_URL = null;
next = json.getJSONObject("paging");
next_page_URL = next.getString("next");
response = facebook.request("me/albums&"+next_page_URL);
In my app, I get the user's friends with the following:
https://graph.facebook.com/me?fields=id,name&access_token=xxxx
The above is actually done by the Facebook Android SDK, so I'm actually creating the request like this:
Bundle params = new Bundle();
params.putString("fields", "name,id");
asyncFacebookRunner.request("me/friends", params, "GET", listener, null);
I parse the JSON response like this:
JSONObject data = Util.parseJson(response);
JSONArray friendsData = data.getJSONArray("data");
mDbAdapter.deleteAllFriends();
for (int i = 0; i < friendsData.length(); i++) {
JSONObject friend = friendsData.getJSONObject(i);
mDbAdapter.addFriend(friend.getString("name"),
friend.getString("id"));
}
The Util class is part of the Facebook Android SDK.
On my Android phone with wifi on, this takes about 15-20 seconds to send, retrieve, and parse the response (my account has about 370 friends). I've seen a few other apps that do it in about 2 seconds. What's slowing me down here?
The only little reason for a delay I think is you database adapter, i.e. adding items while parsing through the result.
To optimize a bit, you could remove some of the assignments in your loop and add the data to the database through a single request:
JSONObject json = Util.parseJson(response);
JSONArray friendsData = json.getJSONArray("data");
String ids[] , names[] = new String[friendsData.length()];
for(int i = 0; i < friendsData .length(); i++){
ids[i] = friendsData .getJSONObject(i).getString("id");
names[i] = friendsData .getJSONObject(i).getString("name");
}
//TODO Add data to your database