set images in image in a loop with Picasso Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to iterate over a JSONObject?Strange out of memory issue while loading an image to a Bitmap objectLazy load of images in ListViewHow to scale an Image in ImageView to keep the aspect ratioSet TextView style (bold or italic)Resize image to full width and fixed height with PicassoPicasso Load image from filesystemHow to touch image button without getview or findViewById on Robotium?Animated loading image in picassoandroid: create circular image with picassoPicasso v/s Imageloader v/s Fresco vs Glide
What is the difference between a "ranged attack" and a "ranged weapon attack"?
Sally's older brother
Trying to understand entropy as a novice in thermodynamics
Where is the Next Backup Size entry on iOS 12?
GDP with Intermediate Production
What does 丫 mean? 丫是什么意思?
Nose gear failure in single prop aircraft: belly landing or nose-gear up landing?
Does silver oxide react with hydrogen sulfide?
White walkers, cemeteries and wights
Would color changing eyes affect vision?
Random body shuffle every night—can we still function?
what is the log of the PDF for a Normal Distribution?
Simple Http Server
Why not send Voyager 3 and 4 following up the paths taken by Voyager 1 and 2 to re-transmit signals of later as they fly away from Earth?
My mentor says to set image to Fine instead of RAW — how is this different from JPG?
What is a more techy Technical Writer job title that isn't cutesy or confusing?
Monty Hall Problem-Probability Paradox
Can you force honesty by using the Speak with Dead and Zone of Truth spells together?
Delete free apps from library
Tips to organize LaTeX presentations for a semester
Can two person see the same photon?
retrieve food groups from food item list
Does the Black Tentacles spell do damage twice at the start of turn to an already restrained creature?
Getting out of while loop on console
set images in image in a loop with Picasso
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to iterate over a JSONObject?Strange out of memory issue while loading an image to a Bitmap objectLazy load of images in ListViewHow to scale an Image in ImageView to keep the aspect ratioSet TextView style (bold or italic)Resize image to full width and fixed height with PicassoPicasso Load image from filesystemHow to touch image button without getview or findViewById on Robotium?Animated loading image in picassoandroid: create circular image with picassoPicasso v/s Imageloader v/s Fresco vs Glide
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a .json
like so
"0":
"image_path": "192.168.1.52/test/image/im1.jpg",
"title": "image 1"
,
"1":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 2"
,
"2":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 3"
,
"size": "3"
My android portion is...
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject jObj_ = jObj.getJSONObject(""+i);
String image_path_variable = jObj_.getString("image_path");
This is where I'm stuck. I don't know how to insert images dynamically using the path I have in my .json
file.
so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
what i am saying the my json wont always have 3 images sometimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self
android json android-imageview picasso
|
show 2 more comments
I have a .json
like so
"0":
"image_path": "192.168.1.52/test/image/im1.jpg",
"title": "image 1"
,
"1":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 2"
,
"2":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 3"
,
"size": "3"
My android portion is...
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject jObj_ = jObj.getJSONObject(""+i);
String image_path_variable = jObj_.getString("image_path");
This is where I'm stuck. I don't know how to insert images dynamically using the path I have in my .json
file.
so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
what i am saying the my json wont always have 3 images sometimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self
android json android-imageview picasso
Is there any way to modifyjson
structure to make it much easier to parse? My proposal is to have array ([ ... ]
) of objects (...
). After that you no neededsize
and keys like0
,1
,...
.
– Boken
Mar 8 at 23:18
i updated my question
– o3203823
Mar 8 at 23:25
But you did not respond to my question. Current json structure is not so good.
– Boken
Mar 8 at 23:27
@Boken i can but m not sure i want to because it is very complicated at the moment
– o3203823
Mar 8 at 23:34
1
OK, so how many element can have this json? And How layout should look like when file will have e.g. 20 elements? Are you expecting 20 ImageViews? If yes, you should useRecyclerView
– Boken
Mar 8 at 23:36
|
show 2 more comments
I have a .json
like so
"0":
"image_path": "192.168.1.52/test/image/im1.jpg",
"title": "image 1"
,
"1":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 2"
,
"2":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 3"
,
"size": "3"
My android portion is...
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject jObj_ = jObj.getJSONObject(""+i);
String image_path_variable = jObj_.getString("image_path");
This is where I'm stuck. I don't know how to insert images dynamically using the path I have in my .json
file.
so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
what i am saying the my json wont always have 3 images sometimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self
android json android-imageview picasso
I have a .json
like so
"0":
"image_path": "192.168.1.52/test/image/im1.jpg",
"title": "image 1"
,
"1":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 2"
,
"2":
"image_path": "192.168.1.52/test/image/im2.jpg",
"title": "image 3"
,
"size": "3"
My android portion is...
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject jObj_ = jObj.getJSONObject(""+i);
String image_path_variable = jObj_.getString("image_path");
This is where I'm stuck. I don't know how to insert images dynamically using the path I have in my .json
file.
so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
what i am saying the my json wont always have 3 images sometimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self
android json android-imageview picasso
android json android-imageview picasso
edited Mar 8 at 23:35
o3203823
asked Mar 8 at 23:04
o3203823o3203823
237
237
Is there any way to modifyjson
structure to make it much easier to parse? My proposal is to have array ([ ... ]
) of objects (...
). After that you no neededsize
and keys like0
,1
,...
.
– Boken
Mar 8 at 23:18
i updated my question
– o3203823
Mar 8 at 23:25
But you did not respond to my question. Current json structure is not so good.
– Boken
Mar 8 at 23:27
@Boken i can but m not sure i want to because it is very complicated at the moment
– o3203823
Mar 8 at 23:34
1
OK, so how many element can have this json? And How layout should look like when file will have e.g. 20 elements? Are you expecting 20 ImageViews? If yes, you should useRecyclerView
– Boken
Mar 8 at 23:36
|
show 2 more comments
Is there any way to modifyjson
structure to make it much easier to parse? My proposal is to have array ([ ... ]
) of objects (...
). After that you no neededsize
and keys like0
,1
,...
.
– Boken
Mar 8 at 23:18
i updated my question
– o3203823
Mar 8 at 23:25
But you did not respond to my question. Current json structure is not so good.
– Boken
Mar 8 at 23:27
@Boken i can but m not sure i want to because it is very complicated at the moment
– o3203823
Mar 8 at 23:34
1
OK, so how many element can have this json? And How layout should look like when file will have e.g. 20 elements? Are you expecting 20 ImageViews? If yes, you should useRecyclerView
– Boken
Mar 8 at 23:36
Is there any way to modify
json
structure to make it much easier to parse? My proposal is to have array ([ ... ]
) of objects ( ...
). After that you no needed size
and keys like 0
, 1
, ...
.– Boken
Mar 8 at 23:18
Is there any way to modify
json
structure to make it much easier to parse? My proposal is to have array ([ ... ]
) of objects ( ...
). After that you no needed size
and keys like 0
, 1
, ...
.– Boken
Mar 8 at 23:18
i updated my question
– o3203823
Mar 8 at 23:25
i updated my question
– o3203823
Mar 8 at 23:25
But you did not respond to my question. Current json structure is not so good.
– Boken
Mar 8 at 23:27
But you did not respond to my question. Current json structure is not so good.
– Boken
Mar 8 at 23:27
@Boken i can but m not sure i want to because it is very complicated at the moment
– o3203823
Mar 8 at 23:34
@Boken i can but m not sure i want to because it is very complicated at the moment
– o3203823
Mar 8 at 23:34
1
1
OK, so how many element can have this json? And How layout should look like when file will have e.g. 20 elements? Are you expecting 20 ImageViews? If yes, you should use
RecyclerView
– Boken
Mar 8 at 23:36
OK, so how many element can have this json? And How layout should look like when file will have e.g. 20 elements? Are you expecting 20 ImageViews? If yes, you should use
RecyclerView
– Boken
Mar 8 at 23:36
|
show 2 more comments
3 Answers
3
active
oldest
votes
So, step by step in Kotlin.
My json file (in res > raw > people.json
):
"0":
"image_path": "https://images.unsplash.com/photo-1485199692108-c3b5069de6a0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 1"
,
"1":
"image_path": "https://images.unsplash.com/photo-1520065949650-380765513210?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 2"
,
"2":
"image_path": "https://images.unsplash.com/photo-1547513609-d80733850d2e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 3"
,
"3":
"image_path": "https://images.unsplash.com/photo-1550094195-2234fa4e2128?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 4"
,
"size": "4"
Layout for Activity (res > layout > activity_main.xml
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2" />
</LinearLayout>
Our main activity of the appliaction (MainActivity.kt
)
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupUi()
private fun setupUi()
// Read file content
val jsonFile = resources
.openRawResource(R.raw.images)
.bufferedReader()
.readText()
// Change "String" in to "Json Object"
val root = JSONObject(jsonFile)
// Get size of the list
val size = root.getString("size").toInt()
// Create (empty) list of people
val peopleList = mutableListOf<Person>()
// Loop to read all of the people
for (position in 0 until size)
val element = root.getJSONObject(position.toString())
// Change json in to Kotlin object
val photo = Person(
element.getString("image_path"),
element.getString("title")
)
// Add element to the list
peopleList.add(photo)
// Add adapter for RecyclerView
recycler_view.adapter = PeopleAdapter(this, peopleList)
Element of the list (res > layout > list_item.xml
)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="@id/image_view"
app:layout_constraintEnd_toEndOf="@id/image_view"
app:layout_constraintStart_toStartOf="@id/image_view" />
</android.support.constraint.ConstraintLayout>
Model class representing single element on the list (Person.kt
)
class Person(
val image: String,
val title: String
)
Adapter (using ViewHolder
pattern)
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.list_item.view.*
class PeopleAdapter(
private val context: Context,
private val listOfColor: List<Person>
) : RecyclerView.Adapter<PeopleAdapter.ViewHolder>()
// Creating elements (one item from list)
override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): ViewHolder
val view = LayoutInflater
.from(viewGroup.context)
.inflate(R.layout.list_item, viewGroup, false)
return ViewHolder(view)
// Number of elements
override fun getItemCount(): Int = listOfColor.size
// Fill elements with a data
override fun onBindViewHolder(row: ViewHolder, position: Int)
row.bind(position)
// Class responsible for one (single) row
inner class ViewHolder(private val item: View) : RecyclerView.ViewHolder(item)
fun bind(position: Int)
// Get image address
val person = listOfColor[position]
// Load image
Glide.with(context).load(person.image).into(item.image_view)
// Set text
item.title.text = person.title
Remember about three (3) dependencies
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation "com.github.bumptech.glide:glide:4.8.0"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
And (1) persmission in (manifest)
<uses-permission android:name="android.permission.INTERNET" />
Colors (in res > values > colors.xml
)
<color name="colorWhite">#FFF</color>
<!-- Black with 50% transparency -->
<color name="colorBlack">#80000000</color>
And expected output:
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
add a comment |
you can use this as a reference
https://stackoverflow.com/a/10593838/7586266
the idea is for you to iterate over the JSON objects you have "0" , "1" ,"2" ... etc
in order to do that go within the json object just like you entered size in here
jObj.getString("size")
and you can use the i in your for loop in order to get the jObj which contains your url
like this
JSONObject jObj = json.getJSONObject(Integer.toString(i)); // where json is your whole json and you are getting the object "0","1" and "2" from it
string image_path_variable = jObj .getString("image_path");
and you can parse your JObj at i to a JSONObject which you can get the "image_path"
once you are able to do that you can use
Picasso.get().load(image_path_variable).into(your_desired_image_view);
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
add a comment |
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject itemObj = jObj.get(Integer.valueOf(i)).getAsJsonObject();
String path = itemObj.getString("image_path")
ImageView view = new ImageView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layout.addView(view, params);
Picasso.get().load(path).into(view);
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072223%2fset-images-in-image-in-a-loop-with-picasso%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
So, step by step in Kotlin.
My json file (in res > raw > people.json
):
"0":
"image_path": "https://images.unsplash.com/photo-1485199692108-c3b5069de6a0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 1"
,
"1":
"image_path": "https://images.unsplash.com/photo-1520065949650-380765513210?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 2"
,
"2":
"image_path": "https://images.unsplash.com/photo-1547513609-d80733850d2e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 3"
,
"3":
"image_path": "https://images.unsplash.com/photo-1550094195-2234fa4e2128?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 4"
,
"size": "4"
Layout for Activity (res > layout > activity_main.xml
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2" />
</LinearLayout>
Our main activity of the appliaction (MainActivity.kt
)
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupUi()
private fun setupUi()
// Read file content
val jsonFile = resources
.openRawResource(R.raw.images)
.bufferedReader()
.readText()
// Change "String" in to "Json Object"
val root = JSONObject(jsonFile)
// Get size of the list
val size = root.getString("size").toInt()
// Create (empty) list of people
val peopleList = mutableListOf<Person>()
// Loop to read all of the people
for (position in 0 until size)
val element = root.getJSONObject(position.toString())
// Change json in to Kotlin object
val photo = Person(
element.getString("image_path"),
element.getString("title")
)
// Add element to the list
peopleList.add(photo)
// Add adapter for RecyclerView
recycler_view.adapter = PeopleAdapter(this, peopleList)
Element of the list (res > layout > list_item.xml
)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="@id/image_view"
app:layout_constraintEnd_toEndOf="@id/image_view"
app:layout_constraintStart_toStartOf="@id/image_view" />
</android.support.constraint.ConstraintLayout>
Model class representing single element on the list (Person.kt
)
class Person(
val image: String,
val title: String
)
Adapter (using ViewHolder
pattern)
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.list_item.view.*
class PeopleAdapter(
private val context: Context,
private val listOfColor: List<Person>
) : RecyclerView.Adapter<PeopleAdapter.ViewHolder>()
// Creating elements (one item from list)
override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): ViewHolder
val view = LayoutInflater
.from(viewGroup.context)
.inflate(R.layout.list_item, viewGroup, false)
return ViewHolder(view)
// Number of elements
override fun getItemCount(): Int = listOfColor.size
// Fill elements with a data
override fun onBindViewHolder(row: ViewHolder, position: Int)
row.bind(position)
// Class responsible for one (single) row
inner class ViewHolder(private val item: View) : RecyclerView.ViewHolder(item)
fun bind(position: Int)
// Get image address
val person = listOfColor[position]
// Load image
Glide.with(context).load(person.image).into(item.image_view)
// Set text
item.title.text = person.title
Remember about three (3) dependencies
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation "com.github.bumptech.glide:glide:4.8.0"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
And (1) persmission in (manifest)
<uses-permission android:name="android.permission.INTERNET" />
Colors (in res > values > colors.xml
)
<color name="colorWhite">#FFF</color>
<!-- Black with 50% transparency -->
<color name="colorBlack">#80000000</color>
And expected output:
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
add a comment |
So, step by step in Kotlin.
My json file (in res > raw > people.json
):
"0":
"image_path": "https://images.unsplash.com/photo-1485199692108-c3b5069de6a0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 1"
,
"1":
"image_path": "https://images.unsplash.com/photo-1520065949650-380765513210?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 2"
,
"2":
"image_path": "https://images.unsplash.com/photo-1547513609-d80733850d2e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 3"
,
"3":
"image_path": "https://images.unsplash.com/photo-1550094195-2234fa4e2128?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 4"
,
"size": "4"
Layout for Activity (res > layout > activity_main.xml
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2" />
</LinearLayout>
Our main activity of the appliaction (MainActivity.kt
)
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupUi()
private fun setupUi()
// Read file content
val jsonFile = resources
.openRawResource(R.raw.images)
.bufferedReader()
.readText()
// Change "String" in to "Json Object"
val root = JSONObject(jsonFile)
// Get size of the list
val size = root.getString("size").toInt()
// Create (empty) list of people
val peopleList = mutableListOf<Person>()
// Loop to read all of the people
for (position in 0 until size)
val element = root.getJSONObject(position.toString())
// Change json in to Kotlin object
val photo = Person(
element.getString("image_path"),
element.getString("title")
)
// Add element to the list
peopleList.add(photo)
// Add adapter for RecyclerView
recycler_view.adapter = PeopleAdapter(this, peopleList)
Element of the list (res > layout > list_item.xml
)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="@id/image_view"
app:layout_constraintEnd_toEndOf="@id/image_view"
app:layout_constraintStart_toStartOf="@id/image_view" />
</android.support.constraint.ConstraintLayout>
Model class representing single element on the list (Person.kt
)
class Person(
val image: String,
val title: String
)
Adapter (using ViewHolder
pattern)
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.list_item.view.*
class PeopleAdapter(
private val context: Context,
private val listOfColor: List<Person>
) : RecyclerView.Adapter<PeopleAdapter.ViewHolder>()
// Creating elements (one item from list)
override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): ViewHolder
val view = LayoutInflater
.from(viewGroup.context)
.inflate(R.layout.list_item, viewGroup, false)
return ViewHolder(view)
// Number of elements
override fun getItemCount(): Int = listOfColor.size
// Fill elements with a data
override fun onBindViewHolder(row: ViewHolder, position: Int)
row.bind(position)
// Class responsible for one (single) row
inner class ViewHolder(private val item: View) : RecyclerView.ViewHolder(item)
fun bind(position: Int)
// Get image address
val person = listOfColor[position]
// Load image
Glide.with(context).load(person.image).into(item.image_view)
// Set text
item.title.text = person.title
Remember about three (3) dependencies
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation "com.github.bumptech.glide:glide:4.8.0"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
And (1) persmission in (manifest)
<uses-permission android:name="android.permission.INTERNET" />
Colors (in res > values > colors.xml
)
<color name="colorWhite">#FFF</color>
<!-- Black with 50% transparency -->
<color name="colorBlack">#80000000</color>
And expected output:
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
add a comment |
So, step by step in Kotlin.
My json file (in res > raw > people.json
):
"0":
"image_path": "https://images.unsplash.com/photo-1485199692108-c3b5069de6a0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 1"
,
"1":
"image_path": "https://images.unsplash.com/photo-1520065949650-380765513210?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 2"
,
"2":
"image_path": "https://images.unsplash.com/photo-1547513609-d80733850d2e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 3"
,
"3":
"image_path": "https://images.unsplash.com/photo-1550094195-2234fa4e2128?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 4"
,
"size": "4"
Layout for Activity (res > layout > activity_main.xml
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2" />
</LinearLayout>
Our main activity of the appliaction (MainActivity.kt
)
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupUi()
private fun setupUi()
// Read file content
val jsonFile = resources
.openRawResource(R.raw.images)
.bufferedReader()
.readText()
// Change "String" in to "Json Object"
val root = JSONObject(jsonFile)
// Get size of the list
val size = root.getString("size").toInt()
// Create (empty) list of people
val peopleList = mutableListOf<Person>()
// Loop to read all of the people
for (position in 0 until size)
val element = root.getJSONObject(position.toString())
// Change json in to Kotlin object
val photo = Person(
element.getString("image_path"),
element.getString("title")
)
// Add element to the list
peopleList.add(photo)
// Add adapter for RecyclerView
recycler_view.adapter = PeopleAdapter(this, peopleList)
Element of the list (res > layout > list_item.xml
)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="@id/image_view"
app:layout_constraintEnd_toEndOf="@id/image_view"
app:layout_constraintStart_toStartOf="@id/image_view" />
</android.support.constraint.ConstraintLayout>
Model class representing single element on the list (Person.kt
)
class Person(
val image: String,
val title: String
)
Adapter (using ViewHolder
pattern)
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.list_item.view.*
class PeopleAdapter(
private val context: Context,
private val listOfColor: List<Person>
) : RecyclerView.Adapter<PeopleAdapter.ViewHolder>()
// Creating elements (one item from list)
override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): ViewHolder
val view = LayoutInflater
.from(viewGroup.context)
.inflate(R.layout.list_item, viewGroup, false)
return ViewHolder(view)
// Number of elements
override fun getItemCount(): Int = listOfColor.size
// Fill elements with a data
override fun onBindViewHolder(row: ViewHolder, position: Int)
row.bind(position)
// Class responsible for one (single) row
inner class ViewHolder(private val item: View) : RecyclerView.ViewHolder(item)
fun bind(position: Int)
// Get image address
val person = listOfColor[position]
// Load image
Glide.with(context).load(person.image).into(item.image_view)
// Set text
item.title.text = person.title
Remember about three (3) dependencies
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation "com.github.bumptech.glide:glide:4.8.0"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
And (1) persmission in (manifest)
<uses-permission android:name="android.permission.INTERNET" />
Colors (in res > values > colors.xml
)
<color name="colorWhite">#FFF</color>
<!-- Black with 50% transparency -->
<color name="colorBlack">#80000000</color>
And expected output:
So, step by step in Kotlin.
My json file (in res > raw > people.json
):
"0":
"image_path": "https://images.unsplash.com/photo-1485199692108-c3b5069de6a0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 1"
,
"1":
"image_path": "https://images.unsplash.com/photo-1520065949650-380765513210?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 2"
,
"2":
"image_path": "https://images.unsplash.com/photo-1547513609-d80733850d2e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 3"
,
"3":
"image_path": "https://images.unsplash.com/photo-1550094195-2234fa4e2128?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80",
"title": "Girl 4"
,
"size": "4"
Layout for Activity (res > layout > activity_main.xml
)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.GridLayoutManager"
app:spanCount="2" />
</LinearLayout>
Our main activity of the appliaction (MainActivity.kt
)
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONObject
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupUi()
private fun setupUi()
// Read file content
val jsonFile = resources
.openRawResource(R.raw.images)
.bufferedReader()
.readText()
// Change "String" in to "Json Object"
val root = JSONObject(jsonFile)
// Get size of the list
val size = root.getString("size").toInt()
// Create (empty) list of people
val peopleList = mutableListOf<Person>()
// Loop to read all of the people
for (position in 0 until size)
val element = root.getJSONObject(position.toString())
// Change json in to Kotlin object
val photo = Person(
element.getString("image_path"),
element.getString("title")
)
// Add element to the list
peopleList.add(photo)
// Add adapter for RecyclerView
recycler_view.adapter = PeopleAdapter(this, peopleList)
Element of the list (res > layout > list_item.xml
)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_margin="10dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="@color/colorBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
app:layout_constraintBottom_toBottomOf="@id/image_view"
app:layout_constraintEnd_toEndOf="@id/image_view"
app:layout_constraintStart_toStartOf="@id/image_view" />
</android.support.constraint.ConstraintLayout>
Model class representing single element on the list (Person.kt
)
class Person(
val image: String,
val title: String
)
Adapter (using ViewHolder
pattern)
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.list_item.view.*
class PeopleAdapter(
private val context: Context,
private val listOfColor: List<Person>
) : RecyclerView.Adapter<PeopleAdapter.ViewHolder>()
// Creating elements (one item from list)
override fun onCreateViewHolder(viewGroup: ViewGroup, p1: Int): ViewHolder
val view = LayoutInflater
.from(viewGroup.context)
.inflate(R.layout.list_item, viewGroup, false)
return ViewHolder(view)
// Number of elements
override fun getItemCount(): Int = listOfColor.size
// Fill elements with a data
override fun onBindViewHolder(row: ViewHolder, position: Int)
row.bind(position)
// Class responsible for one (single) row
inner class ViewHolder(private val item: View) : RecyclerView.ViewHolder(item)
fun bind(position: Int)
// Get image address
val person = listOfColor[position]
// Load image
Glide.with(context).load(person.image).into(item.image_view)
// Set text
item.title.text = person.title
Remember about three (3) dependencies
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation "com.github.bumptech.glide:glide:4.8.0"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
And (1) persmission in (manifest)
<uses-permission android:name="android.permission.INTERNET" />
Colors (in res > values > colors.xml
)
<color name="colorWhite">#FFF</color>
<!-- Black with 50% transparency -->
<color name="colorBlack">#80000000</color>
And expected output:
edited Mar 9 at 0:20
answered Mar 9 at 0:14
BokenBoken
1,6491119
1,6491119
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
add a comment |
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
wow the effort thanks will check it ou
– o3203823
Mar 9 at 0:40
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
Please accept answer or ask about something unclear 😉
– Boken
Mar 9 at 8:15
add a comment |
you can use this as a reference
https://stackoverflow.com/a/10593838/7586266
the idea is for you to iterate over the JSON objects you have "0" , "1" ,"2" ... etc
in order to do that go within the json object just like you entered size in here
jObj.getString("size")
and you can use the i in your for loop in order to get the jObj which contains your url
like this
JSONObject jObj = json.getJSONObject(Integer.toString(i)); // where json is your whole json and you are getting the object "0","1" and "2" from it
string image_path_variable = jObj .getString("image_path");
and you can parse your JObj at i to a JSONObject which you can get the "image_path"
once you are able to do that you can use
Picasso.get().load(image_path_variable).into(your_desired_image_view);
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
add a comment |
you can use this as a reference
https://stackoverflow.com/a/10593838/7586266
the idea is for you to iterate over the JSON objects you have "0" , "1" ,"2" ... etc
in order to do that go within the json object just like you entered size in here
jObj.getString("size")
and you can use the i in your for loop in order to get the jObj which contains your url
like this
JSONObject jObj = json.getJSONObject(Integer.toString(i)); // where json is your whole json and you are getting the object "0","1" and "2" from it
string image_path_variable = jObj .getString("image_path");
and you can parse your JObj at i to a JSONObject which you can get the "image_path"
once you are able to do that you can use
Picasso.get().load(image_path_variable).into(your_desired_image_view);
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
add a comment |
you can use this as a reference
https://stackoverflow.com/a/10593838/7586266
the idea is for you to iterate over the JSON objects you have "0" , "1" ,"2" ... etc
in order to do that go within the json object just like you entered size in here
jObj.getString("size")
and you can use the i in your for loop in order to get the jObj which contains your url
like this
JSONObject jObj = json.getJSONObject(Integer.toString(i)); // where json is your whole json and you are getting the object "0","1" and "2" from it
string image_path_variable = jObj .getString("image_path");
and you can parse your JObj at i to a JSONObject which you can get the "image_path"
once you are able to do that you can use
Picasso.get().load(image_path_variable).into(your_desired_image_view);
you can use this as a reference
https://stackoverflow.com/a/10593838/7586266
the idea is for you to iterate over the JSON objects you have "0" , "1" ,"2" ... etc
in order to do that go within the json object just like you entered size in here
jObj.getString("size")
and you can use the i in your for loop in order to get the jObj which contains your url
like this
JSONObject jObj = json.getJSONObject(Integer.toString(i)); // where json is your whole json and you are getting the object "0","1" and "2" from it
string image_path_variable = jObj .getString("image_path");
and you can parse your JObj at i to a JSONObject which you can get the "image_path"
once you are able to do that you can use
Picasso.get().load(image_path_variable).into(your_desired_image_view);
edited Mar 8 at 23:21
answered Mar 8 at 23:15
Hossam Eldeen OnsyHossam Eldeen Onsy
596
596
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
add a comment |
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
i updated my question
– o3203823
Mar 8 at 23:25
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
@o3203823 are you using a recyclerview ? or in other words , adapter to generate imageviews ?
– Hossam Eldeen Onsy
Mar 8 at 23:35
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
no i am not but i will give it a shot
– o3203823
Mar 8 at 23:37
add a comment |
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject itemObj = jObj.get(Integer.valueOf(i)).getAsJsonObject();
String path = itemObj.getString("image_path")
ImageView view = new ImageView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layout.addView(view, params);
Picasso.get().load(path).into(view);
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
add a comment |
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject itemObj = jObj.get(Integer.valueOf(i)).getAsJsonObject();
String path = itemObj.getString("image_path")
ImageView view = new ImageView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layout.addView(view, params);
Picasso.get().load(path).into(view);
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
add a comment |
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject itemObj = jObj.get(Integer.valueOf(i)).getAsJsonObject();
String path = itemObj.getString("image_path")
ImageView view = new ImageView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layout.addView(view, params);
Picasso.get().load(path).into(view);
RelativeLayout layout= (RelativeLayout) getView().findViewById(R.id.relLayout);
JSONObject jObj = new JSONObject(response);
int size = Integer.parseInt(jObj.getString("size"));
for(int i = 0; i < size; i++)
JSONObject itemObj = jObj.get(Integer.valueOf(i)).getAsJsonObject();
String path = itemObj.getString("image_path")
ImageView view = new ImageView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layout.addView(view, params);
Picasso.get().load(path).into(view);
edited Mar 8 at 23:52
answered Mar 8 at 23:19
BahmanBahman
1,3012717
1,3012717
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
add a comment |
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i think i wasnt clear so my layout will have multiple image view depending on what the for loop has example of there are 3 images the page will have 3 images
– o3203823
Mar 8 at 23:23
i updated my question
– o3203823
Mar 8 at 23:25
i updated my question
– o3203823
Mar 8 at 23:25
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
bro i appreciate your answer but i dont think you are understanding my questions so what i am saying the my json wont always have 3 images somtimes it will have more then 3 or some times less so the image view needs to be created in the for loop it self thank you so much
– o3203823
Mar 8 at 23:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072223%2fset-images-in-image-in-a-loop-with-picasso%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there any way to modify
json
structure to make it much easier to parse? My proposal is to have array ([ ... ]
) of objects (...
). After that you no neededsize
and keys like0
,1
,...
.– Boken
Mar 8 at 23:18
i updated my question
– o3203823
Mar 8 at 23:25
But you did not respond to my question. Current json structure is not so good.
– Boken
Mar 8 at 23:27
@Boken i can but m not sure i want to because it is very complicated at the moment
– o3203823
Mar 8 at 23:34
1
OK, so how many element can have this json? And How layout should look like when file will have e.g. 20 elements? Are you expecting 20 ImageViews? If yes, you should use
RecyclerView
– Boken
Mar 8 at 23:36