How can I create an array of Images.? - java

I want to create an array which contains number of images. Later on I have to use that array in a loop in my code. Can anyone suggest if I can create an array of images.?

define a array of image id like this
int[] p = {R.drawable.image1, R.drawable.image2....}
now for different condition use member of this array like
yourbutton.setBackgroundResource(p[0]) // or p[1]
or you can use ENUM to make it more readable..

this will solve you problem:
int imageArray[] = new int[number_of_images];
for (int i = 0; i < numImages; i++)
imageArray[i] = getDrawableId(getApplicationContext(),"R.drawable." + image_names[i]);

You can define an array of image filenames :
String fileNames[] = {"temp.jpg", "sample_img28.jpg", "normImg.jpg", "drawing.png", "film.png"};
and add those in MeidaTracker while initializing the application
MediaTracker tracker = null;
public void init() {
tracker = new MediaTracker(this);
for(int i = 0; i < fileNames.length; i++) {
System.out.println(" path :"+this.getCodeBase());
image[i] = getImage(this.getCodeBase(),fileNames[i]);
image[i] = image[i].getScaledInstance(256, 256, Image.SCALE_SMOOTH);
tracker.addImage(image[i], i);
}
try {
tracker.waitForAll();
}
}

Create your own customImageAdapter extending ArrayAdapter.
Sample code for your arrayAdapter:
public class CustomImageAdapter extends BaseAdapter{
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public CustomImageAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// if you want to display the image modify the content according o your need
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);//thumb image
return vi;
}

Related

How do I shuffle around my images in the array? [duplicate]

This question already has answers here:
Random shuffling of an array
(31 answers)
Closed 2 years ago.
I'm working in Java code, but I can not figure out how to shuffle my images and text. Here is all of my coding. They are in different classes so I will post them separately. This is in Android Studio.
This is my ImageandTextAdapter code:
public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter (Context ctx, int ViewResourceId, String[] strings, TypedArray icons) {
super(ctx, ViewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = ViewResourceId;
}
#Override
public int getCount () { return mStrings.length; }
#Override
public String getItem(int position) {return mStrings[position]; }
#Override
public long getItemId(int position) {return 0; }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.textView);
tv.setText(mStrings[position]);
return convertView;
}
}
This is my picture array code:
<resources>
<array name="card_faces">
<item>#drawable/b01</item>
<item>#drawable/b02</item>
<item>#drawable/b03</item>
<item>#drawable/b04</item>
<item>#drawable/b05</item>
<item>#drawable/b06</item>
<item>#drawable/b07</item>
<item>#drawable/b08</item>
</array>
</resources>
This is my main activity code:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
Resources resources = context.getResources();
String[] card = {"Blue1", "Blue2", "BlueSnake", "Blue4", "Blue5", "Blue6", "Blue7", "BlueHawk"};
TypedArray card_faces = resources.obtainTypedArray(R.array.card_faces);
setListAdapter(new ImageAndTextAdapter (context, R.layout.secondary_layout, card, card_faces));
}
}
I want to shuffle my pictures which are set in the array, but I am not able to with the Collections.shuffle command. If you know of a way that I could shuffle my pictures using a different and simple method, please help.
You can do it by converting the array to list and shuffle and then convert back to array
String[] card = {"Blue1", "Blue2", "BlueSnake", "Blue4", "Blue5", "Blue6", "Blue7", "BlueHawk"};
List<String> cardList = Arrays.asList(card);
Collections.shuffle(cardList);
cardList.toArray(card);
if you don't want to use more space you can try using random number generator to shuffle manually.
private static <T> void shuffle(T[] input){
Random rand = new Random();
for (int i = 0; i < input.length; i++) {
swap(input, i, rand.nextInt(input.length));
}
}
private static <T> void swap(T[] input, int x, int y){
T tmp = input[x];
input[x] = input[y];
input[y] = tmp;
}
Edit
If you want to shuffle the images which are inside a TypedArray, Instead of shuffling the string you can simply create an array of int with index and shuffle them then use its position for both string and images.
don't shuffle the string
List<Integer> indexes = new ArrayList<>();
for(int i = 0; i < indexes.length; i++){
indexes.add(i);
}
Collections.shuffle(indexes);
While using
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
int newPosition = indexes.get(position);
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
iv.setImageDrawable(mIcons.getDrawable(newPosition));
TextView tv = (TextView)convertView.findViewById(R.id.textView);
tv.setText(mStrings[newPosition]);
return convertView;
}

