ArrayIndexoutofboundException - android

Iam receving below error
java.lang.ArrayIndexOutOfBoundsException: length=55; index=55.
The application is crashing after some images are scrolled down could you please suggest where i need to make changes
import com.squareup.picasso.Picasso;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
int imageTotal = 70;
public static String[] mThumbIds = {
"http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg",
"http://scraps99.com/main/post_images/Mehndi-Design/Mehndi-Design_151.jpg",
"http://stylesatlife.com/wp-content/uploads/2016/03/Ganesha-Chaturthi.jpg",
"https://s-media-cache-ak0.pinimg.com/564x/0a/05/12/0a05120d03dbe15f66eb8c97c085d25c.jpg",
"http://srivinayagacatering.com/cook/upload/media/youtube_O9e0MZkXNxM.jpg",
"http://stylesatlife.com/wp-content/uploads/2016/03/Ganesha-Design-For-Hand.jpg",
"http://i.ethnicoapp.com/m/cOHL4_xuEP_99VVgLId-xD-Eyi7ktW4U8Ph7zi9GczJ6wQbrdHGhqi8yJi0TD3Loee_9_AV_386I7ym1VbImCkqhQZe53CeasIRvy8CItTlcooEvmyPFtenxNUNGHtOFYWJjZGVmZ2hiY2RlZmVmZw,,/ganesha-mehendi-design-1461583969.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/b6/95/44/b69544f3ab5e92a4e723e0017985a26b.jpg",
"http://thestylecircle.com/wp-content/uploads/2015/08/Organic-abstracts.jpg",
"http://www.fashionlady.in/wp-content/uploads/2015/05/Ganesh-Mehndi-Design1.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/62/b6/c8/62b6c82d7d022f1fc6f1352c649263cd.jpg",
"http://i.ethnicoapp.com/m/ao0TV_479pabydo3Al6jF8hNvORkvgC50AYfaZ2pymLoGtEN8Z5u1E7aRPbDsPKMnbGvBUhWipKqcyJ2qppTW0UhVCoqErG4AFTaS5Jwl_ntqN-3qq8eMjaYtlNzXLCJYWJjZGVmZ2hiY2RlZmVmZw,,/mehandi-design-57caf6c3952c2.jpg",
"http://67.media.tumblr.com/41e8e1f6271b1350faa387eb097270c9/tumblr_nv8amk46Pn1qdlbsvo1_1280.jpg",
"https://www.stylishandtrendy.com/wp-content/uploads/2013/09/ganesha-mehndi-designs.png",
"https://s-media-cache-ak0.pinimg.com/736x/f4/f2/36/f4f2362a195833e10b2ad77a17ae4b0d.jpg", "http://i.ethnicoapp.com/m/ZPkNdGld1RNi2p-Iw8e8bj5TGGCKeVx3QRzCh7bc_cWFlN3LvmYlBSaLCgFeS3PB_Y2LBrOwDkCZCsSHpHByavSVh7_Z3d3wEXHWehIJotXpx57SipEsOFYvdGBsvm3OYWJjZGVmZ2hiY2RlZmVmZw,,/simple-mehndi-design-576176bc176cb.jpg",
"http://3.bp.blogspot.com/-WX4WDgL4Q1w/UXJWD_gp9NI/AAAAAAAAAW8/xIKhBR4T5-w/s400/Latest-And-Simple-Arabic-Mehndi-Designs-For-Hands-2012-8.jpg",
"http://www.ozyle.com/wp-content/uploads/2013/07/Beautiful-Bridle-Mehndi-Designs-4.jpg",
"http://img.thebridalbox.com/wp-content/uploads/2016/05/A-Bouquet-Of-Beauty-pakistani-arabic-mehndi-designs.jpg",
"https://s-media-cache-ak0.pinimg.com/564x/9a/bf/99/9abf99999c494045bf8965b64493e2ce.jpg",
"http://fashionspk.net/wp-content/uploads/2013/08/New-Eid-Henna-Mehndi-Collection-2013-Female-Fashion-Designs-1-450x214.jpg",
"http://1.bp.blogspot.com/-8bC4hSwfNcs/VArSX5kIg9I/AAAAAAAAGgs/AiQoxDeTnE0/s1600/0d424e3a3846cd33652611cf018583b5.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/ed/4e/74/ed4e74c785378455236cb69063e8154b.jpg",
"https://lh3.googleusercontent.com/-pdEQcpmOJtc/Va84R8uMpkI/AAAAAAAABD8/LJqXlQsySts/w800-h800/ba3.jpg",
"https://4.bp.blogspot.com/-jzDRz30YAcA/V5affkyTIYI/AAAAAAAAB_o/bxzCpQvi5QM_GoRDWLWcA-sabZAHOCV3QCLcB/s640/20%2Bbest%2Bidea%2Babout%2Bmehndi%2Bdesigns%2Blatest%2Bfor%2Bhands%2B3.jpg",
"https://4.bp.blogspot.com/-RmtccTBjD0U/VzxaKHYPE2I/AAAAAAAAHKI/IX2YSTcLGGkll_dte1re9M1PCBrWV50CwCKgB/s1600/henna-arabic-mehndi-designs-2015-16.jpg",
"http://todayfashion.pk/wp-content/uploads/2016/04/Latest-Finger-Mehndi-Designs-Collection-2016-For-Girls-5.jpeg",
"http://pakifashion.com/wp-content/uploads/2014/06/Arabic-Mehndi-Design-For-Eid-5.jpg",
"http://pakistaniladies.com/wp-content/uploads/2014/09/Beautiful-Latest-Henna-Mehndi-Designs-for-Bride-Hands-Images-2015-Trends-2014-Pakistan-India-Banladesh-Srilanka-Facebook-pinterest-Collections.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/54/0d/32/540d32cb066fac59a665d555b7b4e791.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/09/d3/16/09d316cae4667de5fc043d61a6f45c30.jpg",
"http://www.mehmehndi.com/wp-content/uploads/2016/01/10-Easy-Simple-Arabic-Mehndi-Designs-For-Hands-In-2015-7.jpg",
"http://stylespk.com/wp-content/uploads/2013/05/Mehndi-Designs-collection-2013-9.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/87/ec/c1/87ecc185dfeed84eb4b52378862ac749.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/2b/0c/7f/2b0c7fdeacd199581da22042f949dd15.jpg",
"http://fashioninbox.com/wp-content/uploads/2015/07/Latest-Arabic-Eid-Mehndi-designs-2015-6.jpg",
"https://s-media-cache-ak0.pinimg.com/originals/8d/5b/ad/8d5bada1d6f7c83d804be2506bcf2541.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/aa/68/fb/aa68fb77c63c66bdfd236ef2b2d3c14e.jpg",
"http://i0.wp.com/mehndidesignimages.com/wp-content/uploads/2015/12/back-hand-half-side-arabic-mehndi-design.jpg",
"http://easyday.snydle.com/files/2014/02/easy-arabic-mehandi-designs.jpg",
"http://www.mymehndidesign.com/wp-content/uploads/2014/08/Simple-Mehndi-Designs6.jpg",
"http://kfoods.com/article/images/mehandi-design-07-01.jpeg",
"https://s-media-cache-ak0.pinimg.com/736x/4a/d4/04/4ad404abc0359eaa9b605f52f83f0744.jpg",
"https://2.bp.blogspot.com/-NG9rOvpHAlM/VyYS3zFyHYI/AAAAAAAADWQ/u_M1a9iOu3Ydj-vtJ_qPjf9iUW1IObByACLcB/s1600/New%2BStylish%2BMehndi%2BDesigns-22.jpg",
"http://www.mehndidesign.net/wp-content/uploads/Indian-Hands-Mehndi-Designs.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/cb/3f/f3/cb3ff31c393bb689ca769d32627f09ea.jpg",
"http://www.mymehndidesign.com/wp-content/uploads/2016/04/10-Best-and-Beautiful-Mehndi-Designs-for-Girls-Easyday6.jpg",
"http://www.abeautyclub.com/wp-content/uploads/2016/09/Finger-Eid-Mehndi-Designs-2016-For-Girls1-650x740.jpg",
"https://s-media-cache-ak0.pinimg.com/236x/68/7a/f1/687af13aff8c06615a09093ee9523b8d.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/8b/7d/44/8b7d44fe8579ba677a568a3a14dc1fb5.jpg",
"http://topmehndidesigns.com/wp-content/uploads/2016/04/Beautiful-Best-Simple-Arabic-Mehndi-Designs-for-Hands-2016-2017-Indian-Pakistani.jpg",
"http://pakistaniladies.com/wp-content/uploads/2016/05/New-Latest-Simple-Arabic-Eid-Mehndi-Designs-2016-2017-for-Hands-Indian-Pakistani-13.jpg",
"https://i.ytimg.com/vi/VjMyNU5ozh4/hqdefault.jpg",
"https://s-media-cache-ak0.pinimg.com/1200x/21/07/ef/2107ef2ff18c98a4dd2b8b370ed2a4f0.jpg",
"http://1.bp.blogspot.com/-eG7EHfBDoeA/VS-qp3VwnMI/AAAAAAAACmk/Kx8lIJIL-lo/s1600/New-arabic-mehndi-design-2015-2.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/93/89/5a/93895a42aa904fe91c2f5d7f258e3efe.jpg",
"http://topmehndidesigns.com/wp-content/uploads/2016/03/Simple-Fancy-Beautiful-Arabic-Bridal-Mehndi-Design-for-Hands-2016-2017-9.jpg",
"http://www.listfunda.com/wp-content/uploads/2016/09/2015-eid-mehndi-design-for-hands.jpg",
"http://www.mymehndidesign.com/wp-content/uploads/2016/05/Stylish-Mehndi-Designs-2016-For-Bridal-Wedding-Eid-Party5.jpg",
"http://ytimg.googleusercontent.com/vi/WhjvVcrOeSE/mqdefault.jpg",
"http://kms.ounousa.com/Content/ResizedImages/548/10000/inside/150810104225116.jpg",
"https://s-media-cache-ak0.pinimg.com/736x/19/63/9a/19639ae45916bb8a6491aa28ff0599d8.jpg",
"https://s-media-cache-ak0.pinimg.com/564x/c6/02/c1/c602c14135d4c7f95b54c97c6a43ec29.jpg",
"http://s7.postimg.org/9cxe0c0az/1291_647590481918937_1366870647_n.jpg",
"http://interestingmagazine.in/wp-content/uploads/2016/08/13686631_1751943998377719_2471147301102863183_n.jpg",
};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return imageTotal;
}
#Override
public String getItem(int position) {
return mThumbIds[position];
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(480, 480));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
String url = getItem(position);
Picasso.with(mContext)
.load(url)
.placeholder(R.drawable.loader)
.fit()
.centerCrop().into(imageView);
return imageView;
}
}
Im getting this Error which is
java.lang.ArrayIndexOutOfBoundsException: length=55; index=55
at com.shirisha.mehandi.ImageAdapter1.getItem(ImageAdapter1.java:86)
at com.shirisha.mehandi.ImageAdapter1.getView(ImageAdapter1.java:104)
at android.widget.AbsListView.obtainView(AbsListView.java:3065)
at android.widget.GridView.makeAndAddView(GridView.java:1463)
Any Help please...

