1

在我的主要活动中,我有一个包含三个类别(全部、水平和垂直)的微调器,在其下方有一个 gridView。我在资源文件夹中有一些用于垂直和水平模式的图像。我需要根据微调器上的选定项目将图像设置为 gridView。我可以将一个类别设置为 gridView(例如所有类别)。选择其他类别时,gridView 没有更新。为了更新 gridView 我应该怎么做?请帮帮我?

我尝试通过在我的活动中放置三个 gridView 并将它们的可见性设置为消失,并在设置适配器时使其可见(我使用了三个单独的适配器但不工作。

我的代码如下。

     public class MainActivity extends Activity {
    Spinner vsel;
    ArrayList<String> vimage = new ArrayList<String>();
    ArrayList<String> all = new ArrayList<String>();
    ArrayList<String> himage = new ArrayList<String>();
    ArrayList<Integer> allid = new ArrayList<Integer>();   
    ArrayList<Integer> hid = new ArrayList<Integer>();
    ArrayList<Integer> vid = new ArrayList<Integer>();  
    ArrayList<Integer> tot = new ArrayList<Integer>();  
    ArrayList<String> spinner = new ArrayList<String>();
    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-> " + 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 = new ArrayAdapter<String>(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<?> 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<?> view) {
                // TODO Auto-generated method stub

            }
        });
    }


    public class AppsAdapter extends BaseAdapter {
        private Context context;
        ArrayList<Integer> dirsTemp = new ArrayList<Integer>();
        public AppsAdapter(Context c,ArrayList<Integer> 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->"+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<Integer> dirsTemp1 = new ArrayList<Integer>();
        public AppsAdapter1(Context c,ArrayList<Integer>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<Integer> dirsTemp2 = new ArrayList<Integer>();
        public AppsAdapter3(Context c,ArrayList<Integer>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;         
        }   
    }


}
4

1 回答 1

1

您不需要三个 gridViews 和三个适配器。一个就够了。从 spinner 更改类别时,刷新列表allid, vid, hid(在 Adapter 中用于显示 gridview),然后notifyDataSetChanged()在适配器上。

编辑:试试这个:

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;


    public class ddsds extends Activity {
   Spinner vsel;
   ArrayList<String> vimage = new ArrayList<String>();
   ArrayList<String> all = new ArrayList<String>();
   ArrayList<String> himage = new ArrayList<String>();
   ArrayList<Integer> allid = new ArrayList<Integer>();   
   ArrayList<Integer> hid = new ArrayList<Integer>();
   ArrayList<Integer> vid = new ArrayList<Integer>();  
   ArrayList<Integer> tot = new ArrayList<Integer>();  
   ArrayList<String> spinner = new ArrayList<String>();
   GridView gv;

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

       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-> " + 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 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinner);
       sadpater.setDropDownViewResource(android.R.layout.simple_spinner_item);
       vsel.setAdapter(sadpater);
       ArrayList<String> list ; 
       AppsAdapter adapter = null; 

       vsel.setOnItemSelectedListener(new OnItemSelectedListener() {

           public void onItemSelected(AdapterView<?> 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"))
               {
                   list = allid;
               }
               if(item.equals("Vertical"))
               {
                   list = vid;
               }
                if(item.equals("Horizontal"));
               {
                   list = hid));
               }

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

           public void onNothingSelected(AdapterView<?> view) {
               // TODO Auto-generated method stub

           }
       });
   }


   public class AppsAdapter extends BaseAdapter {
       private Context context;
       ArrayList<Integer> dirsTemp = new ArrayList<Integer>();
       public AppsAdapter(Context c,ArrayList<Integer> 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->"+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;          
       }   
   }




}










}
于 2012-10-18T10:53:48.647 回答