Adding default view in listview in android - android

I'm using a listview in my android application in which the listitems(in my case it is bitmap images) are loaded dynamically. Actually i'm creating the bitmap images and then it is loaded one by one into the list. what i want is to show all the list items with some default image and update them correspondingly when the bitmap image is created. My code is given below,
public class BitmapDemoActivity extends Activity {
HorizontalListView listview;
Vector<Bitmap> thumbImg;
BitmapCreator creator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
creator=new BitmapCreator();
thumbImg= new Vector<Bitmap>(97);
listview = (HorizontalListView)findViewById(R.id.listview);
listview.setAdapter(new BitmapAdapter());
new AsyncBitmapCreate().execute();
}
private class AsyncBitmapCreate extends AsyncTask<Void, Bitmap, Void>{
//Bitmap[] temp=new Bitmap[44];
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
for(int i=0;i<97;i++){
publishProgress(creator.generateBitmap(i+1));
}
return null;
}
#Override
protected void onProgressUpdate(Bitmap... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
new BitmapAdapter().add(values[0]);
new BitmapAdapter().notifyDataSetChanged();
}
}
class BitmapAdapter extends BaseAdapter{
public void add(Bitmap bitmap)
{
Log.w("My adapter","add");
thumbImg.add(bitmap);
}
#Override
public int getCount() {
return thumbImg.capacity();
}
#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) {
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View retval = inflater.inflate(R.layout.listitem, null);
ImageView img = (ImageView) retval.findViewById(R.id.tImage);
img.setImageBitmap(thumbImg.get(position));
return retval;
}
};
}
Here i'm using a vector in which after creating each bitmap, it is inserted into that vector. I'm using an asynctask to create the bitmap. After each bitmap is created i'm calling notifydatasetchanged() method to update the listview. But now in the output whenever each bitmap image is created it is adding one item in the listview with that image. But my requirement is to show all the 97 items in my list with some default image and whenever bitmap is created update the corresponding listitem.
can anyone help me?? Thanks in advance....

The simplest would be to include the default image as the src for the ImageView with id tImage in your layout listitem.xml. And in your getView method, replace the default image if the Bitmap for that position is available.
ImageView img = (ImageView) retval.findViewById(R.id.tImage);
Bitmap bmp = null;
if(position < thumbImg.size()){
thumbImg.get(position);
}
if(null != bmp){
img.setImageBitmap(bmp);
}

Related

How to load Images with image adapter to gallery from server

I have the following two classes
This is the Image Adapter class:
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = { R.drawable.member, R.drawable.me,
R.drawable.member, R.drawable.me};
public ImageAdapter(Context c) {
this.myContext = c;
}
public int getCount() {
return this.myImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView iv = new ImageView(this.myContext);
iv.setImageResource(this.myImageIds[position]);
// Image should be scaled somehow
//iv.setScaleType(ImageView.ScaleType.CENTER);
//iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
//iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
//iv.setScaleType(ImageView.ScaleType.FIT_XY);
//iv.setScaleType(ImageView.ScaleType.FIT_END);
iv.setLayoutParams(new Gallery.LayoutParams(300, 300));
return iv;
}}
And this is the class wich holds the gallery:
public class GreekStore0 extends Activity{
Gallery myGallery;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.store);
myGallery = (Gallery) findViewById(R.id.gallery1);
myGallery.setAdapter(new ImageAdapter(this));
}}
What i want to do is instead of getting the images from the drawablefolder to get them from a server.
For now in the development i want this server to be my localhost. I am using XAMPP to manage a database that is synced with the application in an other part.
I suggest to not reinvent the wheel and instead use a powerful high quality library for this that handles this both smoothly (code and performance wise), such as Square's excellent open source Picasso library.
Example usage:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Picasso is available here:
http://square.github.io/picasso/
Or use the Smoothie library for ListView/GridView specifically:
https://github.com/lucasr/smoothie

Remove selected item from GridView

Is there any way to remove selected item from gridview.
I want to delete the selected item from my gridview.
I did not find any thing . my code is
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context context)
{
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 5, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1
};
}
//////////////////
public class ImageActivity extends Activity {
ImageAdapter iAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
iAdapter = new ImageAdapter(this);
final GridView gView = (GridView)findViewById(R.id.grid_view);
gView.setAdapter(iAdapter);
gView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//gView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// gView.setItemChecked(position, true);
Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
iAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_image, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_delete)
{
Toast.makeText(this, "Delete",Toast.LENGTH_SHORT ).show();
}
return super.onOptionsItemSelected(item);
}
}
can anyone have idea .
thank
you are using a table :
public Integer[] mThumbIds = {
R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1}
Tables are not modifiable.
Replace it by a List on which you will be able to make add or remove operations. Simply call notifyDataSetChanged when a change is made to let the adapter know that its list has been modified.
As Teovald and pskink suggested, you cannot have a fixed list of images and then implement the remove functionality that you are looking for. If you want to add remove, change your design and make sure your data set is also removable. What you have tried so far seems to be using some really basic code which is good to show basic GridView feature.
Just try this. Create a image data set class which returns the actual images. Store the images in a List that can be modified. Add setters/getters to this data set and also add a method to delete a particular item. Change your image adapter to use the image from this new data set. Whenever an image is deleted, call the notifyDataSetChanged on the adpater.
Good luck

Video thumbnail return null