Change the method getCount() into:
public int getCount() {
return mThumbIds.length();
}
Hope it helps!

It is because Arrays in java start from index 0 to index array.length-1 if length of array is 55 then last index of the array to be looped in should be 54.
This change will help you.
public int getCount() {
return mThumbIds.length();
}

You are using an Array. Then you will run into its 2 values that don't quite match up; Length and Index. Example:
ArrayList<String> my_array = new ArrayList<String>();
// Add stuff to it.
my_array.add("Ajeet");
my_array.add("Harry");
my_array.add("Chaitanya");
my_array.add("Steve");
my_array.add("Anuj");
At this point, my_array has a length of 5, but the last, "Anuj", has Index 4. So when you ask for the last item in an Array, you will have to use something like:
String last = my_array.get(my_array.length - 1);

Related

Android: Using two different Adapters for a Gridview grid

I was wondering if it is possible to apply two different Adapters (ArrayAdapter for a String array, and ImageAdapter for the background of each value in the array) for a certain 4X4 dimension grid… I ask this because my actual (the code below is just a sample) program contains user-inputted values for height and width (https://stackoverflow.com/questions/35382979/android-auto-fitting-row-height-of-gridview-based-on-user-inputted-values) as opposed to hardcoding. Say, if I wanted to assign the brown blocks as a background for the odd numbers, and the gray blocks for the even numbers using grid.setAdapter() for both Adapters... How would I code that using Android Studio?
Here's the following Java code for the grid:
package dpark.sample;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private int height, width;
String[] list;
GridView grid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
height = 4;
width = 4;
buildList();
grid = (GridView)findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, list);
grid.setAdapter(adapter);
grid.setNumColumns(width);
//***TEMPORARILY COMMENTING THE FOLLOWING OUT SINCE THIS WILL JUST OVERWRITE THE
// ARRAYADAPTER***
//grid.setAdapter(new ImageAdapter(getApplicationContext()));
}
private void buildList() {
int tempIncrementor = 1;
int dimensions = height * width;
list = new String[dimensions];
for (int i = 0; i < dimensions; i++) {
list[i] = String.valueOf(tempIncrementor);
tempIncrementor++;
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.brownblock,
R.drawable.grayblock
};
}
}
... Which outputs in the virtual emulator:
... As for outputting the color block images, I temporarily commented out the ArrayAdapter blocks in my code so I could output the following:
No you can't set two adapters beacuse the method setAdapter overrides the previous one.
But your goal doesn't require it, you should store the values in arraylist and retrieve them in the getView method.