How do I convert my BaseAdapter to a RecyclerAdapter

So I wanted to change my GridView to a RecyclerView and with that I had to change my BaseAdapter to a RecyclerAdapter.
I already tried making changes, but I have no clue how to switch the code into the RecyclerAdapter.
Here is how my BaseAdapter looks like :
class AlbumAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap< String, String >> data;
public AlbumAdapter(Activity a, ArrayList < HashMap < String, String >> d) {
activity = a;
data = d;
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
AlbumViewHolder holder = null;
if (convertView == null) {
holder = new AlbumViewHolder();
convertView = LayoutInflater.from(activity).inflate(
R.layout.album_row, parent, false);
holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
holder.gallery_count = (TextView) convertView.findViewById(R.id.gallery_count);
holder.gallery_title = (TextView) convertView.findViewById(R.id.gallery_title);
convertView.setTag(holder);
} else {
holder = (AlbumViewHolder) convertView.getTag();
}
holder.galleryImage.setId(position);
holder.gallery_count.setId(position);
holder.gallery_title.setId(position);
HashMap < String, String > song = new HashMap < String, String > ();
song = data.get(position);
try {
holder.gallery_title.setText(song.get(Function.KEY_ALBUM));
holder.gallery_count.setText(song.get(Function.KEY_COUNT));
Glide.with(activity)
.load(new File(song.get(Function.KEY_PATH))) // Uri of the picture
.into(holder.galleryImage);
} catch (Exception e) {}
return convertView;
}
}
class AlbumViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
}
And thanks in advance !
Simply use this, feel free to modify to suit your need, make sure to read the comments as they really will help you understand few things.
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {
private static final String KEY_ALBUM = "KEY_ALBUM";
private static final String KEY_COUNT = "KEY_COUNT";
private static final String KEY_PATH = "KEY_PATH";
private itemClickInterface clickInterface;
//private List<String> data;
private ArrayList<HashMap<String, String >> data;
// public AlbumAdapter(itemClickInterface clickInterface, List<String> data) { // Forget about this if your data is not an array of Strings.
// public AlbumAdapter(itemClickInterface clickInterface, ArrayList<HashMap< String, String >> data) { // Forget about this if you're not ready to pass an onclick interface
public AlbumAdapter(ArrayList<HashMap< String, String >> data) {
this.data = data;
// this.clickInterface = clickInterface; //Simply ignore since you're not passing an interface of clicklister
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
try {
final String data_for_albums = this.data.get(position).get(KEY_ALBUM);
final String data_for_counts = this.data.get(position).get(KEY_COUNT);
final String data_for_paths = this.data.get(position).get(KEY_PATH);
holder.gallery_title.setText(data_for_albums);
holder.gallery_count.setText(data_for_counts);
Glide.with(activity)
.load(new File(data_for_paths)) // Uri of the picture
.into(holder.galleryImage);
/*
* You can modify this as you want.
* */
// holder.galleryImage.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// clickInterface.click(data_for_paths); // This depends on you.
// }
// });
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return data.size();
}
/*--------------------------------------------------------------------------------------
| GET REFERENCES TO VIEWS HERE
*--------------------------------------------------------------------------------------*/
class ViewHolder extends RecyclerView.ViewHolder {
ImageView galleryImage;
TextView gallery_count, gallery_title;
ViewHolder(View itemView) {
super(itemView);
galleryImage = itemView.findViewById(R.id.galleryImage);
gallery_count = itemView.findViewById(R.id.gallery_count);
gallery_title = itemView.findViewById(R.id.gallery_title);
}
}
}
How to call from our activity
ArrayList<HashMap<String, String >> data = new ArrayList<>();
//I'm assuming you already feed your data, so you're not passing null like me here.
cardlistview = findViewById(R.id.cardlistview);
albumAdapter = new AlbumAdapter(your_arraysOfHashMap);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
cardlistview.setLayoutManager(mLayoutManager);
cardlistview.setAdapter(albumAdapter);

Android Icon/Image setting pattern for multiple choices

What pattern do I have to use, if I have ListView in which ImageView and like 500 different icons that could be set on that ImageView. Should I just write If/Switch statement, or there is another way/pattern to do it?. Thanks in advance!
Let me assume that you know what icon(I mean the name of icon) to be loaded into the imageView and those icons are available in your drawable resource folder. In this case
#Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
DataItem dataItem = dataList.get(holder.getAdapterPosistion());
try {
int resID = activityContext.getResources().getIdentifier(dataItem.getIconName() , "drawable"/**resource folder name*/, activityContext.getPackageName());
holder.imageView.setBackgroundResource(resID);
} catch (Exception e) {
throw new RuntimeException("Error getting Resource ID.", e)
}
}
Where are these icons that you want to set? you are getting them from server or they are stored locally in your application file? or they are from user phone gallery?
Here is the code you want for your adapter:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<String> mIconNames;
public MyAdapter(Context context) {
mContext = context;
mIconNames = getIconNames();
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mIconNames.size();
}
#Override
public Object getItem(int position) {
return mIconNames.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get view for row item
View rowView = mInflater.inflate(R.layout.your_layout, parent, false);
ImageView thumbnailImageView =
(ImageView) rowView.findViewById(R.id.your_image_view_id);
Picasso.with(mContext).load(mIconNames.get(position)).placeholder(R.mipmap.ic_launcher).into(thumbnailImageView);
return rowView;
}
//this method builds your icon names
private ArrayList<String> getIconNames() {
ArrayList<String> iconNames = new ArrayList<>();
int numberOfIcons = 99;
String iconBaseName = "icon";
for (int i = 1; i < numberOfIcons; i++) {
iconNames.add(iconBaseName + i);
}
return iconNames;
}
}

