Got strange in BaseAdapter - java

It should be simple part of app with GridView and Images in it, when button click go to camera, capture new photo and add it to GridView
Here I'm apply ImageAdapter extends BaseAdapter
partItem = getPart(position);
StaticGridView gridView = (StaticGridView) view.findViewById(R.id.ImagesGrid);
imageAdapter = new ImageAdapter(mContext, partItem.P_Code, partItem.Images);
gridView.setAdapter(imageAdapter);
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Map<Integer, String> partImages;
public String P_Code;
private static final String TAG = "ImageAdapter";
public ImageAdapter(Context c, String _P_Code, Map<Integer, String> _partImages) {
this.mContext = c;
this.partImages = _partImages;
this.P_Code = _P_Code;
}
public void changeModelList(Map<Integer, String> _partImages) {
partImages = _partImages;
notifyDataSetChanged();
}
public int getCount() {
return partImages.size();
}
#Override
public Object getItem(int position) {
return partImages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, GridView.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(mContext)
.load(API.ApiUrlBase + "/imagepreview/android" + getItem(position))
.placeholder(android.R.drawable.ic_menu_gallery)
.into(imageView);
Log.d(TAG, position + ":" + String.valueOf(getItem(position)));
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
}
got in log
: 1:/data/44452.P1090185.JPG
: 2:/data/44452.P1090186.JPG
: 3:/data/44452.P1090187.JPG
: 0:/data/44452.P1090184.JPG
: 0:/data/44452.P1090184.JPG
it's ok becose of Picasso, but when i try to add new image
partItem.Images.put(imageAdapter.getCount(), mCurrentPhotoPath);
// imageAdapter.notifyDataSetChanged();
imageAdapter.changeModelList(partItem.Images);
got in log
{
4=file:/storage/sdcard/Pictures/JPEG_20160306_213748_1027396172.jpg,
1=/data/44452.P1090185.JPG,
0=/data/44452.P1090184.JPG,
3=/data/44452.P1090187.JPG,
2=/data/44452.P1090186.JPG
}
: 0:/data/44452.P1090184.JPG
and first image appears at the GridView but there is no new file.
Is anyone have an idea?
GridView screenshot

So simple...
I gues BaseAdapter can't work with HashMap
changed this
public Map<Integer, String> partImages;
to this
public ArrayList partImages;

Related

how to create method in activity to put it in custom adapter listview?

at first I got an error " The code of method getView(int, View, ViewGroup) is exceeding the 65535 bytes limit " because I typed too much code, finally I created a method, but the method is error because the variable inside is not detected
I created Method _Differ();
public void _Differ() {
if (_position == 5) {
full.setText("iiii");
}
}
i put method Differ(); in custom adapter listview
this is my code in listview custom adapter
public class Listview1Adapter extends BaseAdapter {
ArrayList<HashMap<String, Object>> _data;
public Listview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
_data = _arr;
}
#Override
public int getCount() {
return _data.size();
}
#Override
public HashMap<String, Object> getItem(int _index) {
return _data.get(_index);
}
#Override
public long getItemId(int _index) {
return _index;
}
#Override
public View getView(final int _position, View _v, ViewGroup _container) {
LayoutInflater _inflater = getLayoutInflater();
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.ccc, null);
}
final LinearLayout linear1 = _view.findViewById(R.id.linear1);
final LinearLayout linear2 = _view.findViewById(R.id.linear2);
final LinearLayout linear3 = _view.findViewById(R.id.linear3);
final LinearLayout linear4 = _view.findViewById(R.id.linear4);
final TextView textview1 = _view.findViewById(R.id.textview1);
final TextView full = _view.findViewById(R.id.full);
final TextView textview2 = _view.findViewById(R.id.textview2);
final TextView shortt = _view.findViewById(R.id.shortt);
final TextView name = _view.findViewById(R.id.name);
final TextView email = _view.findViewById(R.id.email);
full.setText(mapList.get((int)_position).get("end").toString());
shortt.setText(mapList.get((int)_position).get("rule").toString());
email.setText(mapList.get((int)_position).get("start").toString());
_Differ();
return _view;
}
}
//This is your method which will go in your activity >>
public void _Differ(TextView txt, int _position) {
if (_position == 5) {
txt.setText("iiii");
}
}
// This Code Goes in your ListAdpater >>
full.setText(mapList.get((int)_position).get("end").toString());
shortt.setText(mapList.get((int)_position).get("rule").toString());
email.setText(mapList.get((int)_position).get("start").toString());
_Differ(full, _position);
I think this can help you, By the way, Are you using Sketchware Pro 🤔

