Skip to content Skip to sidebar Skip to footer

Setting Custom Adapters To Grid Views

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 f

Solution 1:

You don't need three gridViews and three adapters. One is enough. When changing category from spinner , refresh the list allid, vid, hid (used in Adapter to display gridview) and then notifyDataSetChanged() on the adapter.

EDIT: try this:

import java.util.ArrayList;

import org.apache.commons.logging.Log;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


    publicclassddsdsextendsActivity {
   Spinner vsel;
   ArrayList<String> vimage = newArrayList<String>();
   ArrayList<String> all = newArrayList<String>();
   ArrayList<String> himage = newArrayList<String>();
   ArrayList<Integer> allid = newArrayList<Integer>();   
   ArrayList<Integer> hid = newArrayList<Integer>();
   ArrayList<Integer> vid = newArrayList<Integer>();  
   ArrayList<Integer> tot = newArrayList<Integer>();  
   ArrayList<String> spinner = newArrayList<String>();
   GridView gv;

   @OverridepublicvoidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       gv = (GridView) findViewById(R.id.gridView1);

       Classresources= 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-> " + allid);
        System.out.println("Horizontal arraylist-> " + hid);
        System.out.println("Vertical arraylist-> " + vid);
     //  String[] viewsel = {"All","Vertical","Horizontal"};
        spinner.add("All");
        spinner.add("Vertical");
        spinner.add("Horizontal");
       vsel = (Spinner) findViewById(R.id.spinner1); 
       ArrayAdapter<String> sadpater = newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinner);
       sadpater.setDropDownViewResource(android.R.layout.simple_spinner_item);
       vsel.setAdapter(sadpater);
       ArrayList<String> list ; 
       AppsAdapteradapter=null; 

       vsel.setOnItemSelectedListener(newOnItemSelectedListener() {

           publicvoidonItemSelected(AdapterView<?> parent, View view,
                   int position, long id) {
               // TODO Auto-generated method stub//String item = parent.getItemAtPosition(position).toString();Stringitem= parent.getSelectedItem().toString();

               if(item.equals("All"))
               {
                   list = allid;
               }
               if(item.equals("Vertical"))
               {
                   list = vid;
               }
                if(item.equals("Horizontal"));
               {
                   list = hid));
               }

               if (adapter = null ) {
                   adapter = newAppsAdapter(MainActivity.this, list);
                   gv.setAdapter(adapter);
               }
               adapter.notifyDataSetChanged();
           }

           publicvoidonNothingSelected(AdapterView<?> view) {
               // TODO Auto-generated method stub

           }
       });
   }


   publicclassAppsAdapterextendsBaseAdapter {
       private Context context;
       ArrayList<Integer> dirsTemp = newArrayList<Integer>();
       publicAppsAdapter(Context c,ArrayList<Integer> allid)
       {

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

           context = c;

       }


       //---returns the number of images---publicfinalintgetCount() {

           return dirsTemp.size();


       }

        //---returns the ID of an item--- publicfinal Object getItem(int position) {
           return dirsTemp.get(position);
       }

       publicfinallonggetItemId(int position) {

           return position;
       }

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

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


           return iv;          
       }   
   }




}










}

Post a Comment for "Setting Custom Adapters To Grid Views"