I am have a list (100's) of image urls which I want to display in a ListView together with text. The problem I am having is that my listview displays all the URLs as string and not IMAGE. I have implemented a BITMAP class and tested it with a sigle image url, and on an IMAGEVIEW, it worked. I have read some post but can't really figure it out working with ListView. How to load an ImageView by URL in Android?
In the my code below, imageUrls holds all the image urls as string and concatString holds img + text, which was used in the adapter. My question is how do I apply the BITMAP class on a STRING that holds all the urls before putting it in the adapter, that is imageUrls? Or is ther a better approach.
Bitmap bitmap;
String temp, json, namesImg, imageUrls, allImageANDnames;
StringBuffer sb = new StringBuffer();
ArrayList<String> concatString = new ArrayList<String>(); // ARRAYLIST THAT HOLDS IMG & TXT
JSONObject object;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
// Reading json file from assets folder
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"myjson.json")));
while ((temp = br.readLine()) != null)
sb.append(temp);
json = new String(sb);
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
// Creating JSONObject from JSONArray
object = jsonArray.getJSONObject(i);
namesImg = object.getString("name"); // holds all text
imageUrls = object.getString("url"); // holds all image urls (HERE IS WHERE I NEED HELP)
allImageANDnames = imageUrls + "\t\t" + namesImg; // holds text and img urls
concatString.add(allImageANDnames); //(I WANT THIS TO HOLD IMAGES & TEXT) not urls & text
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}finally {
if ((br != null)) {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}
}
// adapter to show list in activity
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, concatString); // I USED THE HOLDER HERE
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter); // LIST THAT POPULATE THE IMG & TXT
}
// HOW CAN I APPLY THIS CLASS ON ***imageUrls***
public class ImageFromUrl extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public GetImageFromUrl(ImageView img){
this.imageView = img;
}
@Override
protected Bitmap doInBackground(String... url) {
String stringUrl = url[0];
bitmap = null;
InputStream inputStream;
try {
inputStream = new java.net.URL(stringUrl).openStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
}
}
.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
</ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
Sorry I am ney on this.