String array of image URL in Android GridView - android

I have an array of photos that has been filled with url obtained from webservice.
Now i want to display the photos into a gridview, but i can seem to understand how you do it..
This is the code that i have simplified:
public GridView grid1;
public String photos[];
grid1 = (GridView)root.findViewById(R.id.gridphotos);
grid1.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,photos ));
grid1.setTextFilterEnabled(true);
But it only appears the links text....
Can i use WebView to fill the adapter?
Thanks

You need to create your own adapter either by extending ArrayAdapter or BaseAdapter.
Check out the sample here.. http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html

Related

How add photo to recycleview using Android kotlin firebase

Using Android kotlin firebase recycle view
I want to make the user take a picture of his phone or get a photo from gallery and add it to recyclerView list , and every time he takes a photo the recycler list increase automatically it works if i get the photo from drawable now i want to get it from camera or gallery. how to achieve it thanks
Please refer to the documentation https://developer.android.com/guide/topics/ui/layout/recyclerview, under section Add a list adapter you can read a following information :
To feed all your data to the list, you must extend the RecyclerView.Adapter class. This object creates views for items, and replaces the content of some of the views with new data items when the original item is no longer visible.
So, basically you have to create adapter for your recycler view, from which you can insert new item (photo) to you recyclerview.
More information about implementation the functionality is here:
How to update RecyclerView Adapter Data?
To capture/select photo from your gallery first you need permission accessing internal/external storage and camera I suggest you to read this https://developer.android.com/training/permissions/requesting
If you are willing to use Firebase to get these drawable/imgs, so you've to get the URL of the image as a string and put it in ArrayList/List.
Then your adapter class get URL and then use bitmap to download the image and convert it to bitmap (data type), How to load an ImageView by URL in Android?
Now when retrieving data from fire base: https://firebase.google.com/docs/database/admin/retrieve-data
add the string you got from the database in your arraylist then usenotifyOnDataChanged() to update your recyclerview.
How to update RecyclerView Adapter Data?

String Image Url set in imageview Android

I Need a help for My First develop android app. I want to set a image with the help of url in my ImageView which is coming from google Api. I have a listView in Which the data are coming from google apis. in Listview, I implement a image with the help of picasso android libruary. I am using a fragment when the user click on each list item from listView. On CLick of listItem I am opening a fragment on Which I need To display a data of list Item.
For ListView, I am using a userDefined List in which the images are of type
In DetailFragment where I want to display the details I am using the below Source Code.
#Override
public void findViewById() {
foodtitle=(TextView)mView.findViewById(R.id.detail_foodtext);
foodsubtitle=(TextView)mView.findViewById(R.id.detail_foodsubtext);
foodresImage=(ImageView)mView.findViewById(R.id.detail_image_left);
foodaddress=(TextView) mView.findViewById(R.id.detail_foodaddress);
Bundle b=getArguments();
b.getString("ItemName");
b.getString("ItemSubItem");
b.getInt("ItemImage");
b.getString("ItemAddress");
foodtitle.setText(b.getString("ItemName"));
foodsubtitle.setText(b.getString("ItemSubItem"));
foodresImage.setImageResource(b.getInt("ItemImage"));
foodaddress.setText(b.getString("ItemAddress"));
}
on This place
foodresImage.setImageResource(b.getInt("ItemImage"));
I want my Image which is one imageView in detail Fragment.
Since you are using Picasso then you should use it like:
Picasso.with(getContext()).load(fb.getString("ItemImage")).into(foodresImage);
You are using fb.getInt() in your setImageResource and you are getting a string from the Google API. You cannot convert the link to an int resource.

Why do we use single adapter per class in android?

I have just started learning android using Big Nerd Ranch Guide. In a project, we were to implement a listView and get the data from an array using an ArrayAdapter, as the crime objects were stored in some list.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.crimes_title);
mCrimes = CrimeLab.get(getActivity()).getCrimes();
ArrayAdapter<Crime> adapter =
new ArrayAdapter<Crime>(getActivity(),
android.R.layout.simple_list_item_1,
mCrimes);
setListAdapter(adapter);
}
After that, we were to create custom list to display the title as well as the checkbox and date so we created a custom layout. Now, we used a different adapter for that purpose.
private class CrimeAdapter extends ArrayAdapter<Crime> {
public CrimeAdapter(ArrayList<Crime> crimes) {
super(getActivity(), 0, crimes);
}
My question is why cant we use the same adapter that we used earlier?I mean we can just give it new values or create a new object.I searched the internet and found that we can create multiple adapters with some restrictions. why we extend it to ArrayAdapter? cant we just do it like we did previously by creating the adapter?
Please explain in detail, as I have just started.
Thanks.
Let us talk about the list view adapter implementation in android first of all in your first example you used the default implementation of listview adapter in android which provide you with list view of text
but most of the time this is not enough for your user interface you want to add an image a checkbox like in your second example so android OS gives you the ability to make your own custom cell (each row in list) that's why you would find yourself in a need to extend array adapter
in your first example you used
android.R.layout.simple_list_item_1
which is a layout containing only textview
and you provide a data source mCrimes
so when you extend array adapter you do the same you provide your own layout which represent the cell and a data source which will populate your list
hope that clarify it for you and welcome aboard :)

ArrayAdapter : Custom array adapter for list view with image view keeps loading wrong images

I have a Custom Array Adapter For a list view that contains an Image View. In the custom adapter class I have an asynctask that Downloads a unique picture from a unique url of each item in the list . The problem is the ImageViews for each item keeps changing pictures continously . Please what is there any problem with getting Images from urls through an AsyncTask in a custom array adapter class .
Help is needed (In any form)
Take a look of those libraries that can help you :)
Picasso
Universal Image Loader
The problem with starting an asynchronously loading things for a ListView (or any other adapter sourced view) is that Views can get reused once they are off the screen. The easiest way to deal with this is to use a library to do it for you, my favorite is AQuery
If you don't want to go that route, you'll have to role your own. To do this, you will need to check if the view at the completion of the AsyncTask is showing the same stuff as when it started. Call setTag(tag) on the View you're returning in the adapter's getView(). Then before you set the image to the ImageView, call getTag() on the view and check it equals the same object as what you set it as. It should look something like this:
final View view = MY VIEW;
final Object tag = new Object();
view.setTag(tag);
new AsyncTask() {
...
public void onPostExecute(Bitmap bar) {
if(tag.equals(view.getTag()) {
imageView.setImage(bar);
}
}
...
}.execute();
return view;
And then you can start caching and things like that. Complex right? So yeah, you should use a library.

Android Gridview update only one Element from outside the ImageAdapter

I have a gridview using an image adapter for the elements,
it's implemented like in this example:
http://developer.android.com/resources/tutorials/views/hello-gridview.html
Additionally I added a click listener for each item in the getView method, which sends the clicked position to the main class (outside of ImageAdapter) using a handler.
Now I want to update only the concerned imageView, but:
I don't know how to get the imageView outside of the ImageAdapter class (send with the handler? It's not serializable - create a buffer in ImageAdapter and getter?)
I'm not sure which method to use to change the image.
Currently I'm updating the whole grid each time:
((ImageAdapter)gridview.getAdapter()).setImages(imageIds);
gridview.invalidate();
ImageAdapter:
public void setImages(int[] images) {
mImages = images;
notifyDataSetChanged();
}
Thanks in advance
What you are currently doing is somewhat correct.
As an optimization, you could use an ArrayList<int> as the images and create a method in your adapter class to modify the value of a given index using ArrayList.set(int index, E element). Then calling notifyDataSetChanged() method should "theoretically" update the changed image view only :)

Categories

Resources