Android Icon/Image setting pattern for multiple choices

What pattern do I have to use, if I have ListView in which ImageView and like 500 different icons that could be set on that ImageView. Should I just write If/Switch statement, or there is another way/pattern to do it?. Thanks in advance!
Let me assume that you know what icon(I mean the name of icon) to be loaded into the imageView and those icons are available in your drawable resource folder. In this case
#Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
DataItem dataItem = dataList.get(holder.getAdapterPosistion());
try {
int resID = activityContext.getResources().getIdentifier(dataItem.getIconName() , "drawable"/**resource folder name*/, activityContext.getPackageName());
holder.imageView.setBackgroundResource(resID);
} catch (Exception e) {
throw new RuntimeException("Error getting Resource ID.", e)
}
}
Where are these icons that you want to set? you are getting them from server or they are stored locally in your application file? or they are from user phone gallery?
Here is the code you want for your adapter:
public class MyAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private ArrayList<String> mIconNames;
public MyAdapter(Context context) {
mContext = context;
mIconNames = getIconNames();
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mIconNames.size();
}
#Override
public Object getItem(int position) {
return mIconNames.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get view for row item
View rowView = mInflater.inflate(R.layout.your_layout, parent, false);
ImageView thumbnailImageView =
(ImageView) rowView.findViewById(R.id.your_image_view_id);
Picasso.with(mContext).load(mIconNames.get(position)).placeholder(R.mipmap.ic_launcher).into(thumbnailImageView);
return rowView;
}
//this method builds your icon names
private ArrayList<String> getIconNames() {
ArrayList<String> iconNames = new ArrayList<>();
int numberOfIcons = 99;
String iconBaseName = "icon";
for (int i = 1; i < numberOfIcons; i++) {
iconNames.add(iconBaseName + i);
}
return iconNames;
}
}

How to make one view bigger than the other views in RecyclerView?

I have few images in the json API, and I managed to fetch those images using volley Library. I used recyclerview with an image adapter to display image views vertically in two columns, but I want to make one image big and display it as the first image that user can click on. Also that image will be changed each interval of time. Basically, the API will do all the backend task like setting the time and telling which image to be displayed on the top of the recyclerview if not all images must have the same size and be shown in 2 columns vertically.
I have square layout class for recyclerview. I just want to know how I can do this. Even the concept will be fine.
you will have to create two view one for small item other for the header item and in onBindViewHolder have to bind those accordingly the following code is an example of a news article app and should help.
public class AdapterNewsArticlesListWithHeader extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<NewsArticles> items = new ArrayList<>();
private Context ctx;
private NewsArticles header;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, NewsArticles obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
// Provide a suitable constructor (depends on the kind of dataset)
public AdapterNewsArticlesListWithHeader(Context context, NewsArticles header, List<NewsArticles> items) {
this.items = items;
this.header = header;
ctx = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView title;
public TextView short_content;
public TextView date;
public ImageView image;
public LinearLayout lyt_parent;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
short_content = (TextView) v.findViewById(R.id.short_content);
date = (TextView) v.findViewById(R.id.date);
image = (ImageView) v.findViewById(R.id.image);
lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
}
}
class ViewHolderHeader extends RecyclerView.ViewHolder {
public TextView title;
public TextView date;
public ImageView image;
public LinearLayout lyt_parent;
public ViewHolderHeader(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
date = (TextView) v.findViewById(R.id.date);
image = (ImageView) v.findViewById(R.id.image);
lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_header, parent, false);
return new ViewHolderHeader(v);
} else if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_news, parent, false);
return new ViewHolder(v);
}
return null;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderHeader) {
ViewHolderHeader vHeader = (ViewHolderHeader) holder;
vHeader.title.setText(header.getTitle());
vHeader.date.setText(header.getDate());
Picasso.with(ctx).load(header.getImage()).into(vHeader.image);
vHeader.lyt_parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO: do your thing
}
});
} else if (holder instanceof ViewHolder) {
final NewsArticles c = items.get(position);
ViewHolder vItem = (ViewHolder) holder;
vItem.title.setText(c.getTitle());
vItem.short_content.setText(c.getShort_content());
vItem.date.setText(c.getDate());
Picasso.with(ctx).load(c.getImage()).into(vItem.image);
vItem.lyt_parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO: do your thing
}
});
}
}
// need to override this method
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
public NewsArticles getItem(int position) {
return items.get(position);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return items.size();
}
}