Integrating current ListView with a Custom Adapter with ListView to allow multiple views

The current project I have includes a ListView with a Custom Adapter. However, I am now interested in adding multiple types of views to my ListView but after several attempts I have been unable to add the two sources of code together to successfully integrate them.
Article on ListView with multiple views: ListView Article for multiple views
The custom adapter in my current code retrieves the data from another class called getData which is referenced by "data".
Code from article (ListView with multiple views):
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 1; i < 50; i++) {
mAdapter.addItem("item " + i);
if (i % 4 == 0) {
mAdapter.addSeparatorItem("separator " + i);
}
}
setListAdapter(mAdapter);
}
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
Current code (ListView with custom adapter):
FragmentA.java
package com.example.newsapp;
public class FragmentA extends Fragment{
getData data = getData.getMyData();
public Integer ArticleID;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_a, container, false);
ListView listView = (ListView)V.findViewById(R.id.list)
CustomList adapter = new
CustomList(getActivity(), data.Headline.toArray(new String[data.Headline.size()]), data.Description.toArray(new String[data.Description.size()]), data.BitmapList.toArray(new Bitmap[data.BitmapList.size()]), data.ArticleID.toArray(new Integer[data.ArticleID.size()]));
listView.setAdapter(adapter);
listView.setOnItemClickListener(this); //Removed on click item event code.
return V;
}
CustomList.java
package com.example.newsapp;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] titleId;
private final String[] descriptionId;
private final Bitmap[] pictureid;
public CustomList(Activity context,
String[] Headline, String[] Description, Bitmap[] BitmapList, Integer[] ArticleID) {
super(context, R.layout.single_row, Headline);
this.context = context;
this.titleId = Headline;
this.descriptionId = Description;
this.pictureid = BitmapList;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.tvTitle);
TextView txtDescription = (TextView) rowView.findViewById(R.id.tvDescription);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivIcon);
txtTitle.setText(titleId[position]);
txtDescription.setText(descriptionId[position]);
imageView.setImageBitmap(pictureid[position]);
return rowView;
}
}
Edit:
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] titleId;
private final String[] descriptionId;
private final Bitmap[] pictureid;
public CustomList(Activity context,
String[] Headline, String[] Description, Bitmap[] BitmapList, Integer[] ArticleID) {
super(context, R.layout.single_row, Headline);
this.context = context;
this.titleId = Headline;
this.descriptionId = Description;
this.pictureid = BitmapList;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
int viewType = getItemViewType(position);
View rowView = null;
switch(viewType) {
case 0:
LayoutInflater inflater = context.getLayoutInflater();
rowView= inflater.inflate(R.layout.single_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.tvTitle);
TextView txtDescription = (TextView) rowView.findViewById(R.id.tvDescription);
ImageView imageView = (ImageView) rowView.findViewById(R.id.ivIcon);
txtTitle.setText(titleId[position]);
txtDescription.setText(descriptionId[position]);
imageView.setImageBitmap(pictureid[position]);
case 1:
LayoutInflater inflater2 = context.getLayoutInflater();
rowView= inflater2.inflate(R.layout.single_row_loadmore, null, true);
}
return rowView;
}
#Override
public int getViewTypeCount() {
return 2; // TODO make this a static final
}
#Override
public int getItemViewType(int position) {
return position % 2; // 0 or 1
}
}
First, a bit of an aside: you should create a class that encapsulates a headline, description, etc. and use an array/collection of those objects to back your adapter. It will be far easier than managing many disparate arrays of things, especially if one day you decide you need another attribute of an Article (its category, for example).
class Article {
int id;
String headline;
String description;
Bitmap picture;
}
With regard to your ListView, the magic happens in the methods getItemViewType() and getViewTypeCount(). In getViewTypeCount() you return the maximum number of row types -- the article you posted uses two row types and so returns 2. In getItemViewType() you return a value between zero and (viewTypeCount - 1) -- in the article, his implementation can return 0 or 1 because his viewTypeCount is 2.
How you decide which row type applies to each item is entirely up to you. If, for example, you wanted to simply alternate view types on every row, you can do this:
#Override
public int getViewTypeCount() {
return 2; // TODO make this a static final
}
#Override
public int getItemViewType(int position) {
return position % 2; // 0 or 1
}
In other applications you would probably inspect the item at the given position to help you determine what should be returned in getItemViewtype().
The reason this functionality exists is that getView() provides a parameter (called convertView) that is a row which has been recycled. In order to give you an appropriate convertView, ListView needs to first know what row type it was. When you want to implement getView() for an adapter with multiple row types, it generally looks something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
switch(viewType) {
case 0:
return setUpOneViewType(position, convertView, parent);
case 1:
return setUpAnotherViewType(position, convertView, parent);
}
}
Note the cases for the switch statement correspond to the possible values that can be returned from getItemViewType(). These could be static final members.
I highly suggest watching The World of ListView. That video covers this topic as well as how to properly use convertView in your adapter implementation.

