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
Related
I am making a simple Android list view app which will show some basic name, in list view. My app is stopping unexpectedly. The logcat is showing this error.
Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()'
on a null object reference.
CustomAdapter
public class CustomAdapter extends BaseAdapter {
Context context;
String animals[];
LayoutInflater inflater;
public CustomAdapter(Context applicationContext, String[] animals){
this.context = applicationContext;
this.animals = animals;
inflater = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return animals.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.listview, null);
TextView textView = (TextView) view.findViewById(R.id.tv);
textView.setText(animals[i]);
return null;
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
ListView listView;
String animals[]= {"Dog", "Cat", "Humans", "Monkey", "Rat", "Snake", "Elephant", "Giraffe", "Deer", "Tiger", "Lion","Cow", "Pig", "Goat"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView =(ListView) findViewById(R.id.lv);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), animals);
listView.setAdapter(customAdapter);
}
}
Here you did Mistakes
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.listview, null);
TextView textView = (TextView) view.findViewById(R.id.tv);
textView.setText(animals[i]);
return view;
}
And
public long getItemId(int i) {
return animals.length;
}
Screenshort
I've created a Fragment that should display some emojis in my GridView, when I try to put the URLs statically outside the public void call method the images get displayed successfully however when I put the code inside this method, the images don't display, I'm doing this for testing as I'm going to make it dynamic, but this is just for testing.
So, appreciate your assistance to advise why the images are not appearing inside this method specifically.
BottomSheetFragment.java
public class BottomSheetFragment extends BottomSheetDialogFragment {
ArrayList<EmojiModel> emojieModels = new ArrayList<>();;
ArrayList<String> imageId = new ArrayList<>();
View grid;
GridView gridView;
ImageAdapter2 imageAdapter2;
public BottomSheetFragment() {
// Empty Constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.gifts_layout_2, container,
false);
gridView = (GridView)rootView.findViewById(R.id.grid_view2);
getEmojies();
imageAdapter2 = new ImageAdapter2(getActivity().getApplicationContext(),
imageId);
gridView.setAdapter(imageAdapter2);
return rootView;
}
public void getEmojies(){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
Communicator.getInstance().on("subscribe start", new
Emitter.Listener() {
#Override
public void call(Object... args) {
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
}
});
}
});
}
public class ImageAdapter2 extends BaseAdapter {
private Context context;
private ArrayList<String> emojieImages = new ArrayList<>();
// Constructor
public ImageAdapter2(Context context, ArrayList<String> emojieImages) {
this.context = context;
this.emojieImages = emojieImages;
}
#Override
public int getCount() {
return emojieImages.size();
}
#Override
public Object getItem(int position) {
return emojieImages.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
ImageView imageView;
}
#Override
public View getView(final int position, View convertView, ViewGroup
parent) {
Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater)
getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smiles_items_layout, null);
holder.imageView = (ImageView)
grid.findViewById(R.id.smile_image_view);
Picasso.with(getApplicationContext())
.load(emojieImages.get(position))
.fit()
.centerInside()
.into(holder.imageView);
notifyDataSetChanged();
return grid;
}
}
}
That's because your method runOnUIThread is asynchronous.
To make it work you should set the adapter after the images are loaded:
#Override
public void call(Object... args) {
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageId.add("https://static.pexels.com/photos/247932/pexels-photo-
247932.jpeg");
imageAdapter2 = new ImageAdapter2(getActivity(), imageId);
gridView.setAdapter(imageAdapter2);
}
Also note that you don't need to call runOnUIThread inside a Fragment, it is already running on that thread.
Maybe your image access problems
https://static.pexels.com/photos/247932/pexels-photo-%20247932.jpeg
Can not access
I am working on a setup screen that allows the user to change the color theme of the app. I want to use a GridView of colored square images for the user to choose from. Right now I have a toast message that pops with the position in the color image array when clicked. The following opens my theme activity from a setup menu:
themeButton = (ImageButton) findViewById(R.id.imBtnAccentColor);
themeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SetupContactActivity.this, ThemeActivity.class);
startActivity(intent);
}
});
Here's an image of the GridView that the user will choose a theme color from.
gridViewTiles
Theme Activity:
public class ThemeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(ThemeActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is an ImageAdapter class that helps build the image view based on the number of images in my array of images:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(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;
if (convertView == null) {
// If it's not recycled, initialize some attributes.
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(320, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// References to the color images
private Integer[] mThumbIds = {
R.drawable.orange,
R.drawable.yellow,
R.drawable.yellow_orange,
R.drawable.brown,
R.drawable.medium_grey,
R.drawable.dark_grey,
R.drawable.pink,
R.drawable.light_blue,
R.drawable.medium_green,
R.drawable.purple,
R.drawable.blue,
R.drawable.brownish_green,
R.drawable.dark_purple,
R.drawable.dark_blue,
R.drawable.green
};
}
I'm trying to create a 2-line list view in my activity but I've come across one error that I don't know how to fix. How can I achieve this the AppCompat way? I want to achieve something like in the screenshot attached. My error is on line 52.
public class WCLineActivity extends ActionBarActivity {
private class Sample {
private CharSequence title;
private CharSequence summary;
private Class<? extends Activity> activityClass;
public Sample(int titleResId, int summaryResId, Class<? extends Activity> activityClass) {
this.activityClass = activityClass;
this.title = getResources().getString(titleResId);
this.summary = getResources().getString(summaryResId);
}
#Override
public String toString() { return title.toString(); }
public String getSummary(){ return summary.toString(); }
}
private static Sample[] mSamples;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wc_line);
// Instantiate the list of samples.
mSamples = new Sample[]{
new Sample(R.string.bank, R.string.zone_1, MainActivity.class),
new Sample(R.string.waterloo, R.string.zone_1, MainActivity.class)
};
setListAdapter(new MyAdapter(this, mSamples));
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView title;
TextView summary;
}
LayoutInflater inflater;
Sample[] mSamples;
public MyAdapter(Context contexts, Sample[] samples) {
this.mSamples = samples;
inflater = LayoutInflater.from(contexts);
}
#Override
public int getCount() {
return mSamples.length;
}
#Override
public Object getItem(int position) {
return mSamples[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.title = (TextView) convertView.findViewById(R.id.list_item_title);
viewHolder.summary = (TextView) convertView.findViewById(R.id.list_item_subtitle);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(mSamples[position].title);
viewHolder.summary.setText(mSamples[position].getSummary());
return convertView;
}
}
}
setListAdapter is a method of ListActivity, and it is not available in Activity. To overcame it, you can declare a ListView in your layout, retrieve the ListView with findViewById and call setAdapter, on that object. E.g.
ListView listView = (ListView) findViewById(R.id.id_list);
listView.setAdapter(new MyAdapter(this, mSamples));
I tried with your code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wc_line);
ListView listView = (ListView) findViewById(R.id.listView);
// Instantiate the list of samples.
mSamples = new Sample[]{
new Sample(R.string.bank, R.string.zone_1, MainActivity.class),
new Sample(R.string.waterloo, R.string.zone_1, MainActivity.class)
};
listView.setAdapter(new MyAdapter(this, mSamples));
}
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.