I am creating thumbnails from videos stored in my sd card ,displaying thumbnails and its names in grid view. On item selected event of the grid view pop ups a dialog and asking x, y, right, bottom positions then pasting it to the main activity . I got the video files, and tried to create thumbnail using media store also am retrieving thumbnail as bitmap, but the bitmap is null. In the grid view video names are shown and i am able to select the corresponding thumbnail and can give positions also am able set the thumbnail to the main activity. The problem is the bitmap is null and bitmap image not showing(text vie video name shown). What's the problem ? I can't figure it out? Plz help me? My code is given below. thanks in advance.
if (f.isFile()) {
if (fName.endsWith(".mpg")
|| fName.endsWith(".mov")
|| fName.endsWith(".wmv")
|| fName.endsWith(".rm")
|| fName.endsWith(".mp4")) {
tv.setText(fName);
path = f.getAbsolutePath();
System.out.println("Video file path=>"+path);
thumb = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(),MediaStore.Video.Thumbnails.MICRO_KIND);
if(thumb==null)
{
/**Every time it printing null**/
System.out.println("Thumb is null");
}
iv.setImageBitmap(thumb);
From ThumbnailUtils.createVideoThumbnail documentation: May return null if the video is corrupt or the format is not supported.
By default, almost all supported formats are mp4 and 3gp. See here: http://developer.android.com/guide/appendix/media-formats.html for full list of default-supported media formats.
If you are creating thumbnail from sd card video this would create ThumbnailUtils.createVideoThumbnail otherwise use a cursor.
See this example.
Try this code. It is getting the thumbnail of videos from urls. instead of pass the path of sd card .it will help you . Dont forgot to add internet permission in manifest file.
public class VideoThumbnailActivity extends Activity {
public static final String Downloader = null;
static String uri1="http://daily3gp.com/vids/lucky_guy.3gp";
static String uri2="http://daily3gp.com/vids/reporter_hit_by_plane.3gp";
static String uri3="http://daily3gp.com/vids/motorcycle_wipesout_explodes.3gp";
static String uri4="http://commonsware.com/misc/test2.3gp";
public static String uri_array[]={uri1,uri2,uri3,uri4,uri1,uri2,uri3,uri4,uri1,uri2,uri3,uri4};
ImageView imageView;
String url;
Gallery ga1,ga2;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.imageView);
ga1 = (Gallery)findViewById(R.id.gallery1);
ga1.setAdapter(new ImageAdapter(getApplicationContext()));
imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(uri_array[0], MediaStore.Video.Thumbnails.FULL_SCREEN_KIND));
//on click event on gallery
ga1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, final int position,long arg3) {
imageView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(uri_array[position], MediaStore.Video.Thumbnails.FULL_SCREEN_KIND));
//on click event on imageview to play video
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),PlayActivity.class);
intent.putExtra("path",uri_array[position]);
startActivity(intent);
}
});
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return uri_array.length;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View view, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
Bitmap curThumb = null;
curThumb = ThumbnailUtils.createVideoThumbnail(uri_array[position],MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
iv.setImageBitmap(curThumb);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
let me know your problem is resolved or not.

Setting the List<Bitmap> to a GridView in Android

I am developing an app where I have a list of bitmaps and I want to place those bitmaps in a gridView. I have the folleowing code.
My Activity for gridView
public class TestBitmap extends Activity {
private Bitmap bitmap;
private ImageView image;
public static List<Bitmap> splittedBitmaps;
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.splitted_grid);
splittedBitmaps = getIntent().getParcelableArrayListExtra("split");
GridView gv = (GridView)findViewById(R.layout.splitted_grid);
gv.setAdapter(new SplittedImageAdapter(this));
}
}
and this is my Adapter class
public class SplittedImageAdapter extends BaseAdapter{
private Context mContext;
public SplittedImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return TestBitmap.splittedBitmaps.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(30,30));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
//imageView.setImageResource(TestBitmap.splittedImages.get(arg0));
imageView.setImageBitmap(TestBitmap.splittedBitmaps.get(arg0));
return imageView;
}
}
I am getting a NullPointerException in the last line of the onCreate method of my Activity class.
Please help me to trace the bug.
In last line of onCreate gb is null. That means (GridView)findViewById(R.layout.splitted_grid); is returning null, which means it is not finding the GridView.
I believe the argument of findViewById is incorrect. R.layout.splitted_grid is a name of layout file. Instead you should use the ID of a view, eg. R.id.your_view_id_here if you have ID specified in your layout XML like that:
<GridView
android:id="#+id/your_view_id_here"
(...)
/>

How to create a Photo Gallery on Android?

I want to create a photo gallery on Android. I'm using Gallery and BaseAdapter to create a scrollable gallery. Now I want to add an action when an image of the gallery is shown (each image's width is the same as screen width), so I need to get its index in the image array. My question is how to get the index of the image which shows on the screen?
I want to get the index of the image as soon as it shows on the screen, not on click event.
I've tried to get current image position in getView(), but the result is strange:
I scroll to image02 but position=2 (should be 1). When I scroll back, image02's position=0 (should be 1).
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
//each image's size is 320x60
R.drawable.btm01,
R.drawable.btm02,
R.drawable.btm03
};
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(320,60));
return imageView;
}
Check this example.
// Reference the Gallery view
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
// Set a item click listener, and just Toast the clicked position
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

Categories

Resources