How to put image in HashMap by giving a specific url

private static final String picpic = "picpic";
private ArrayList < HashMap < String, Object>> myBooks;
myBooks = new ArrayList<HashMap<String,Object>>();
HashMap < String, Object> hm;
hm = new HashMap<String, Object>();
drawable=LoadImage("http://www.wauhaha.com/smart/company/album/pic.jpg");
hm.put(picpic, drawable);
myBooks.add(hm);
final ListView listView = (ListView)findViewById(R.id.list);
SimpleAdapter adapter = new SimpleAdapter(this, myBooks, R.layout.listbox, new String[]{picpic}, new int[]{R.id.image1});
listView.setAdapter(adapter);
See this Example Listview With Custom Adapter.....And see CustomizedListView Activity
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
If you want the Key to be Url then use this code.
//Declarations
public Map<String, Object> ImageMap = new WeakHashMap<String, Object>();
//Inserting into HashMap
public void addToHash(String url)
{
ImageMap.put(url,LoadImage(url));
}
// Get from HashMap
public Object getImageFromHash(String url)
{
ImageMap.get(url);
}
EDITED
You can use the Solution given by Samir,
you need to use ImageLoader.java, ImageCache.java, FileCache.java and Utils.java
For your adapter
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<String> urls;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<String> u) {
activity = a;
urls=u;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listbox, null);
ImageView thumb_image=(ImageView)vi.findViewById(your.imageview.id); // thumb image
imageLoader.DisplayImage(urls.get(position), thumb_image);
return vi;
}
}

Categories

Resources