Shouldn't I be able to use an Integer[] array in a switch statement?

I thought Integer[] arrays are fine to use in a switch statement, due to auto-boxing, where the Integer object and int primitive are exchanged back and forth automatically by the compiler. But I am not having luck putting my Integer[] items into a switch statement for an OnClick event. When the user clicks an image in my array, I want it to open in a new activity. What am I doing wrong? Getting red errors on my code, in both the case numbers and the startActivity(i) so it's definitely not right. Thanks.
GridViewSitesAdapter.java
package org.azurespot.cutelinks;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import org.azurespot.R;
/**
* Created by mizu on 2/11/15.
*/
public class GridViewSitesAdapter extends BaseAdapter {
public Context mContext;
public GridViewSitesAdapter(Context c) {
mContext = c;
}
// Keep all Images in array
public Integer[] mThumbIds = new Integer[]{
R.drawable.cute_overload, R.drawable.attack_of_the_cute,
R.drawable.zoo_borns, R.drawable.cutest_paw,
R.drawable.mochimochiland, R.drawable.baby_mugging,
R.drawable.cutest_food, R.drawable.tiny_cute_things,
R.drawable.etsy_robot_plush,
R.drawable.fuck_yeah_fruits,
R.drawable.ark_in_space,
R.drawable.we_heart_it
};
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams
(270,270));
imageView.setBackgroundResource(R.drawable.gridview_image_border);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (mThumbIds){
case 0:
Intent i = new Intent(mContext, CuteOverloadActivity.class);
startActivity(i);
break;
case 1:
}
}
}
});
return imageView;
}
}
Your switch statement shouldn't be on mThumbIds but on position (as it is the position of the element which determines what to do). Note you'll need to declare position as final to access it within the onClick method.
Use setTag/getTag method to get actual position of clicked row:
imageView.setTag(position);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int clickedPos=Integer.parseInt(v.getTag().toString());
switch (clickedPos){
case 0:
....
}
}
}
});

