I am working on developing an app to play songs, I use Glide to get the image of the song and display it in the ListView,
the first problem
when I scrolling in my ListView the application becomes slow, and I have another problem which is when I scrolling Up the Glide load the images that have already been loaded Is there a way to fix these problems?
here is my code to get the image of songs
`
import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ListAdapter
extends BaseAdapter {
private Context context;
private static ArrayList<ListItemFromPhone> newList = new ArrayList<>();
ListAdapter(){}
ListAdapter(Context context, ArrayList<ListItemFromPhone> list) {
notifyDataSetChanged();
this.context = context;
newList = list;
}
#Override
public int getCount() {
return newList.size();
}
#Override
public Object getItem(int position) {
return newList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.view,null);
TextView textView = view.findViewById(R.id.textView);
final ImageView imageView = view.findViewById(R.id.imageView);
byte [] imgArtist = getImg(newList.get(position).data);
if (imgArtist != null){
Glide.with(context).asBitmap()
.load(imgArtist)
.placeholder(R.drawable.music_img)
.into(imageView);
}
else imageView.setImageResource(R.drawable.music_img);
textView.setText(newList.get(position).name);
return view;
}
private byte[] getImg(String path){
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(path);
byte[] img = retriever.getEmbeddedPicture();
retriever.release();
return img;
}
}
To avoid this problem you must use RecyclerView. There are a lot of tutorials on youtube on how to implement it.
Related
I have been messing with tmdb and movies and I have run into an issue where when my fragment loads and populates my gridview adapter the loading of images takes place without any error, but physically I can't see it until I scroll the GridView down a few lines and the back-up again.
The image shows what it's like before scrolling. Then after scrolling all the other images appear.???
Here is my adapter code for my GridView.
package com.rwsw.livesofa.tv.gridviews;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.rwsw.livesofa.R;
import com.rwsw.livesofa.models.MovieModel;
import com.rwsw.livesofa.tv.fragments.FragmentRecommended;
import java.util.ArrayList;
import java.util.HashMap;
public class Grid_View_Recommended extends BaseAdapter {
Context context;
LayoutInflater inflater;
private static ArrayList<MovieModel> movieModelList;
MovieModel resultp;
public Grid_View_Recommended(Context context, ArrayList<MovieModel> arraylist) {
this.context = context;
movieModelList = arraylist;
}
public class ViewHolder {
public ImageView poster;
}
#Override
public int getCount() {
return movieModelList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = convertView;
Grid_View_Recommended.ViewHolder holder;
if (convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = inflater.inflate(R.layout.item_movie, parent, false);
holder = new ViewHolder();
holder.poster = (ImageView) itemView.findViewById(R.id.poster);
itemView.setTag(holder);
} else {
holder = (Grid_View_Recommended.ViewHolder) itemView.getTag();
resultp = movieModelList.get(position);
Glide.with(context)
.asBitmap()
.load("https://image.tmdb.org/t/p/w342" + resultp.getPosterPath())
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.into(holder.poster);
}
return itemView;
}
}
I have done many searches and tried loading as bitmap and using different cache strategy's, but cant get it to work.
The first time the listview loads the null condition is true.
If you see the null condition does not have the Glide code in it. Hence the picture loads only when the convertView is not null.
So you can keep the Glide code in both sections.
If that does not work.
Here are a few things you can try
Load it directly with
Glide.with(this)
.load("https://image.tmdb.org/t/p/w342" + resultp.getPosterPath())
..into(holder.poster);
```
If that does not solve it. Change the DiskCacheStrategy to NONE
We have been working on implementing a RecyclerView in our project for a while now, and although the backend seems to work just fine, as soon as I call the recyclerView.setAdapter it causes the program to crash. I have spent several hours poring over this program, and nothing yet, although I have suspicions that the answer lies in the adapter. Can anyone shed some light on this?
Create and set adapter:
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView = findViewById(R.id.navRV);
mRecyclerView.setHasFixedSize(false);
mRecyclerView.setLayoutManager(mLayoutManager);
Date todaysDate = new Date(System.currentTimeMillis());
AnnouncementList.add("Test Announcement 1", "This is some random text in the body" +
". It really needs to show up correctly!!", todaysDate);
mAdapter = new TwitterAdapter(mContext);
mRecyclerView.setAdapter(mAdapter);
And this is the adapter:
package com.company.application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Twitter Adapter:
* This adapter is the interface between the data and the view
*/
public class TwitterAdapter extends RecyclerView.Adapter<TwitterAdapter.ViewHolder> {
//Use this as the tag for logging in this activity
final private String LOGTAG = "TWITTER_ADAPTER";
private Context mContext;
public TwitterAdapter(Context mContext) {
this.mContext = mContext;
}
#NonNull
#Override
public TwitterAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_layout, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull TwitterAdapter.ViewHolder holder, int position) {
//Set the text and the title
holder.textViewBody.setText((String)AnnouncementList.getList().get(position).getBody());
holder.textViewTitle.setText((String)AnnouncementList.getList().get(position).getTitle());
//If we have an image, insert it so we can draw it
if(AnnouncementList.getList().get(position).getImage() != null)
holder.imageView.setImageDrawable((Drawable)AnnouncementList.getList().get(position).getImage());
else
holder.imageView.setImageDrawable(null);
}
#Override
public int getItemCount() {
Log.d(LOGTAG, "getItemCount returns " + AnnouncementList.size());
return AnnouncementList.size();
}
/**
* Holds the view so that the RecyclerView can process it
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public RelativeLayout mRelativeLayout;
ImageView imageView;
TextView textViewTitle, textViewBody, textViewDate;
public ViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewBody = itemView.findViewById(R.id.textViewBody);
// textViewDate = itemView.findViewById(R.id.textViewDate);
}
}
}
You need to pass the the arraylist to adapter like this
mAdapter = new TwitterAdapter(mContext, AnnouncementList);
Then in the adapter change the constructor like this
private Context mContext;
private ArrayList<ModelClass> mAnnouncementList = new ArrayList();
public TwitterAdapter(Context mContext,ArrayList<ModelClass> mAnnouncementList) {
this.mContext = mContext;
this.mAnnouncementList= mAnnouncementList;
}
and then access variables like this
holder.textViewBody.setText(mAnnouncementList.getList().get(position).getBody());
holder.textViewTitle.setText(mAnnouncementList.getList().get(position).getTitle());
dont forget to return the size like below
#Override
public int getItemCount() {
Log.d(LOGTAG, "getItemCount returns " + mAnnouncementList.size());
return mAnnouncementList.size();
}
I am trying to retrieve an image from an SQLite database. The image is stored as a BLOB and trying to retrieve it using an array. I'm not sure why its doing this. The permissions are all set correctly.
package com.example.joao_.quizathonegroupteamproject.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.joao_.quizathonegroupteamproject.DatabaseClasses.User;
import com.example.joao_.quizathonegroupteamproject.R;
import java.util.ArrayList;
/**
* Created by Quoc Nguyen on 13-Dec-16.
*/
public class UserListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<User> foodsList;
public UserListAdapter(Context context, int layout, ArrayList<User> foodsList) {
this.context = context;
this.layout = layout;
this.foodsList = foodsList;
}
#Override
public int getCount() {
return foodsList.size();
}
#Override
public Object getItem(int position) {
return foodsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView imageView;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
View row = view;
ViewHolder holder = new ViewHolder();
if(row == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layout, null);
holder.imageView = (ImageView) row.findViewById(R.id.imgFood);
row.setTag(holder);
}
else {
holder = (ViewHolder) row.getTag();
}
User food = foodsList.get(position);
byte[] tblUsersImage = food.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(tblUsersImage, 0, tblUsersImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
You can retrieve BLOB image and stored it in Byte array like this:
byte[] array = cursor.getBlob(columnIndex);
Bitmap bitmap = BitmapFactory.decodeByteArray(array, 0 ,array.length);
and then set it into image view
holder.imageView.setImageBitmap(bitmap);
you didn't provide your SQLite database class but i think , your food.getImage(); return null value .
check dataInsert method OR getData in your SQLite db !
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi i am new to android development. I have made an app where picture are now shown in a gridview. But i want to make it more user friendly. I have found an example but i need some help to implement my code as the example. I want to write my UserList.java as ImageAdapter.java. I want to use imageview instead of holder. How can i do that??
Example i am trying to follow:
ImageAdapter.java
package com.step2rock.www.photographynowroadtopro;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by Sushimz on 5/7/2016.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
// R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10,
// R.drawable.pic_11, R.drawable.pic_12,
// R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15
};
// 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(70, 70));
return imageView;
}
}
Now Here is my code.
UserList.java
package com.step2rock.www.adapter;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.step2rock.www.crudproject.R;
import com.step2rock.www.crudproject.UserlistActivity;
import com.step2rock.www.model.User;
public class UserList extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
UserlistActivity activity;
ArrayList<User> users;
public UserList(Context context, ArrayList<User> users) {
mContext = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.users = users;
}
#Override
public int getCount() {
return users.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
static class ViewHolder {
public ImageView ivUserImage;
public TextView tvUserName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
activity = new UserlistActivity();
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.userrow_activity, null);
holder.ivUserImage = (ImageView) convertView.findViewById(R.id.ivUserImage);
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvHeader);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = new User();
user = users.get(position);
holder.ivUserImage.setImageBitmap(convertToBitmap(user.get_user_pic()));
holder.tvUserName.setText(user.get_first_name());
return convertView;
}
public Bitmap convertToBitmap(String base64String) {
byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT);
Bitmap bitmapResult = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return bitmapResult;
}
}
You are new to android and want to create a grid of showing pictures.
You can develop the grid like below Follow this tutorial.
I created a simple android application with a ListView in it. I have my custom list adapter below:
package com.example.android.efaas;
import java.util.List;
import com.example.android.R;
import com.example.android.efaas.bean.ListItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
LayoutInflater inflater = null;
Context ctx;
List<ListItem> data;
public CustomListAdapter(Activity activity, List<ListItem> data){
ctx = activity;
this.data = data;
inflater = ( LayoutInflater )ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int pos) {
return pos;
}
#Override
public long getItemId(int pos) {
return pos;
}
#Override
public View getView(int pos, View view, ViewGroup vgroup) {
View rowView = inflater.inflate(R.layout.list_menu, null);
ImageView img = (ImageView) rowView.findViewById(R.id.list_menu_image);
TextView title = (TextView) rowView.findViewById(R.id.list_menu_title);
ListItem item = data.get(pos);
img.setImageResource(item.getId());
title.setText(item.getTitle());
return rowView;
}
public ListItem getListItem(int pos){
return data.get(pos);
}
}
I have a public method getListItem, I tried to call it during ListView onClick but somehow I cant seem to make it work. Here's the image of the problem:
How do I resolve this?
You should cast adapter.getAdapter() to CustomListAdapter class then
the compile time error is due of the fact that adapter is not your CustomListAdapter. What you can do is cast the return value of adapter.getAdapter() to CustomListAdapter. Conversely you should use what's already available to retrieve the object at position. Like getItemAtPosition(int)