List<Map<String, List<>>> in BaseAdapter, Android

This question has been asked previously. Sorry, I am new to Android, those answers didn't helped me.
I am trying to parse the following json using retrofit,Android. In the json below, objects 4 and 1 are Dynamic. So I am using Map to get the data.
{
"effect_list":[
{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
]
}
Now I want to display the json data inside listview using BaseAdapter.
I tried
MyContactAdapter2.java
public class MyContactAdapter2 extends BaseAdapter {
List<Map<String, List<EffectList>>> contactList;
Context context;
private LayoutInflater mInflater;
// Constructors
public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = objects;
}
#Override
public int getCount() {
return contactList.size();
}
#Override
public EffectList getItem(int position) {
return (EffectList) contactList.get(position); <----- ERROR HERE DURING RUNTIME
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyContactAdapter2.ViewHolder vh;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
view.setTag(vh);
} else {
vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
}
EffectList item = getItem(position); <----- ERROR HERE DURING RUNTIME
vh.textViewName.setText(item.getEffectsId());
vh.textViewEmail.setText(item.getEffectsName());
return vh.rootView;
}
private static class ViewHolder {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
}
}
}
I am getting the following error during Runtime.
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to my class
What am I doing wrong here?
Return Map<String,List<EffectList>> from getItem method because contactList contains Map as items:
#Override
public Map<String,List<EffectList>> getItem(int position) {
return contactList.get(position);
}
And in getView use keys to access values from Map:
Map<String,List<EffectList>> map=getItem(position);
List<EffectList> effectList = map.get("KEY_NAME");
// get item from effectList using for loop

How to show image and video as thumbnail in grid view?

I have a database which contains list of image and video path. My problem is I have to show all the images and video in a GridView. I have taken list of path from database but I am not able to show them in GridView. Please help me. Here is my code.
Thanks in Advance
public class GridGallery extends Activity
{
ArrayList<String>list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_gallery);
DataModel dbModel = new DataModel(this);
list = dbModel.selectAll();
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
}
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private final Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount()
{
return list.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
for(int i=0;i<list.size();i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(list.get(i));
picturesView.setImageBitmap(mBitmap);
}
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;
}
}
}
Yes..
I got the answer after a long experiment: here it is:
public class GridGallery extends Activity
{
ArrayList<String>list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_gallery);
DataModel dbModel = new DataModel(this);
list = dbModel.selectAll();
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
}
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private final Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount()
{
return list.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
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;
}
}
}
This works fine for me
You have to just add this line before your return of the picturesView
Just add this
picturesView.setImageBitmap(bitmap);
just after this :
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
You will get it what you are willing to get!
Hope it'll help. I know I'm bit late but may be it'll be helpful someone else who is need of it.

Categories

Resources