Page Curl. Switc between activities

I downloaded the page curl project in github to apply it on my project using gridview. Once I click the image in gridview it will go to another activity in which there are arrays of image which I will pass the id from my gridview to my fullview activity using bundle. I want to use the page curl animation to slide between image in fullview so that the use will not have to go back to gridview just to see another image. But I can't pass an Integer to an ARRAYLIST. Here are the snippets:
This is my pagecurl adapter in this link: http://pastebin.com/JCq2YD6r
and my Base adapter for my gridview is here:
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.r01,R.drawable.r02
,R.drawable.r03,R.drawable.r04,
R.drawable.r05,R.drawable.r06,
R.drawable.r07,R.drawable.r08,
R.drawable.r09,R.drawable.r10,
R.drawable.r11,R.drawable.r12
};
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(MainTab.current_act);
int dpw = app_preferences.getInt("dpiw", 0);
Log.i("IMmmm" , "" + dpw);
// if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mThumbIds[position]);
// int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, parent.getResources().getDisplayMetrics());
// int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 175, parent.getResources().getDisplayMetrics());
final float scale = parent.getResources().getDisplayMetrics().density;
int width = (int) (100 * scale + 0.5f);
int height = (int) (150 * scale + 0.5f);
Log.i("with h " , ""+width + " " + height + " scale " + scale );
imageView.setLayoutParams(new GridView.LayoutParams(width, height));
//}
// } else {
// imageView = (ImageView) convertView;
// }
return imageView;
}
}
This is the part i want to change in page curl adapter
// Create pages
mPages = new ArrayList<Bitmap>();
mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
How can I pass the Array of Integer to this arraylist the is bitmap? And by the way what do you call this? Arraylist? Is that arraylist of a bitmap?? :)
Check this example: http://devappandroid.com/android-beginner/page-curlturn-effect-android
// Create pages
mPages = new ArrayList<Bitmap>();
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page1));
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page2));
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page3));
mPages.add(BitmapFactory.decodeResource(getResources(),
R.drawable.page4));

values arent being passed to intent

Stack Trace
Picture of stack trace here
Values aren't being passed to the other intent. Every time I try to start the activity with the intent in it, it crashes.
//This activity will retrieve and display the different rewards that are available.
public class RewardsActivity extends Activity {
#Override
public void onCreate(Bundle SavedInstanceState)
{
super.onCreate(SavedInstanceState);
setContentView(R.layout.rewards);
//stores retrieves and stores the current gridview
GridView gridView = (GridView)findViewById(R.id.grid_view);
//Instance of ImageAdapter class that will load the images into the gridview
gridView.setAdapter(new ImageAdapter(this));
//This function is used to set a listener on each item in the grid so that when its clicked it will go to the other view.
gridView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
Intent i = new Intent(getApplicationContext(),RewardsViewActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
This new intent is being passed to, when it's passed here it is stored in a variable then used in a ImageView to load an image.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class RewardsViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_view);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image);
imageView.setImageResource(imageAdapter.finalImages[position]);
}
}
ImageAdapter.java
package org.android.pps;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/*This class will be used handle the loading of the image view that will display all the
images of the rewards. (this will be used along with RewardsActivity and rewards.xml)
*/
public class ImageAdapter extends BaseAdapter {
//variable that will store the current context of the application
private Context c;
private Integer num = 6;
private int[] rewards_num=new int[num];
private Integer[] Images = new Integer[6];
public Integer[] finalImages;
//for loop will set the correct image to the array if its either activated or deactivated
public Integer[] fillImageArray()
{
//Array that will be used to show the reward images
Integer[] Activated ={
R.drawable.rewards1,
R.drawable.rewards2,
R.drawable.rewards3,
R.drawable.rewards4,
R.drawable.rewards5,
R.drawable.rewards6,
};
Integer[] Deactivated ={
R.drawable.rewards1b,
R.drawable.rewards2b,
R.drawable.rewards3b,
R.drawable.rewards4b,
R.drawable.rewards5b,
R.drawable.rewards6b,
};
//for loop that checks to see all the rewards that a particular users has to assign a particular image.
for(int x = 0;x<rewards_num.length;x++)
{
for(int y = 0;y<6;y++)
{
if(rewards_num[x]==y)
{
Images[x]=Activated[y];
}
else
{
Images[x]=Deactivated[y];
}
}
}
return Images;
}
//constructor with the context being passed.
public ImageAdapter(Context m)
{
c = m;
}
public int getCount() {
return 6;
}
public Object getItem(int position) {
return Images[position];
}
public long getItemId(int position) {
return 0;
}
// The function View create a new ImageView for each item that is being referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
finalImages = fillImageArray();
ImageView imageView = new ImageView(c);
imageView.setImageResource(finalImages[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
return imageView;
}
}
finalImages in noi initialized there .........
it is finalImages in the getview which is not get called in 2nd activity (RewardsViewActivity )...........
if possible move this line to constructor finalImages = fillImageArray();

android: remove a view from a gallery

I'm using a Gallery view in my app. The app is designed so that I can drag and drop a view from that Gallery.
How can I remove the dragged view from the Gallery?
You remove it from the underlying adapter. If you do this correctly, the Gallery will refresh itself. Otherwise, call notifyDataSetChanged() on the adapter to trigger a Gallery update.
If you override ImageAdapter you can modify the contents at will by adding methods to delete or add items to your image list(s) or in the case of the example, completely swap lists on the fly. I am displaying a app banner at startup, and then changing the Gallery to display the mode the app is in as a slider. Whenever you call a method that modifies the dataset in the ImageAdapter, call imageAdapter.notifyDataSetChanged() as CommonsWare says above :
// in onCreate
_gallery = (Gallery) this.findViewById(R.id.gallery_header);
_imageAdapter = new ImageAdapter(getApplicationContext(),screen_width,screen_height);
_imageAdapter.setBannerMode(true);
_gallery.setAdapter(_imageAdapter);
// the main activity, in my case in a message handler.
_imageAdapter.setBannerMode(false);
_imageAdapter.notifyDataSetChanged();
_gallery.setSelection(0,true);
// this is my extended image adapter class
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class ImageAdapter extends BaseAdapter
{
private Context _context = null;
private int[] imageIds = { R.drawable.add_banner,R.drawable.subtract_banner,R.drawable.multiply_banner,R.drawable.divide_banner };
private int[] bannerIds = { R.drawable.mathpiggie_banner };
private static boolean bannerEnabled = true;
int _screen_width;
int _screen_height;
public ImageAdapter(Context context, int screen_width, int screen_height) {
this._context = context;
_screen_width = screen_width;
_screen_height = screen_height;
}
public void setBannerMode(boolean val)
{
bannerEnabled = val;
}
#Override
public int getCount()
{
if (bannerEnabled)
return bannerIds.length;
else
return imageIds.length;
}
#Override
public Object getItem(int index)
{
if (bannerEnabled)
return bannerIds[index];
else
return imageIds[index];
}
#Override
public long getItemId(int index)
{
return index;
}
#Override
public View getView(int postion, View view, ViewGroup group)
{
ImageView imageView = new ImageView(_context);
if (bannerEnabled)
imageView.setImageResource(bannerIds[postion]);
else
imageView.setImageResource(imageIds[postion]);
return imageView;
}
}

Categories

Resources