Android: Using two different Adapters for a Gridview grid - android

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.

Related

How can I click a picture and make it expand to full screen like in WhatsApp messages?

If you click a picture message in WhatsApp it will expand and take up the full screen. I haven't been able to figure out how to do this and have so far been relying on opening an Dialog that takes up the full screen to show the image. It's just not as slick though as the picture expanding to take up the full screen as in WhatsApp.
Here's the code I have so far for the AlertDialog.
MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Item item1 = new Item("Message 1");
Item item2 = new Item("Message 2");
Item item3 = new Item("Message 3");
Item item4 = new Item("Message 4");
ArrayList<Item> itemsArrayList = new ArrayList<>(); // calls function to get items list
itemsArrayList.add(item1);
itemsArrayList.add(item2);
itemsArrayList.add(item3);
itemsArrayList.add(item4);
CustomAdapter adapter = new CustomAdapter(this, itemsArrayList);
final ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
}
CustomAdapter.java
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
class CustomAdapter extends BaseAdapter {
private Context context;
private ArrayList<Item> items;
public CustomAdapter(Context context, ArrayList<Item> items) {
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).
inflate(R.layout.list_view_row, parent, false);
}
Item currentItem = (Item) getItem(position);
TextView textViewItemName = (TextView)
convertView.findViewById(R.id.textView1);
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
textViewItemName.setText(currentItem.getText());
if (position == 0) {
imageView.setImageResource(R.drawable.a);
} else{
imageView.setImageResource(R.drawable.anime);
}
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Dialog mSplashDialog = new Dialog(CustomAdapter.this.context);
mSplashDialog.setContentView(R.layout.zoom_layout);
ImageView imageView = (ImageView) mSplashDialog.findViewById(R.id.imageViewZoom);
imageView.setImageDrawable(imageView.getDrawable());
mSplashDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mSplashDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
mSplashDialog.show();
}
});
return convertView;
}
}
Could anyone point me to resources on how to do this or provide some sample code to get me started?
I've tried implementing the solution here How to Zoom in/Out an ImageView(not using Canvas) in android but the ImageView is already zoomed and doesn't respond to clicks. Also, my default image size is larger than the screen so it's being cut off.
I think you could have an activity for the full screen image, and code a transition animation so you get the desired effect.
Check out this link as it might be of help.
Android - How to create a transition from an item in listview to a whole activity?
You can use PhotoView library for zooming in/out:
1) Add below to your module gradle:
compile 'com.commit451:PhotoView:1.2.4'
2)
PhotoViewAttacher photoAttacher;
photoAttacher = new PhotoViewAttacher(My_Image);
photoAttacher.update();
If you need to zoom full screen then you can use:
explanation: setScaleType attribute can be used to resize the image to match the size of the ImageView.
myImageView = (ImageView) findViewById(R.id.my_img);
myImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isImageFullScreen) {
isImageFullScreen=false;
myImageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
myImageView.setAdjustViewBounds(true);
}else{
isImageFullScreen=true;
myImageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
myImageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
});

ArrayIndexoutofboundException

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);

Android Grid View Images : Show Text

I have prepared a grid view in a fragment which shows images and when you click on them it shows the image in another Activity. I want the name of the image to be shown below it for that i took a textview but it shows position. How to show the name of the photo choosen from grid view on that textview. Below is the code fragments.
full_image_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView android:id="#+id/full_image_view"
android:layout_width="500dp"
android:layout_height="500dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/image_title"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
FullImageFragment
package com.androidbelieve.HIT_APP;
import android.app.Activity;
/**
* Created by Akash on 2/13/2016.
*/
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class FullImageFragment extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image_layout);
// 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_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(String.valueOf(position));
}
}
GalleryFragment
package com.androidbelieve.HIT_APP;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by Ratan on 7/29/2015.
*/
public class GalleryFragment extends Fragment {
private GridView gridView;
private ImageView imageView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_gallery_grid, container,
false);
GridView gridView = (GridView) view.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(view.getContext()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(),FullImageFragment.class);
i.putExtra("id",position);
startActivity(i);
}
});
return view;
}
}
Please Help Me Out!!!
ImageAdapter
package com.androidbelieve.HIT_APP;
/**
* Created by Akash on 2/13/2016.
*/
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.akash, R.drawable.akash,
R.drawable.akash , R.drawable.akash,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#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(200, 200));
return imageView;
}
}
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(),FullImageFragment.class);
i.putExtra("id",position);
i.putExtra("image_name",arrayImageName[position]); // get your selected image name and pass it
startActivity(i);
}
});
In your image display class,
String name = i.getExtras().getString("image_name");
TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(name);
You can do this way:
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.akash, R.drawable.akash,
R.drawable.akash , R.drawable.akash,
};
public String[] mThumbNames = {
"akash 0", "akash 1","akash 2", "akash 3",
};
In Next Activity:
TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(imageAdapter.mThumbNames[position])
Edit 1:
private class CustomClass{
public int drawable;
public String title;
public String information;
}
ArrayList<CustomClass> listCustom = new ArrayList<>();
Integer[] mThumbIds = {
R.drawable.akash, R.drawable.akash,
R.drawable.akash , R.drawable.akash,
};
String[] mThumbNames = {
"akash 0", "akash 1","akash 2", "akash 3",
};
String[] mThumbInfos = {
"akash 0 Info", "akash 1 Info","akash 2 Info", "akash 3 Info",
};
for (int i = 0; i < mThumbIds.length; i++) {
CustomClass customClass = new CustomClass();
customClass.drawable = mThumbIds[i];
customClass.title = mThumbNames[i];
customClass.information = mThumbInfos[i];
listCustom.add(customClass);
}
Hope this will help you.
I think you want something like this?
Hiren Patel has already answered that. I am just putting it in steps.
1) create 'CustomClass' which will act as POJO class to structure your image name, resource reference.
2) Create an adapter with extends ArrayAdapter and take 'CustomClass' as argument
3) In your activity, create a list of that class and load the data (Image name, resource id etc)
4)Initialize your adapter with above list (can be array as well)
5) access that values on getView method of adapter

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