Solved

Scroll of Grid View in android

Posted on 2011-03-03
7
6,139 Views
Last Modified: 2012-05-11
Hello friend,

I am working on grid view for displaying an Media store images, In grid view images will be displayed but the scroll is not as smooth as device gallery grid . So please explain me how I  increase the performance of grid in my code, here I attach my code.

Thank You.
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridView=(GridView)findViewById(R.id.gridView);
        String selection = MediaStore.Images.Thumbnails.KIND + "="  + // Select only mini's
        MediaStore.Images.Thumbnails.MINI_KIND;

        Cursor cursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, selection, null, null);
       
        cursor.moveToFirst();
        gridAdapter gAdapter=new gridAdapter(getApplicationContext(), cursor);
        gridView.setAdapter(gAdapter);
    }
    
    class gridAdapter extends BaseAdapter{
    	Context context;
    	Cursor cursor;
    	LayoutInflater inflater;
    	public gridAdapter(Context context,Cursor cursor){
    		this.context=context;
    		this.cursor=cursor;
    		inflater=LayoutInflater.from(context);
    	}
    	
		@Override
		public int getCount() {
			return cursor.getCount();
		}

		@Override
		public Object getItem(int arg0) {
			return cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView img;
			cursor.moveToPosition(position);
			String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
			if(convertView==null){
				img=new ImageView(context);
				img.setLayoutParams(new GridView.LayoutParams(90, 90));

				convertView=img;
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
			}else{
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
			}
			return convertView;
		}
    	
    }
}


       main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<GridView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/gridView"
    android:numColumns="auto_fit"
	android:columnWidth="90dip"
	android:horizontalSpacing="0dip"></GridView>
</LinearLayout>

Open in new window

0
Comment
Question by:jbajaj
  • 3
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 500 total points
ID: 35026194

You are already using convertView, which is there for performance reasons.


In order to see where is the bottleneck you should add some output int the getView method - so you can see which part of the code takes up too much time. Since the method is very short, you could measure time for practicaly each sentence in the method.

Next thing you can do is test the application on a real device, to see how it performs. Or have you already done that? Performance on a emulator is drasticaly slower.
0
 

Author Comment

by:jbajaj
ID: 35026863
Thank you dejanpazin.

I immediately check time to execute  each line in code by adding some output.
But is there any other faster way to see all the images from gallery. Because I also test it on device but the scroll is not as smooth as default gallery in device.
If any other way to do same thing then please help me.

Thank You.
0
 
LVL 17

Assisted Solution

by:Dejan Pažin
Dejan Pažin earned 500 total points
ID: 35027136

You have to find the bottleneck - the point where it takes too long to execute the code. After that, you can improve that piece of code.

Usually this things are solved by caching images, but then you have to take care of the memory.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:jbajaj
ID: 35027419
Thank you dejanpazin,

I will try to find  bottleneck of my code and then improve it, and also try to caching images if necessary.

Thank you for help.
0
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 35058973

I guess this question is requested to be closed by a mistake - the author stated the reason as 'excellent'.
0
 

Expert Comment

by:_alias99
ID: 35120694
All,
 
Following an 'Objection' by dejanpazin (at http://www.experts-exchange.com/Q_26868664.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
_alias99
Community Support Moderator
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Viber 1 43
Understanding mobile browsers 9 58
How can I stop employees from downloading apps on their company phones? 6 37
Android tablet will not connect to internet. 4 21
Working settings for French ISP Orange "Prêt à Surfer" SIM cards for data connections only. Can't be found anywhere else !
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now