Dynamic GridView?

Put your problem here if it does not fit any of the other categories.

Dynamic GridView?

Postby lokidil » Thu Sep 03, 2009 1:20 pm

Hey i try to load a gridview in a dynamic manner!. By replacing the arrays with vector but it throws Exception! How can i achieve this in a decent manner? Can anyone help me plz!!
lokidil
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Aug 10, 2009 6:08 am

Top

Postby lokidil » Fri Sep 04, 2009 9:14 am

Hey i have the solution now if anybody needs i will post it here!!
lokidil
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Aug 10, 2009 6:08 am

Postby coolalok » Sun Sep 06, 2009 7:23 pm

Please do post it :wink: :D :wink:
Android......
Here I come!!!
User avatar
coolalok
Developer
Developer
 
Posts: 29
Joined: Tue Sep 01, 2009 7:35 pm
Location: Pune,India

Dynamic GridView Sample

Postby lokidil » Mon Sep 07, 2009 5:56 am

Here is the code that is used to add images dynamic in gridview.
Code: Select all
some classname
{
   public GridView gv;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
      Button btnAdd = (Button)findViewById(R.id.btnAdd);
      btnAdd.setOnClickListener(addListener)
      gv = (GridView)findViewById(R.id.gv);
      gv.setAdapter(new ImageAdapter(this));
   }


OnClickListener addListener = new OnClickListener() {
      public void onClick(View v) {
           try{
              Integer[] tmp = new Integer[mThumbIds.length];
              for(int i=0;i<mThumbIds.length;i++){
                 tmp[i]=mThumbIds[i];
              }
           mThumbIds = new Integer[mThumbIds.length+1];
           for(int i=0;i<tmp.length;i++){
              mThumbIds[i]=tmp[i];
          }
           mThumbIds[mThumbIds.length-1]=R.id.image;//here u can put your own resource id that will be added to grid view or this can be your Vector of ids position.
           gv.invalidateViews();
           }
           catch (Exception e) {
              Log.e("Error:-", ""+e);
         }
          }
   };
public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }
        public int getCount() {
           /*if(mThumbIds1!=null)
            return mThumbIds1.size();
           else
              return 0;*/
           return mThumbIds.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 imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                //imageView.setLayoutParams(new GridView.LayoutParams(158,58));
                imageView.setLayoutParams(new GridView.LayoutParams(158,58));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(0, 0, 0, 0);
            } else {
                imageView = (ImageView) convertView;
            }        
        imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }

        private Context mContext;
       
    }
    private Integer[] mThumbIds = {};
}

If you had any doubts regarding the code feel free post here i will reply here always!!
lokidil
Junior Developer
Junior Developer
 
Posts: 13
Joined: Mon Aug 10, 2009 6:08 am

Postby coolalok » Tue Sep 08, 2009 9:36 am

Hey lokidil,
Thanks a lot :)
Android......
Here I come!!!
User avatar
coolalok
Developer
Developer
 
Posts: 29
Joined: Tue Sep 01, 2009 7:35 pm
Location: Pune,India

Re: Dynamic GridView?

Postby shripal25 » Tue Apr 12, 2011 1:01 pm

I need little bit more your help...i want to create gridview in which the photos wil be come from webservice and there should be two buttons next and previous each time only 20 photos on screen and after pressing next the same screen should display next 20 photos. same in in previous button..plz help me out.
shripal25
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 18, 2011 12:23 pm

Top

Re: Dynamic GridView?

Postby wakawaka » Sun Apr 17, 2011 2:37 pm

Its very simple.

Create TableRow and keep it adding programaticaly.
Android Development Tutorials and Code Snippets: http://forum.gssdaily.com/
wakawaka
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Tue Jan 11, 2011 1:51 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests