setting custom adapters to grid views
<p>In my main activity I have a spinner which contains three categories(All,Horizontal and Vertical), below that there is a gridView. I have some images in resource folder which are for both vertical and horizontal mode. I need to set the images to gridView according to selected item on spinner. I am able to set one category into gridView (for e.g all category). When selecting other category gridView did not update. For updating the gridView what should I do? plz help me?</p>

<p>I tried it by using placing three gridViews in my activity and set their visibility to gone, and make it visible when setting adapter(I used three separate adapters but not working.</p>

<p>My code is given below.</p>

<pre><code>     public class MainActivity extends Activity {
    Spinner vsel;
    ArrayList&lt;String&gt; vimage = new ArrayList&lt;String&gt;();
    ArrayList&lt;String&gt; all = new ArrayList&lt;String&gt;();
    ArrayList&lt;String&gt; himage = new ArrayList&lt;String&gt;();
    ArrayList&lt;Integer&gt; allid = new ArrayList&lt;Integer&gt;();   
    ArrayList&lt;Integer&gt; hid = new ArrayList&lt;Integer&gt;();
    ArrayList&lt;Integer&gt; vid = new ArrayList&lt;Integer&gt;();  
    ArrayList&lt;Integer&gt; tot = new ArrayList&lt;Integer&gt;();  
    ArrayList&lt;String&gt; spinner = new ArrayList&lt;String&gt;();
    GridView gv,sgv,gv1,gv2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gv = (GridView) findViewById(R.id.gridView1);
        gv1 = (GridView) findViewById(R.id.gridView2);
        gv2 = (GridView) findViewById(R.id.gridView3);
        Class resources = R.drawable.class;
        java.lang.reflect.Field[] fields = resources.getFields();
         for(java.lang.reflect.Field field : fields)
        {
             try {
                    if(field.getName().contains("split"))
                    {
                        all.add(field.getName());
                        allid.add(field.getInt(fields));
                    }
                    if(field.getName().contains("splith"))
                        {
                            //String iname = field.getName();
                            himage.add(field.getName());
                            hid.add(field.getInt(fields));
                        }
                    if(field.getName().contains("splitv"))
                        {
                            vimage.add(field.getName());
                            vid.add(field.getInt(fields));
                        }
                        /*else
                        {
                            all.add(field.getName());
                            allid.add(field.getInt(fields));
                        }*/
                    }
                 catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                }
        }
         System.out.println("All arraylist-&gt; " + allid);
         System.out.println("Horizontal arraylist-&gt; " + hid);
         System.out.println("Vertical arraylist-&gt; " + vid);
      //  String[] viewsel = {"All","Vertical","Horizontal"};
         spinner.add("All");
         spinner.add("Vertical");
         spinner.add("Horizontal");
        vsel = (Spinner) findViewById(R.id.spinner1); 
        ArrayAdapter&lt;String&gt; sadpater = new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_spinner_item,spinner);
        sadpater.setDropDownViewResource(android.R.layout.simple_spinner_item);
        vsel.setAdapter(sadpater);
        vsel.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                //String item = parent.getItemAtPosition(position).toString();
                String item = parent.getSelectedItem().toString();

                if(item.equals("All"))
                {
                    gv2.setVisibility(View.INVISIBLE);
                    gv1.setVisibility(View.INVISIBLE);


                    Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();

                    gv.setVisibility(View.VISIBLE);
                     gv.setAdapter(new AppsAdapter(MainActivity.this,allid));
                }
                if(item.equals("Vertical"))
                {
                    gv.setVisibility(View.INVISIBLE);
                    gv2.setVisibility(View.INVISIBLE);

                    gv1.setVisibility(View.VISIBLE);
                    Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();

                     gv1.setAdapter(new AppsAdapter1(MainActivity.this,vid));
                }
                 if(item.equals("Horizontal"));
                {
                    gv.setVisibility(View.INVISIBLE);
                    gv1.setVisibility(View.INVISIBLE);

                    Toast.makeText(MainActivity.this,item,Toast.LENGTH_SHORT).show();

                    gv2.setVisibility(View.VISIBLE);
                     gv2.setAdapter(new AppsAdapter3(MainActivity.this,hid));
                }
            }

            public void onNothingSelected(AdapterView&lt;?&gt; view) {
                // TODO Auto-generated method stub

            }
        });
    }


    public class AppsAdapter extends BaseAdapter {
        private Context context;
        ArrayList&lt;Integer&gt; dirsTemp = new ArrayList&lt;Integer&gt;();
        public AppsAdapter(Context c,ArrayList&lt;Integer&gt; allid)
        {

            Log.d("In appsadapter1 all","Hii");
            dirsTemp = allid;

            context = c;

        }


        //---returns the number of images---

        public final int getCount() {

            return dirsTemp.size();


        }

         //---returns the ID of an item--- 

        public final Object getItem(int position) {
            return dirsTemp.get(position);
        }

        public final long getItemId(int position) {

            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //final File f = (File) getItem(position);
            int i = (Integer) getItem(position);
            System.out.println("Dirstemp-&gt;"+dirsTemp);
            ImageView iv;
            if (convertView == null) 
            {
                    iv = new ImageView(context);
                    //iv.setPadding(5, 5, 5, 5);
            }

            else
            {
                    iv = (ImageView) convertView;
            }
            iv.setImageResource(dirsTemp.get(position));


            return iv;          
        }   
    }






    public class AppsAdapter1 extends BaseAdapter {
        private Context context1;
        ArrayList&lt;Integer&gt; dirsTemp1 = new ArrayList&lt;Integer&gt;();
        public AppsAdapter1(Context c,ArrayList&lt;Integer&gt;vid) {
            Log.d("In appsadapter1 vertical","Hii");
            dirsTemp1 = vid;

            context1 = c;

        }


        //---returns the number of images---

        public final int getCount() {

            return dirsTemp1.size();


        }

         //---returns the ID of an item--- 

        public final Object getItem(int position) {
            return dirsTemp1.get(position);
        }

        public final long getItemId(int position) {

            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //final File f = (File) getItem(position);
            int i = (Integer) getItem(position);
            ImageView iv1;
            if (convertView == null) 
            {
                    iv1 = new ImageView(context1);
                    //iv.setPadding(5, 5, 5, 5);
            }

            else
            {
                    iv1 = (ImageView) convertView;
            }
            iv1.setImageResource(dirsTemp1.get(position));


            return iv1;         
        }   
    }  




    public class AppsAdapter3 extends BaseAdapter {
        private Context context2;
        ArrayList&lt;Integer&gt; dirsTemp2 = new ArrayList&lt;Integer&gt;();
        public AppsAdapter3(Context c,ArrayList&lt;Integer&gt;hid) {
            Log.d("In appsadapter1 horizontal","Hii");
            dirsTemp2 = hid;
            context2 = c;


        }


        //---returns the number of images---

        public final int getCount() {

            return dirsTemp2.size();


        }

         //---returns the ID of an item--- 

        public final Object getItem(int position) {
            return dirsTemp2.get(position);
        }

        public final long getItemId(int position) {

            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //final File f = (File) getItem(position);
            int i = (Integer) getItem(position);
            ImageView iv2;
            if (convertView == null) 
            {
                    iv2 = new ImageView(context2);
                    //iv.setPadding(5, 5, 5, 5);
            }

            else
            {
                    iv2 = (ImageView) convertView;
            }
            iv2.setImageResource(dirsTemp2.get(position));


            return iv2;         
        }   
    }


}
</code></pre>
