Here is the code for the gridview, it shows the same images in a repetition, the sample images 2 and 3 are shown over and over again, what should i do?
public class GridViewImageAdapter extends BaseAdapter {
private Context mContext;
public GridViewImageAdapter(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;
ImageView icon;
if (convertView == null) { // if it's not recycled, initialize some attributes
icon = new ImageView(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.grid_icon, parent, false);
icon=(ImageView)row.findViewById(R.id.album_image);
icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
icon.setImageResource(mThumbIds[position]);
//overlay
View overlay = (View) row.findViewById(R.id.overlay);
int opacity = 100; // from 0 to 255
overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha
FrameLayout.LayoutParams params =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 600);
params.gravity = Gravity.CENTER;
overlay.setLayoutParams(params);
overlay.invalidate();
return row;
}
return convertView;
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1
};
}
It's becuase you are returning the same view repeatedly.
convertView is a reused view, so once it's been used and is no longer null, you are returning it without changing it.
Change to this
if (convertView == null) { // if it's not recycled, initialize some attributes
icon = new ImageView(mContext);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.grid_icon, parent, false);
}
icon=(ImageView)row.findViewById(R.id.album_image);
icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
icon.setImageResource(mThumbIds[position]);
//overlay
View overlay = (View) convertView.findViewById(R.id.overlay);
int opacity = 100; // from 0 to 255
overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha
FrameLayout.LayoutParams params =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 600);
params.gravity = Gravity.CENTER;
overlay.setLayoutParams(params);
overlay.invalidate();
return convertView;
The above should work, it's inefficient though, you should look up the ViewHolder pattern
Related
I have arraylist.Size of arraylist is 156.when I am running for loop it's running 156 times and it's adding all values into another arraylist.But when I am setting this arraylist into adapter adapter taking 156 items but when I am setting adapter into listview that time Listview taking only 98 items only.
When I am debugging I seen arralist size 156.and arraylist deviding into sets like
0-99
100-199
200-210
see below image :
Here my adapter taking only 0-99 items.How I can add 100-199 set Items into Listview.
Edit #1:
Adapter class :
public class AllserversListAdapter extends BaseAdapter implements OnClickListener {
Activity mActivity;
ArrayList<AllServers> mAllServersArray;
private static LayoutInflater mInflater = null;
ArrayList<MyFavorites> mFavoriteServers;
public AllserversListAdapter(Activity activity,
ArrayList<AllServers> allServers,
ArrayList<MyFavorites> favoriteServers) {
// TODO Auto-generated constructor stub
mActivity = activity;
mAllServersArray = allServers;
mFavoriteServers = favoriteServers;
mInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mAllServersArray.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (vi == null) {
/****** Inflate list items layout file for each row ( Defined below ) *******/
vi = mInflater.inflate(R.layout.servers_list_items, null);
/****** View Holder Object to contain list items file elements ******/
holder = new ViewHolder();
holder.serverName = (TextView) vi
.findViewById(R.id.server_items_country_name_textview);
holder.speed = (TextView) vi
.findViewById(R.id.server_items_speed_textview);
holder.speedTitleTextView = (TextView) vi
.findViewById(R.id.speed_title_textview);
holder.flag = (ImageView) vi.findViewById(R.id.server_flag_image);
holder.star = (ImageView) vi.findViewById(R.id.server_items_star);
holder.bodyLayout = (LinearLayout) vi
.findViewById(R.id.servere_items_boady_layout);
holder.speedBackgroundLayout = (LinearLayout) vi
.findViewById(R.id.speed_backround_layout);
holder.itemsParentLayout = (LinearLayout) vi
.findViewById(R.id.server_list_items_parentlayout);
holder.itemsParentLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.light_gray));
holder.bodyLayout.setBackgroundColor(mActivity.getResources()
.getColor(R.color.dark_gray_black));
holder.star.setOnClickListener(this);
/************ Set holder with LayoutInflater ************/
vi.setTag(holder);
holder.star.setTag(position);
holder.flag.setTag(position);
} else {
holder = (ViewHolder) vi.getTag();
}
String ip = mAllServersArray.get(position).getIp();
String country = mAllServersArray.get(position).getCountry();
String region = mAllServersArray.get(position).getRegion();
String speed = mAllServersArray.get(position).getSpeed();
String flagCountryCode = mAllServersArray.get(position)
.getCountrycode();
int ipSpeed = Integer.valueOf(speed);
holder.speedTitleTextView.setVisibility(View.GONE);
holder.speed.setText("TimeOut");
holder.speed.setTypeface(Typeface.DEFAULT);
holder.speed.setPadding(26, 0, 26, 0);
holder.speedBackgroundLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.time_out_color));
holder.serverName.setText(country + "\n" + region + "\n" + ip);
return vi;
}
}
Adding items into arraylist :
Any one give me suggest.
Try this way,hope this will help you to solve your problem.
public class AllserversListAdapter extends BaseAdapter{
Context context;
ArrayList<AllServers> allServers;
ArrayList<MyFavorites> favoriteServers;
public AllserversListAdapter(Context context,ArrayList<AllServers> allServers,ArrayList<MyFavorites> favoriteServers) {
this.context = context;
this.allServers = allServers;
this.favoriteServers = favoriteServers;
}
#Override
public int getCount() {
return mAllServersArray.size();
}
#Override
public Object getItem(int position) {
return mAllServersArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.servers_list_items, null);
holder.serverName = (TextView) convertView.findViewById(R.id.server_items_country_name_textview);
holder.speed = (TextView) convertView.findViewById(R.id.server_items_speed_textview);
holder.speedTitleTextView = (TextView) convertView.findViewById(R.id.speed_title_textview);
holder.flag = (ImageView) convertView.findViewById(R.id.server_flag_image);
holder.star = (ImageView) convertView.findViewById(R.id.server_items_star);
holder.bodyLayout = (LinearLayout) convertView.findViewById(R.id.servere_items_boady_layout);
holder.speedBackgroundLayout = (LinearLayout) convertView.findViewById(R.id.speed_backround_layout);
holder.itemsParentLayout = (LinearLayout) convertView.findViewById(R.id.server_list_items_parentlayout);
holder.itemsParentLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.light_gray));
holder.bodyLayout.setBackgroundColor(mActivity.getResources().getColor(R.color.dark_gray_black));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String ip = mAllServersArray.get(position).getIp();
holder.speedTitleTextView.setVisibility(View.GONE);
holder.speed.setText("TimeOut");
holder.speed.setTypeface(Typeface.DEFAULT);
holder.speed.setPadding(26, 0, 26, 0);
holder.speedBackgroundLayout.setBackgroundColor(mActivity
.getResources().getColor(R.color.time_out_color));
holder.serverName.setText(mAllServersArray.get(position).getCountry() + "\n" + mAllServersArray.get(position).getRegion() + "\n" + ip);
holder.flag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// try use (position) value to check which list item click.
}
});
holder.star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// try use (position) value to check which list item click.
}
});
return convertView;
}
class ViewHolder {
TextView serverName;
TextView speed;
TextView speedTitleTextView;
ImageView flag;
ImageView star;
LinearLayout bodyLayout;
LinearLayout speedBackgroundLayout;
LinearLayout itemsParentLayout;
}
}
I am trying to implement a listview that displays a list of directories. and under each directory is a gridview with associated adapter (shown below) showing a list of image thumbnails (see below image). I have it working great except whenever the list item is off the screen then brought back on screen, the images are reloaded. I am using an asynctask to download the thumbnails and replace the placeholder image for each imageview so it is not acceptable that everytime an item is offscreen, all of its thumbnails are downloaded again. Does anyone have an example of this type of implementation (gridview adapter within a listview adapter) where the imageview (or images) are stored? What is the proper way to do this?
Thanks in advance for your help.
Gallery Adapter
public class GalleryAdapter extends BaseAdapter {
private Context mContext;
ArrayList<GalleryItem> GalleryList;
//MediaAdapter adapter;
public GalleryAdapter(Context c,ArrayList<GalleryItem> l) {
mContext = c;
GalleryList = l;
}
public int getCount() {
return GalleryList.size();
}
public Object getItem(int position) {
return GalleryList.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
viewHolder = new GalleryViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
convertView.setTag(viewHolder);
}
else{
viewHolder = (GalleryViewHolder) convertView.getTag();
}
viewHolder.title.setText(GalleryList.get(position).getTitle());
//Formatting the gridView to fit the screen dim.
ImageTools mWidth = new ImageTools(mContext);
viewHolder.mediaGrid.setColumnWidth(mWidth.imageSize());
int rows = (int) Math.ceil((GalleryList.get(position).getMedia().size() / mWidth.columnNumber)+1);
LinearLayout.LayoutParams labelLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, mWidth.imageSize()*rows);
viewHolder.gridHolder.setLayoutParams(labelLayoutParams);
viewHolder.mediaGrid.setLayoutParams(labelLayoutParams);
viewHolder.mediaGrid.setMinimumHeight(mWidth.imageSize()*rows);
//Set Adapter for image views
viewHolder.mediaGrid.setAdapter(new MediaAdapter(convertView.getContext(),GalleryList.get(position).getMedia()));
viewHolder.folder_settings.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Dialogs.createListDialog(mContext,"Folder Actions", R.array.gallery_action_array).show();
}
});
viewHolder.mediaGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
}
});
return convertView;
}
private class GalleryViewHolder {
private TextView title;
private ArrayList<ImageView> imageList;
private GridView mediaGrid;
private ImageView folder_settings;
private LinearLayout gridHolder;
private int position;
}
}
Media Adapter
public class MediaAdapter extends BaseAdapter {
private Context mContext;
ArrayList<MediaItem> mediaitems;
public MediaAdapter(Context c,ArrayList<MediaItem> l) {
mContext = c;
mediaitems = l;
}
public int getCount() {
return mediaitems.size();
}
public Object getItem(int position) {
return mediaitems.get(position);
}
public long getItemId(int position) {
return 0;
}
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.setImageResource(R.drawable.loading);
imageView.setTag(R.integer.path,mediaitems.get(position).getPath().toString());
imageView.setTag(R.integer.fullsize,"false");
imageView.setTag(R.integer.parentpath,mediaitems.get(position).getParentPath().toString());
imageView.setTag(R.integer.index , String.valueOf(position));
try {
new thumbDownload(mContext).execute(imageView);
} catch (DbxException e) {
e.printStackTrace();
}
ImageTools mWidth = new ImageTools(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(mWidth.imageSize(), mWidth.imageSize()));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
//imageView.setImageBitmap(mediaitems.get(position).getBitmap());
return imageView;
}
}
Try with this..may be its silly way, but its worked for me. Just add a line of code inside your method like this for gallery adapter:
public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;
// Add this line.
convertView = null;
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.gallery_item, parent, false);
viewHolder = new GalleryViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
convertView.setTag(viewHolder);
}
else{
viewHolder = (GalleryViewHolder) convertView.getTag();
}
// rest of your code
}
You can use StickyGridHeaders to implement your UI and Android-Universal-Image-Loader for flexible asynchronous image loading.
StickyGridHeaders is an Android library that provides a GridView that shows items in sections with headers. By default the section headers stick to the top like the People app in Android 4.x but this can be turned off.
Android-Universal-Image-Loader aims to provide a reusable instrument for asynchronous image loading, caching and displaying.
I ended up Using a HashMap<String,Bitmap> to store the images once they were downloaded. I made the hashMap static in my mediaAdapter so I could add the bitmap from my asynctask when it was downloaded. Then in my media Adapter getView(), I added a if statement to check if the image had already been downloaded. If it had, I used setImageBitmap(myHash.get(key)).
my images get shuffled or changed when i scroll in list view.......
Images get shuffled when i scroll down and keep on changing own its own...............
public class CustomListAdapter extends BaseAdapter {
private ArrayList<NewsItem> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<NewsItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.activity_list_search, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView.findViewById(R.id.title);
holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
NewsItem newsItem = (NewsItem) listData.get(position);
holder.headlineView.setText(newsItem.getHeadline());
holder.reporterNameView.setText("Model: " + newsItem.getReporterName());
holder.reportedDateView.setText(newsItem.getDate());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
}
return convertView;
}
static class ViewHolder {
TextView headlineView;
TextView reporterNameView;
TextView reportedDateView;
ImageView imageView;
}
}
The problem is very generic for new Android developers.
Your ImageDownloaderTask should take care of whether the downloaded image should still be posted on screen.
For example, after ImageDownloaderTask finishes one image downloading task (e.g. for index 15), it should make sure the 15th item is still displaying on screen. Since ListView is reusing all the item views, if you directly set the bit map to the original referenced view, your imageview will be shuffled.
I have setup a custom gridView adapter, which looks like this
public class ImageAdapter extends BaseAdapter {
String[] urls = new String[10];
//....some code
class MyTask extends AsyncTask<Void,Void,Void>{
#Override
protected Void doInBackground(Void... arg0) {
//perform a get request and fill up the "urls" array by 10 values..
return null;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
new MyTask().execute(); //execute AsyncTask
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView = null;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_layout.xml
gridView = inflater.inflate(R.layout.grid_layout, null);
final ImageButton grid_art = (ImageButton) gridView.findViewById(R.id.grid_art);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
imageloader = ImageLoader.getInstance();
imageloader.init(config);
imageloader.loadImage(blog_urls[position], new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
grid_art.setImageBitmap(loadedImage);
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return urls.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
the adapter loads 10 image url's in an asynctask and stores it into the urls array.
I am using universal image loader to download and load images found at the urls into an imagebutton as background,
however only one imagebutton gets the background and the rest are blank.
Change your getView method like this:
public View getView(int position, View convertView, ViewGroup parent) {
new MyTask().execute(); // execute AsyncTask
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View gridView = null;
MyView myView;
if (convertView == null) {
myView= new MyView();
//gridView = new View(context);
// get layout from grid_layout.xml
convertView = inflater.inflate(R.layout.grid_layout, null);
myView.grid_art = (ImageButton) convertView.findViewById(R.id.grid_art);
convertView.setTag(myView);
} else {
myView= (MyView) convertView.getTag();
//gridView = (View) convertView;
}
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context).build();
imageloader = ImageLoader.getInstance();
imageloader.init(config);
imageloader.loadImage(blog_urls[position],
new SimpleImageLoadingListener() {
#Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
myView.grid_art.setImageBitmap(loadedImage);
}
});
return convertView;
}
Where MyView is:
class MyView {
ImageButton grid_art;
}
Hope it helps...
this is the problematic code:
public class Level1 extends Activity {
int[] logos = {
R.drawable.arutz8,
R.drawable.channel1,
R.drawable.doctor_gav,
R.drawable.foxgroup3,
R.drawable.careline,
R.drawable.golfnew,
R.drawable.haaretz,
R.drawable.hafenix,
/*R.drawable.hando,
R.drawable.bankleumi,
R.drawable.jerusalempostred,
R.drawable.laisha,
R.drawable.logo,
R.drawable.logodelta,
R.drawable.maariv,
R.drawable.pelephone,
R.drawable.ravbariah,
R.drawable.renuar,
R.drawable.reshet_tv,
R.drawable.sano,
R.drawable.shilav,
R.drawable.sport5,
R.drawable.srigamish,
R.drawable.steimatzky,
R.drawable.superpharm,
R.drawable.supersal,
R.drawable.tambur,
R.drawable.tzometsfarim,
R.drawable.walla,
R.drawable.yediot,*/
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.level1);
ListAdapter adapter = (new ArrayAdapter<Integer>(this, R.layout.level1));
GridView grid = (GridView) findViewById(R.id.gridview1);
grid.setAdapter(new ImageAdapter(this));
}
private class ImageAdapter extends BaseAdapter
{
private Context context;
public ImageAdapter(Context c)
{
context = c;
}
//---returns the number of images---
public int getCount() {
return logos.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(90, 90));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(logos[position]);
return imageView;
}
}
}
This program works perfectly as long as the size of all images is about 60px per each image. The problem in that size of image is that every image got constricted and its ugli!.
I tried to use in bigger images and for some reason only the first five images appears on the screen and when i try to load the rest images, the app crashes.
I thought that happens because of the size of the images but than i realized that all the images are in the same big size and still 5 of them were drawn on the screen.
Any ideas?
Those are the logs from logCat:
logs
and this is the result when i press on the level 1 button:
result
There is a problem in your public View getView(...) method in your ImageAdapter.
When the convertView == null, you never link the convertView to your imageView. So there will be a problem in the else-statement.
You can do this in two ways:
By defining your ImageView in code like you did, in your case this will be the best choice:
ImageView imageView = (ImageView) convertView;
if (convertView == null) {
convertView = new ImageView(context);
imageView = (ImageView) convertView;
// Set other parameters
}
// Set resource
return convertView;
By defining your layout in a xml-layout file an using a layout inflater:
Handler handler;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(yourLayoutResourceId, parent, false);
handler = new Handler();
handler.itemIV = (ImageView) convertView.findViewById(imageViewLayoutId);
convertView.setTag(handler);
} else {
handler = (Handler) convertView.getTag();
}
handler.imageView.setImageResource(...);
return convertView;
Where yourLayoutResourceId is the id of the created xml-layout file (R.layout.exmaple), and imageViewLayoutId is the id of your imaeView in the layout (R.id.exmapleIV).
As last step define an inner class Handler in your ImageAdapter:
class Handler {
ImageView imageView;
}
Good luck!
Have nice development.
Kr