ImageAdapter cannot be applied to fragment class - java

I've followed the documentation on how to use GridView and had the same issue as this guy ImageAdapter cannot be applied to a Fragment Class
The code in my Fragment Class is as follows
public class SecondFragment extends Fragment {
View myView;
GridView gridview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.second_layout, container, false);
gridview = (GridView) myView.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(getActivity()));
return myView;
}
}
However I'm getting an error in the second to last line "Image Adapter cannot be applied to android.app.activity"
My ImageAdapter is as follows
public class ImageAdapter extends BaseAdapter {
private Context mcontext;
#Override
public int getCount() {
return mthumbids.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
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;
}
return imageView;
}
private Integer[] mthumbids =
{
R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4,
R.drawable.img5, R.drawable.img6
};
}

You need to have a constructor in your ImageAdapter class which will take the context as a parameter.
And you have to set the background of the image based on the adapter position as well.
Here's the modified adapter class.
public class ImageAdapter extends BaseAdapter {
private Context mcontext;
public ImageAdapter (Context context) {
mContext = context;
}
#Override
public int getCount() {
return mthumbids.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mcontext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
// Add the following to load the image
imageView.setBackground(ContextCompat.getDrawable(context, mthumbids[position]));
}
else {
imageView = (ImageView)convertView;
}
return imageView;
}
private Integer[] mthumbids =
{
R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4,
R.drawable.img5, R.drawable.img6
};
}

You can also get the context from the parent in getView(). Then you don't have to pass and store the context. So your getView looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
Context context = parent.getContext(); // <-- add this line
imageView = new ImageView(context); // use the context from the parent
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else {
imageView = (ImageView)convertView;
}
return imageView;
}

Missing constructor with argument context for ImageAdapter.
Only empty constructor will be created automatically
you should write constructors with specific argument values.
public ImageAdapter(Context context){
mContext = context;
}

Add this code in ImageAdapter class.
public ImageAdapter (Context context) {
super();
mContext = context;
}

Related

GridView.setOnItemClickListener triggers whole row

I have a grid view 3x3. Inside this grid I have ImageViews.
I set up OnItemClickListener but when I click on the ImageView only the first one is pressed in a row no matter which column I am pressing. Also I can be pressing outside the ImageView (empty space) but still first ImageView on that row is pressed.
XML
<GridView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
MainActivity
GridView gridview = (GridView) findViewById(R.id.test);
gridview.setAdapter(new HexAdapter(getBaseContext()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.e("gridView", "hallo" + position);
}
});
and this is my adapter
public class HexAdapter extends BaseAdapter {
private Context mContext;
public HexAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return (Object) mThumbIds[position];
}
public long getItemId(int position) {
return position;
}
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.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo,
};
}
R.drawable.logo is just a random image a chose.
I have experienced this problem and the solution I came up with is simple:
In your adapter give an unique TAG to each view
view.setTag(id); //example
And on ItemClickListener just match the tags for each view and you will be sure this will never happen:
if(view.getTag().equals(id))
doStuff();
if(view.getTag().equals(otherId))
doOtherStuff();
And so on.
Here is an example:
Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ImageView imageView;
CustomTextView textView;
LinearLayout main_grl;
if (v == null) {
v = layoutInflater.inflate(R.layout.professions_gridview_item,parent,false);
}
main_grl = (LinearLayout)v.findViewById(R.id.main_grl);
main_grl.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ps_background_selector));
imageView = (ImageView) v.findViewById(R.id.imageView);
textView = (CustomTextView) v.findViewById(R.id.textView);
imageView.setImageResource(ApplicationConstants.finalListIV[position]);
textView.setText(ApplicationConstants.finalListTV[position]);
v.setTag(ApplicationConstants.finalListTV[position]);
return v;
}
Activity
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if((Integer) view.getTag() == someTag){
//doStuff
} else if((Integer) view.getTag() == otherTag){
//doOtherStuff
} else{
//andSomeOther
}
}

Android: how to use BaseAdapter class without having a nullpointerexception

I understand that many of you are already fed up with the nullpointerexception-related questions, so I thought again and again whether or not to ask this question. I read up the following short code for an hour, and I still cannot figure out why the nullpointerexception is thrown..
Here is the ImageAdapter class..
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public ImageAdapter(Context context) {
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.dialog_addprofile, null);
convertView.setLayoutParams(new GridView.LayoutParams(90, 90));
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.categoryText);
holder.icon = (ImageView) convertView.findViewById(R.id.categoryimage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.icon.setAdjustViewBounds(true);
holder.icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.icon.setPadding(8, 8, 8, 8);
holder.title.setText(categoryContent[position]);
holder.icon.setImageResource(mThumbIds[position]);
return convertView;
}
class ViewHolder {
TextView title;
ImageView icon;
}
private Integer[] mThumbIds = {
android.R.drawable.ic_menu_gallery, android.R.drawable.ic_menu_camera
};
private String[] categoryContent = {
"Gallery", "Camera"
};
}
And here's the part of the logcat.
java.lang.NullPointerException
at com.marshall.thequizshow.ui.adapter.ImageAdapter.getView(ImageAdapter.java:56)
According to the logcat, the end part below the if-else statement in the getView method seems to have the problem. I tried really hard to figure out what should be fixed, but I'm still lost. Somebody please help me?
Add this line in your constructor
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

non static variable cannot be referenced from a static context in Adapter

I am using Base Adapter for displaying image in grid view. The working fine when images are fixed IMAGE_URLS directly but am try with geting url from list and assign into IMAGE_URLS that shows non static variable cannot be referenced from a static context. I don't know how to solve this issue. please help to solve this issue
public class ImageGridFragment extends AbsListViewBaseFragment {
public static final int INDEX = 1;
String description="";
ArrayList<String> img = new ArrayList<String>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fr_image_grid, container, false);
listView = (GridView) rootView.findViewById(R.id.grid);
img.clear();
Bundle bundle = this.getArguments();
description = bundle.getString("description");
String[] separated= description.split(",");
for(int i=0;i<separated.length;i++)
{
img.add(separated[i]);
}
ImageGalleryFragment.imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
((GridView) listView).setAdapter(new ImageAdapter(getActivity()));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
return rootView;
}
private static class ImageAdapter extends BaseAdapter {
String[] IMAGE_URLS = img.toArray(new String[img.size()]);
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context) {
inflater = LayoutInflater.from(context);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
#Override
public int getCount() {
return IMAGE_URLS.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
#Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
Change the class from private static to "private class ImageAdapter".

GridView in android, gridView.setAdapater(this) error

I follow some tutorial "How to use GridView in android" and try to make Gallery....
but why?
I already did same with tutorial did....
this file GridActivity.java :
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
and this for file ImageAdapter.java :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapater(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId (int position) {
return 0;
}
public View getView(int position, View converView, 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;
}
public Integer[] mThumbIds = {
R.drawable.image1
};
}
there's no error...
but when I tried to run with emulator...
"Unfotunetly, ........... has stop"
please....help...me..
new ImageAdapter(this) Needs a constructor that takes context as the param.
Change this
public void ImageAdapater(Context c) { //remove void for constructor
to
public ImageAdapater(Context c) {
More info
http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

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