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));
Related
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);
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.
I have been going over the logic all night, and just cannot get an answer. I need for my drawables placeholder images, to not be clickable, and for my real images (photos from an SD card) to all be clickable for both regular clicks and long clicks. I have logged a million versions of how to write an if condition that would say something like: if the image Bitmap is the drawable and equal to the Bitmap in the position clicked on, then process the click listeners. But this can never happen! They are not ever equal. The Bitmap numbers always come up as different (which makes no sense to me), when I compare the drawable bitmap with the bitmap at the position clicked.
So I have tried another approach. I want to try what I found here to directly disable the drawable at its position. These methods, I put in my adapter class.
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
But the methods make no sense to me. Where/how do I call them in my situation? I don't understand how these are supposed to disable select items in my custom GridView. If you have any ideas, thanks.
Here is my log statement values from logcat. I got these by clicking on the drawable image in my GridView, but as you can see, the image Bitmap and the adapter position item are not the same. So how can I identify my drawable so I know to disable the click on there?
Value of adapter position item: android.graphics.Bitmap#430706e0
Value of drawableObject: android.graphics.Bitmap#4301fff0
Value of photoGridItem: android.graphics.Bitmap#4301a768
PhotoTab.java
package org.azurespot.cutecollection.phototab;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.azurespot.R;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment {
private GridView gridView;
File[] files;
ArrayList<PhotoGridItem> photoList = new ArrayList<>();
ArrayAdapter<PhotoGridItem> adapter;
Bitmap bitmap;
private String[] numberSDCardFiles = null;
PhotoGridItem drawableObject;
PhotoGridItem photoGridItem;
public PhotoTab() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
adapter = new GridViewPhotoAdapter(getActivity(), photoList);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
gridView.setAdapter(adapter);
if(adapter.getCount() == 0) {
// load contents of SD card
loadSDCard();
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - numberSDCardFiles.length); i++) {
drawableObject = new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder));
adapter.add(drawableObject);
adapter.notifyDataSetChanged();
}
}
setupGridViewListener();
return v;
}
public void loadSDCard() {
try {
// gets directory CutePhotos from sd card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "Cute Photos");
// lists all files in CutePhotos, loads in Files[] array
files = cutePhotosDir.listFiles();
for (File singleFile : files) {
String filePath = singleFile.getAbsolutePath();
// this method makes size small for the view (to save memory)
bitmap = decodeImageBitmap(filePath, 270, 270);
photoGridItem = new PhotoGridItem(bitmap);
// Check if this is a new bitmap file
adapter.add(photoGridItem);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
// get number of files in Cute Photos directory
numberSDCardFiles = new String[files.length];
}
private void setupGridViewListener(){
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View item, int pos, long id) {
//Convert the bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bm = adapter.getItem(pos).getImage();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Intent i = new Intent(getActivity(), PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
startActivity(i);
Log.d("TAG", "Value of bm: " + bm);
Log.d("TAG", "Value of adapter position item: " + adapter.getItem(pos));
Log.d("TAG", "Value of drawableObject: " + drawableObject);
}
});
// to delete a photo item
gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> aView, View item,
final int pos, long id) {
new AlertDialog.Builder(getActivity())
.setTitle("Delete")
.setMessage("Delete this cute photo?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// delete from ArrayList first
photoList.remove(pos);
// get file name then delete it from SD card
String name = files[pos].getName();
File file = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "Cute Photos" + "/" + name);
file.delete();
// after each item delete, replace with default icon
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
adapter.notifyDataSetChanged();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
});
}
// next 2 methods scale the bitmap image to a better size (so not huge)
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeImageBitmap(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(path, options);
return bm;
}
}
GridViewPhotoAdapter.java
package org.azurespot.cutecollection.phototab;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
import java.util.ArrayList;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
ViewHolder holder = null;
int position;
public GridViewPhotoAdapter(Context context, ArrayList<PhotoGridItem> photos) {
super(context, 0, photos);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
this.position = position;
if (v == null) {
v = LayoutInflater.from(getContext())
.inflate(R.layout.photo_grid_item, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) v.findViewById(R.id.photo_grid_view);
// stores holder with view
v.setTag(holder);
} else {
holder = (ViewHolder)v.getTag();
}
// gets position of whichever photo you click on in the GridView
final PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
Bitmap bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(270, 270));
}
return v;
}
public class ViewHolder{
ImageView imageView;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return true;
}
}
Add your logic inside IsEnabled method like below
#Override
public boolean isEnabled(int position) {
if(position == POSITION_U_WANT_TO_DISABLE) {
return false;
}
return true;
}
According to your code you always returning true (Enabled) inside that method.
Unbelievable, but I found the right logic after all. I did not need the 2 List methods mentioned above, since those relied upon me identifying a drawable versus a photo in my GridView positioning. So at the time of adding my placeholder drawable to my grid, I also put them all into an ArrayList<Bitmap>, so that I could search the list and pull out drawable values when needed.
So then I added a conditional to the click listener's code, that said if my ArrayList of Bitmaps contains the bitmap found at the position of the view that was clicked, then do not proceed with executing the click listener's code. If it does not contain a Bitmap from the ArrayList, then go ahead and process it.
I checked if I could still add and delete items from the GridView and still have that conditional work, and I sure could. Below are the new additions in context.
ArrayList<Bitmap> bmList = new ArrayList<>();
...
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - numberSDCardFiles.length); i++) {
drawableObject = new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder));
// adds each drawable Bitmap to an ArrayList
bmList.add(drawableObject.getImage());
// adds to adapter
adapter.add(drawableObject);
adapter.notifyDataSetChanged();
}
...
private void setupGridViewListener(){
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView,
View item, int pos, long id) {
if(!bmList.contains(adapter.getItem(pos).getImage())) {
//Convert the bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bm = adapter.getItem(pos).getImage();
bm.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
Intent i = new Intent(getActivity(), PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
startActivity(i);
}
}
});
I have tried all the suggestions I could find on S.O., and I was able to use this exact code successfully on a ListView of mine, but for some reason I can't set a long click listener on my GridView. I log inside the method setupGridViewListener(); and it does not even run, yet I have my method inside onCreateView(). Don't know why it won't work. The alert dialog should pop up once long-clicked, but it doesn't. Method is at bottom of my class.
UPDATE:
I did not mention that my onClickListener does work fine. So I can at least do a single click, and this displays my activity. But, that code is in my adapter, so maybe they should be together in the same class? I am including my adapter and my 2 xmls below.
UPDATE 2
I discovered several things that were worth mentioning, even after I got my code working again. First of all, putting my click listeners into my adapter was a mistake. It slowed things down HUGELY... so much so, I was frantic about finding a way to get things on another thread. But in the end, I did not need to. I realized that I could actually get both of my click listeners into my PhotoTab.java class. This immediately made it faster (back to normal). But another curious thing, the IDE would not let me use OnClickListener, but insisted I go back to OnItemClickListener. So once I changed those back, everything worked again. So the listeners ended up being on my GridView variables, e.g. gridView.setOnItemClickListener(new OnItemClickListener() {} not my ImageView item variables. Perhaps this is what made things faster? There is one GridView, but 24 ImageViews (GridView cells). But that is just a guess. I have updated my code to the current, working code. Except I'm still working on the long click code, so that's not right yet. The dialog alert works though, and each item in the GridView is clickable.
PhotoTab.java
package org.azurespot.cutecollection.phototab;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
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.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.azurespot.R;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment {
private GridView gridView;
File[] files;
ArrayList<PhotoGridItem> photoList = new ArrayList<>();
ArrayAdapter<PhotoGridItem> adapter;
Bitmap bitmap;
byte[] byteArray = null;
private String[] allSDCardFiles = null;
PhotoGridItem photoGridItem;
public PhotoTab() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
// this null check won't instantiate again if it was already
// if(adapter == null)
adapter = new GridViewPhotoAdapter(getActivity(), R.layout.photo_grid_item);
// Set the Adapter to GridView
gridView.setAdapter(adapter);
// load contents of SD card
loadSDCard();
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - allSDCardFiles.length); i++) {
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
adapter.notifyDataSetChanged();
i++;
}
//Convert the bitmap to byte array, so can pass through intent
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
this.byteArray = byteArray;
setupGridViewListener();
return v;
}
public void loadSDCard() {
try {
// gets directory CutePhotos from sd card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "Cute Photos");
// lists all files in CutePhotos, loads in Files[] array
files = cutePhotosDir.listFiles();
for (File singleFile : files) {
String filePath = singleFile.getAbsolutePath();
// this method makes size small for the view (to save memory)
bitmap = decodeImageBitmap(filePath, 270, 270);
photoGridItem = new PhotoGridItem(bitmap);
// Check if this is a new bitmap file
adapter.add(photoGridItem);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
// get number of files in Cute Photos directory
allSDCardFiles = new String[files.length];
}
private void setupGridViewListener(){
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter,
View item, int pos, long id) {
Intent i = new Intent(getActivity(), PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
startActivity(i);
}
});
// to delete a photo item
gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> aView, View item,
final int pos, long id) {
new AlertDialog.Builder(getActivity())
.setTitle("Delete")
.setMessage("Delete this cute photo?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// delete from ArrayList first
// PhotoTab.photoList.remove(position);
// get file name then delete it from SD card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "CutePhotos/" + photoGridItem);
cutePhotosDir.delete();
// after each item delete, must refresh load so can delete again
// photoTab.loadSDCard();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
dialog.cancel();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
});
}
// next 2 methods scale the bitmap image to a better size (so not huge)
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
public static Bitmap decodeImageBitmap(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bm = BitmapFactory.decodeFile(path, options);
return bm;
}
}
GridViewPhotoAdapter
package org.azurespot.cutecollection.phototab;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
public Context context;
private int resourceId;
ViewHolder holder = null;
int position;
public GridViewPhotoAdapter(Context context, int layoutResourceId) {
super(context, layoutResourceId);
this.context = context;
this.resourceId = layoutResourceId;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
this.position = position;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.photo_grid_view);
// stores holder with view
row.setTag(holder);
} else {
holder = (ViewHolder)row.getTag();
}
// gets position of whichever photo you click on in the GridView
final PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
Bitmap bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(270, 270));
}
return row;
}
public class ViewHolder{
ImageView imageView;
}
}
photo_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_view"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2198bb">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/photo_grid"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:verticalSpacing="5dp"
android:horizontalSpacing="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="25dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:scrollbarStyle="outsideOverlay"
android:verticalScrollbarPosition="right" />
</RelativeLayout>
photo_grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/photo_grid_view"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
package com.example.slideanim;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;
public class MyGridAdapter extends BaseAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Activity activity;
private LayoutInflater layoutInflater;
public MyGridAdapter(Activity activity, ArrayList<String> list) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.list = list;
layoutInflater = activity.getLayoutInflater();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int pos, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.row, null);
holder.imageView = (ImageView) convertView
.findViewById(R.id.photo_grid_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
holder.imageView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(activity, "got", Toast.LENGTH_SHORT).show();
return true;
}
});
return convertView;
}
private static class ViewHolder {
public ImageView imageView;
}
}
It would be better to either use click and long click for the imageview, or both on the gridview item.
If you want on imageview, do not setup the GridViewListener and try this in your adapter:
holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//your code
return true;
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(context, PhotoViewerActivity.class);
i.putExtra("photo", byteArray);
context.startActivity(i);
}
});
-First of all check if any Views In your gridview are Focusable or Clickable or not make them False and then check your log again
-Let me inform if further issue exist
-THanks
I have a ListView that has one image and two lines of texts for each element (organized by a RelativeLayout). It works ok, but it's too slow and I know where the problem comes from!
This is the getView() method for the custom adapter that I'm using:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false);
mViewHolder = new ViewHolder();
mViewHolder.cover = (ImageView) convertView.findViewById(R.id.app_icon);
mViewHolder.title = (TextView) convertView.findViewById(R.id.selection);
mViewHolder.description = (TextView) convertView.findViewById(R.id.app_short_description);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
// Here is the origin of the issue !
final Feed currentFeed = getItem(position);
mViewHolder.title.setText(currentFeed.getTitle());
mViewHolder.description.setText(currentFeed.getDescription());
try {
if(currentFeed.getThumbnailUrl() != null) {
downloadThumbnail(mViewHolder.cover, currentFeed.getThumbnailUrl());
}
} catch(Exception e) {
e.printStackTrace();
}
return convertView;
}
private static class ViewHolder {
TextView title;
TextView description;
ImageView cover;
}
So I have done some manual benchmarking and it appears that allocating an instance of Feed is the source of this slowness:
final Feed currentFeed = getItem(position);
I know this because I have written another version of this to compare the two:
// Here is the origin of the issue !
//final Feed currentFeed = getItem(position);
mViewHolder.title.setText("Title");
mViewHolder.description.setText("Description");
try {
if(currentFeed.getThumbnailUrl() != null) {
downloadThumbnail(mViewHolder.cover, "some url");
}
} catch(Exception e) {
e.printStackTrace();
}
This one was way smoother (even with the downloadThumbnail() method working).
I also precise that there are only 15 items on my ListView.
I know that allocating objects is very expensive because of garbage collection but I can't any other way to do it!
Any idea?
Thanks!
EDIT
Don't mind too much about the downloadThumbnail() method, it already does some caching. And actually even without any picture, it's still slow.
When user scrolls the list, getView gets called on the adapter. Make sure that you dont do same things repeatedly, for example generating thumbnail. If number of items is limited (for example video content), then you can create all views and keep it ready for get view. Otherwise you may have to implement cacheing.
Below code shows an adapter and listView implementation, where in all listviews are created and stored in memory. Since this is meant for video browsing, memory does not pose any issue. (limited number of content, max 100)
Video List Adapter
import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class VideoListAdapter extends BaseAdapter {
private Context mContext = null;
private HashMap<String, VideoListItem> mHashedItems = new HashMap<String, VideoListItem>();
private static final String TAG = "VideoListAdapter";
public static final int VIDEO_CONTENT_ID = 0;
public static final int VIDEO_CONTENT_TITLE = 1;
public static final int VIDEO_CONTENT_DURATION = 2;
public static final int VIDEO_CONTENT_RESOLUTION = 3;
public static final int VIDEO_CONTENT_MIME = 4;
private Cursor mCursorForVideoList = null;
private ContentResolver mContentResolver = null;
private int mListCount = 0;
VideoListAdapter(Context context, ContentResolver cr) {
mContext = context;
mContentResolver = cr;
Log.i(TAG, "In the Constructor");
mCursorForVideoList =
mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.TITLE,
MediaStore.Video.VideoColumns.DURATION,
MediaStore.Video.VideoColumns.RESOLUTION
},
null,
null,
null);
mListCount = mCursorForVideoList.getCount();
}
#Override
public int getCount() {
return mListCount;
}
#Override
public Object getItem(int arg0) {
return getVideoListItem(arg0);
}
#Override
public long getItemId(int position) {
//Log.i(TAG, "position : " + position);
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Log.i(TAG, "GetView :: Position : " + position);
return getVideoListItem(position);
}
private VideoListItem getVideoListItem(int position)
{
//Log.i(TAG, "getVideoListItem :: Position : " + position);
String key = Integer.toString(position);
VideoListItem item = mHashedItems.get(key);
if(item == null)
{
//Log.i(TAG, "New getVideoListItem :: Position : " + position);
mCursorForVideoList.moveToPosition(position);
mHashedItems.put(key, new VideoListItem(mContext, mContentResolver, mCursorForVideoList));
}
return mHashedItems.get(key);
}
};
Video List View
import java.util.Formatter;
import java.util.Locale;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
class VideoListItem extends LinearLayout
{
private static final String TAG = "VideoListAdapter";
private ImageView mThumbnail = null;
private TextView mDuration = null;
private TextView mTitle = null;
private TextView mResolution = null;
private LayoutInflater mLayoutFactory = null;
private long mContentId = 0;
public VideoListItem(Context context, ContentResolver cr, Cursor cursor) {
super(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
params.setMargins(10, 10, 10, 10);
mLayoutFactory = LayoutInflater.from(context);
View thisView = mLayoutFactory.inflate(R.layout.videolistitem, null);
addView(thisView);
mThumbnail = (ImageView) findViewById(R.id.thumbnail);
mDuration = (TextView) findViewById(R.id.DDuration);
mTitle = (TextView) findViewById(R.id.DTitle);
mResolution = (TextView) findViewById(R.id.DResolution);
mThumbnail.setLayoutParams(new LinearLayout.LayoutParams(144, 144));
Resources r = this.getResources();
Bitmap bMap = MediaStore.Video.Thumbnails.getThumbnail(cr, cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID), MediaStore.Video.Thumbnails.MINI_KIND, null);
if(bMap != null)
{
mThumbnail.setImageBitmap(Bitmap.createScaledBitmap(bMap, 128, 128, true));
}
else
{
mThumbnail.setImageDrawable(r.getDrawable(R.drawable.error));
}
mThumbnail.setPadding(16, 16, 16, 16);
mTitle.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_TITLE));
mTitle.setSingleLine();
mTitle.setTextColor(Color.GREEN);
mResolution.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_RESOLUTION));
mResolution.setSingleLine();
mResolution.setTextColor(Color.RED);
mDuration.setText(stringForTime(cursor.getInt(VideoListAdapter.VIDEO_CONTENT_DURATION)));
mDuration.setSingleLine();
mDuration.setTextColor(Color.CYAN);
mContentId = cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID);
}
public long getContentId()
{
return mContentId;
}
private StringBuilder mFormatBuilder = null;
private Formatter mFormatter = null;
private String stringForTime(int timeMs) {
int totalSeconds = timeMs / 1000;
mFormatBuilder = new StringBuilder();
mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int hours = totalSeconds / 3600;
mFormatBuilder.setLength(0);
if (hours > 0) {
return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
} else {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}
};
Shash
Don't allocate or store the Feed object in your View holder and instead only store the position (position). When you need to reference the object then grab the reference index from the ViewHolder and act accordingly.
Edit
Of course I missed that you're using the object later on... You might also create a number of minimal, static methods for your Feed object that only return specific things, such as the title, etc. Then call these methods in your getView method to set the UI elements without full creation of the Feed itself.