This is my code
public class ImageAdapter extends BaseAdapter {
ImageView imageView;
private Context context;
public int Images[] = {Integer.parseInt(("http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg")),
Integer.parseInt("https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300"),
Integer.parseInt("https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg"),
Integer.parseInt("http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg")};
public ImageAdapter (Context c){
context= c;
}
#Override
public int getCount() {
return Images.length;
}
#Override
public Object getItem(int position) {
return Images[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview = new ImageView(context);
imageview.setImageResource(Images[position]);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setLayoutParams(new GridView.LayoutParams(240,240));
Picasso.with(context).load(Images[position]).into(imageview);
return imageview;
}
}
I am trying to implement Picasso in my project the problem i am facing is
I have an int array of Images .and i am getting error java.lang.NumberFormatException ,i guess its because there is no pars Int in array of Images.And if i make it String i.e
public String Images[] = {("URL"),("URL"),("URL"),("URL"),("URL")};
But setimageResourcewants an int value please help me i am stuck with this .Any help would really be appreciated .
Thanks.
Syntax from the picasso site:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Use a String[] instead of a Integer[] would be the correct solution
public String[] Images = { "http://www.fashionlady.in/wp-content/uploads/2016/03/creative-punjabi-mehndi-design-2016.jpg",
"https://lumiere-a.akamaihd.net/v1/images/uk_toystory_chi_woody_n_5b5a006f.png?region=0,0,300,300",
"https://lumiere-a.akamaihd.net/v1/images/open-uri20150422-20810-10n7ovy_9b42e613.jpeg",
"http://www.wetpaint.com/wp-content/uploads/2015/11/toy-story-20th-anniversary.jpg" };
You don't need imageview.setImageResource(Images[position]);when you load the image with Picasso, so just remove that line
Related
I am creating an auto slider in android and it is working with drawable images i want to add images getting from server using rest api how can i do that please help here is my code. this is my adapter where i am passing drawable images to the slider...
public class HomeImageAdapter extends PagerAdapter {
private Context mContext;
private int[] mImageIds = new int[]{R.drawable.slideone , R.drawable.slideone , R.drawable.slideone};
public HomeImageAdapter(Context context){
mContext = context;
}
#Override
public int getCount() {
return mImageIds.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view == o;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(mImageIds[position]);
container.addView(imageView , 0);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((ImageView) object);
}
}
In the above code i want to pass array of images getting from server please help
I'm beginner in programming in Android Studio and I'm making now some kind of messenger via bluetooth. So I have my own ArrayAdapter class which extends ArrayAdapter class and it is for outgoing and incoming messages. I want incoming messages to be at the left side ang outgoing ones at the right, so I made two layouts for this Adapter. I know, that on stackoverflow there is a lot of solutions to make ArrayAdapter with few diffrent layouts for each row, but every one of them doesn't work - changing layouts cause change view of every row. So my solution is to make another ArrayList of booleans, and in getView() I check what I have in this List - true or false - and use right layout on that row in ArrayAdapter (I'm checking it by position field from getView()). And when I send a lot of messages to second device and try to response to first device there is NullPointerException in line with
(TextView)row.findViewById(R.id.singleIncomingMessage);
or
(TextView)row.findViewById(R.id.singleOutgoingMessage);
This exceptions seems to appear in random situation, but of course there must be some pattern. Here's the whole code. What it's wrong? And I'm sorry for my language if there is some misspells ;)
public class MyArrayAdapter extends ArrayAdapter<String> {
public MyArrayAdapter(Context context, ArrayList<String> list) {
super(context, 0, list);
this.list =list;
this.context=context;
}
ArrayList<String> list;
Context context;
#Override
public int getCount() {
return list.size();
}
#Override
public String getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
if(Messenger.inOut){
return 1;
}
else{
return 0;
}
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int type=getItemViewType(position);
View row=convertView;
if(row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(Messenger.inOutList.get(position)==0){
row=inflater.inflate(R.layout.outgoing_message_layout, parent, false);
}
if(Messenger.inOutList.get(position)==1){
row=inflater.inflate(R.layout.incoming_message_layout, parent, false);
}
}
String message=getItem(position);
TextView label;
if(Messenger.inOutList.get(position)==0){
label=(TextView)row.findViewById(R.id.singleOutgoingMessage);
label.setText(message);
}
if(Messenger.inOutList.get(position)==1){
label=(TextView)row.findViewById(R.id.singleIncomingMessage);
label.setText(message);
}
return row;
}
}
I think the issue is from the case where row is non-null. It may have previously been inflated as an outgoing message layout, now you are recycling it and trying to treat it as an incoming message layout, so it can't find the TextView.
It's hard to say for sure this is the issue though since I don't really know how the Messenger.* calls behave in your code. Since you already get type in getView you should use that rather than Messenger.inOutList.get(position)==X to determine which view logic to use if you keep it this way. This question has some good answers on how to do this consistently.
Also keep in mind that for this to work getItemViewType must always return the same type for a given position, or else you have to detect the change and inflate a new layout in getView. If Messenger.inOut is constant, there's not reason to use this format (multi-layout format). If it's not a constant and it gets changed, then you need to detect this in getView
Okay, thank you very much, I finally did it. I was using two list (of booleans and messages) and after I read about ArrayAdapter I decided to make class with this two fields and make a list of its object. That error was probably because of that second list of booleans. Here's below my final code if anyone have similiar problem.
public class MyArrayAdapter extends ArrayAdapter<MessageWithType> {
public MyArrayAdapter(Context context, ArrayList<MessageWithType> list) {
super(context, 0, list);
this.list =list;
this.context=context;
}
public static final int TYPE_OUT = 0;
public static final int TYPE_IN = 1;
ArrayList<MessageWithType> list;
Context context;
#Override
public int getCount() {
return list.size();
}
#Override
public MessageWithType getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
if(list.get(position).inOut){
return 1;
}
else{
return 0;
}
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MessageWithType item=getItem(position);
int type=getItemViewType(position);
View row=convertView;
ViewHolder viewHolder=null;
if(row==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type==TYPE_OUT){
row=inflater.inflate(R.layout.outgoing_message_layout, parent, false);
}
if(type==TYPE_IN){
row=inflater.inflate(R.layout.incoming_message_layout, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.singleMessage);
viewHolder=new ViewHolder(label);
row.setTag(viewHolder);
}
else{
viewHolder=(ViewHolder)row.getTag();
}
viewHolder.textView.setText(item.message);
return row;
}
public class ViewHolder{
TextView textView;
public ViewHolder(TextView textView){
this.textView=textView;
}
}
}
I've been working on making a video gallery for myself and got stuck here. Followed this link for some references but still getting some problems:
For making thumbnails for the videos
Here is my code :
public class AddFragment extends Fragment {
private ImageButton imageButton;
private GridView gridView;
private File files;
ArrayList<File> list;
public AddFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_add, container, false);
imageButton = (ImageButton) view.findViewById(R.id.gotoButton);
gridView = (GridView) view.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(getContext()));
list = videoReader(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//for making the button visible as ssonas the item gets selected
imageButton.setVisibility(view.VISIBLE);
}
});
return view;
}
ArrayList<File> videoReader(File root) {
ArrayList<File> arrayList = new ArrayList<>();
File[] file = root.listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isDirectory()){
}else{
if(file[i].getName().endsWith(".mp4")){
arrayList.add(file[i]);
}
}
}
return arrayList;
}
public class ImageAdapter extends BaseAdapter{
private Bitmap bitmap;
private final Context context;
private ImageAdapter(Context c){
context = c;
}
//for the video numbers
#Override
public int getCount() {
return list.size();
}
//for getting the video items position vise
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
if(list.get(position).contains(".jpg"))
{
bitmap = BitmapFactory.decodeFile(list.get(position)); //Creation of Thumbnail of image
}
else if(list.get(position).contains(".mp4"))
{
bitmap = ThumbnailUtils.createVideoThumbnail(list.get(position), 0); //Creation of Thumbnail of video
}
picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else
{
picturesView = (ImageView)convertView;
}
return picturesView;
}
} }
The problems I'm getting are in my ImageAdapter class in getView method
These are :
1. In the if(list.get(position).contains(".jpg")) //cannot resolve contains
2. In bitmap = BitmapFactory.decodeFile(list.get(position)); //saying the decodeFile(java.lang.string) from Bitmapfacotory cannot be applied to (java.file.io)
P.S. for the second option I tried doing that after getting reference from this link but failed:
Java contradicting behavior resolved
Try this.
if(list.get(position).contains(".jpg"))
{
bitmap = BitmapFactory.decodeFile(list.get(position).toString());
}
list.get(position) is File object and you need to pass String object so just make it String by writing .toString().
contains expect to see the same object type as the list items. if you want to check if the file is an image read this: Android: How to check if file is image?
Decode file expects to get a file url and not a file object. see here how to use it: Bitmapfactory example
I'm not exactly sure what I'm missing, but I've been trying to get the most popular movie posters to display in a gridview from themoviedb API utilizing the Picasso library. Anyone know what I'm doing wrong?
Here's my ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 20;
}
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.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(mContext).load("https://api.themoviedb.org/3/movie/popular?api_key={APIKEY_HERE}&poster_path&images").into(imageView);
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
Above mentioned API will give popular movie list this..
results: [
{
poster_path: "/inVq3FRqcYIRl2la8iZikYYxFNR.jpg",
adult: false,
overview: "life.",
release_date: "2016-02-09",
backdrop_path: "/nbIrDhOtUpdD9HKDBRy02a8VhpV.jpg",
popularity: 91.92864,
vote_count: 3497,
video: false,
vote_average: 7.2
}
]
Extract poster_path of which movie image you want to display
Use the below URL to display image http://image.tmdb.org/t/p/w500/inVq3FRqcYIRl2la8iZikYYxFNR.jpg
You can find the document here http://docs.themoviedb.apiary.io/#reference/configuration/configuration
you have to check that url in picasso it returns the whole data not the image url, try to add the value of poster_path to the right url
This is my adapters code:
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.ViewHolder> {
ArrayList<CatModel> objects_;
Context context;
Class res = R.drawable.class;
public class ViewHolder extends RecyclerView.ViewHolder {
TextView cat_text,cat_des;
ImageView cat_img;
public ViewHolder(View v) {
super(v);
cat_text = (TextView) v.findViewById(R.id.cat_txt);
cat_des = (TextView) v.findViewById(R.id.cat_des);
cat_img = (ImageView) v.findViewById(R.id.cat_img);
}
}
public CatAdapter(ArrayList<CatModel> arrayList, Context context) {
this.context = context;
objects_ = arrayList;
}
#Override
public CatAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cat_list_view, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.cat_text.setText(objects_.get(position).txt);
holder.cat_des.setText(objects_.get(position).des);
try {
Field field = res.getField(objects_.get(position).img);
int drawableId = field.getInt(null);
holder.cat_img.setImageDrawable(context.getResources().getDrawable(drawableId));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return objects_.size();
}
}
CatModel class
public class CatModel {
public String txt,img,des;
}
CatModel.img is id of my drawables that I put them in R.strings.
All of my items int arraylist is about 20 items and my drawables are optimized SVG that I converted into vector drawable. but when I'm scrolling it's not smooth. What can I do to optimize it?
You need to access your drawables using the resources directly, instead of reflection.
You should place your drawables in the respective "drawables" folder (drawable-xhdpi, drawable-xxhdpi and so on). Then in your object you reference the int associated with each drawable:
public class CatModel {
public String txt,des;
public int drawable;
}
where the drawable is one of your drawables, like
CatModel catModel = new CatModel();
catModel.drawable = R.drawable.my_drawable_1;
and so on.
Then in your adapter you use it like this:
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.cat_text.setText(objects_.get(position).txt);
holder.cat_des.setText(objects_.get(position).des);
holder.img.setImageResource(objects_.get(position).drawable);
}