0

我有一个关于ImageAdapter填充GridView. 以下是我的代码Activity

public class RestListActivity extends Activity {
    //ImageButton [] buttons;
    RelativeLayout rl;
    int restCount = 0;
    //Resources a;
    int resIndex = 0;
    boolean resFound = false;
    int last;
    int resCount = 0;
    TextView tv;
    ViewFlipper vf;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    Vector<Bitmap> iconBmp;
    Vector<GridView> gvs;
    Vector<ImageAdapter> ias;

    // TODO Auto-generated constructor stub
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.restlist);
        rl = (RelativeLayout) findViewById(R.id.layoutRestPage);

        //a=this.getResources();
        boolean done = true;

        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("Sistemi kullandığınız için teşekkür ederiz!");

        Button nb = (Button) findViewById(R.id.nextButton);
        nb.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                vf.setInAnimation(RestListActivity.this, R.anim.view_right_transition_in);
                vf.setOutAnimation(RestListActivity.this, R.anim.view_right_transition_out);
                vf.showNext();
            }
        });
        Button pb = (Button) findViewById(R.id.prevButton);
        pb.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                vf.setInAnimation(RestListActivity.this, R.anim.view_left_transition_in);
                vf.setOutAnimation(RestListActivity.this, R.anim.view_left_transition_out);
                vf.showPrevious();
            }
        });
        Button backButton = (Button) findViewById(R.id.restButton);
        backButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                Log.i("ACTION", "AD BUTTON CLICKED");
                try {
                    Intent i = new Intent(RestListActivity.this, AVMOrderSystemMain.class);
                    startActivity(i);
                } catch (Exception e) {
                    Log.i("cort", e.toString());
                }
            }
        });
        Button hb = (Button) findViewById(R.id.helpButton);
        hb.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(RestListActivity.this,
                        "\tSiparis vermek için bir restoran reklamına tıklayarak\n" +
                                "ilgili restoranın sayfasına gidebilir, ya da restoranlar\n" +
                                "listesinden bir restoran seçerek, yine ilgili restoranın\n" +
                                "sayfasına gidebilirsiniz.\n" +
                                "\tBu sayfadan menü tercihinizi yaparak detayları belirledikten\n" +
                                "sonra siparişinizi onaylayarak ödemeniz gereken tutarı\n" +
                                "öğrenebilirsiniz\n", Toast.LENGTH_LONG).show();
            }
        });

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        //************************************************************************************
        //************************************************************************************
        //************************************************************************************
        ias = new Vector<ImageAdapter>();
        iconBmp = new Vector<Bitmap>();
        gvs = new Vector<GridView>();
        for (int i = 0; i < StaticParameters.getCompleteData().getRestaurantsVector().size(); i++) {
            System.out.println("_______________________________________-_-_-_-" +
                    StaticParameters.getCompleteData().getRestaurantsVector().get(i).getActiveImage());
            byte[] imgBytes = FileHandler.readFile2(this, "main_icons", StaticParameters.getCompleteData().getRestaurantsVector().get(i).getActiveImage());
            iconBmp.add(BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length));
            System.out.println("///////////////77"+imgBytes.length);
        }
        System.out.println("))))))))))))))" + iconBmp.size());
        //************************************************************************************
        vf = (ViewFlipper) findViewById(R.id.restFlipper);

        int count = iconBmp.size();
        Log.i("THE SIZE", "=" + count);

        for (int y = 0; y < iconBmp.size(); y+=6) {
            System.out.println("0000000000000000___");
            Vector<Bitmap> bmpSubSet = new Vector<Bitmap>();
            for (int o = 0; o < 6 && o + y < iconBmp.size(); o++) {
                System.out.println("INSIDE!!!!");
                bmpSubSet.add(iconBmp.get(o + y));
            }
            ImageAdapter ia = new ImageAdapter(this);
            ia.setBitmaps(bmpSubSet);
            ias.add(ia);
            GridView gv = new GridView(this);

            gv.setId(y);
            gv.setLayoutParams(new GridView.LayoutParams(800, 600));
            gv.setBackgroundColor(Color.WHITE);
            gv.setNumColumns(3);
            gv.setColumnWidth(GridView.AUTO_FIT);
            gv.setVerticalSpacing(5);
            gv.setHorizontalSpacing(5);
            gv.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

            System.out.println("++++++++++++++++4"+ia.restLogos.size());
            gv.setAdapter(ia);
            gv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    try {
                        /*Intent i = new Intent(RestListActivity.this,RestPageActivity.class);
                     RestPageActivity.setText(h);
                     startActivity(i);*/
                    } catch (Exception e) {
                        Log.i("cort", e.toString());
                    }
                }
            });
            vf.addView(gv);
        }

        //////////////////////////////////////////////////////

    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    vf.setInAnimation(RestListActivity.this, R.anim.view_left_transition_in);
                    vf.setOutAnimation(RestListActivity.this, R.anim.view_left_transition_out);
                    vf.showPrevious();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    vf.setInAnimation(RestListActivity.this, R.anim.view_right_transition_in);
                    vf.setOutAnimation(RestListActivity.this, R.anim.view_right_transition_out);
                    vf.showNext();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
            return true;
        else
            return false;
    }
}

以下是ImageAdapter

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList <Integer> restLogoIDs=new ArrayList<Integer>();
    private Resources res;
    Vector <Bitmap>  restLogos=new Vector<Bitmap>();
    public ImageAdapter(Context c) {
        mContext = c;
    }
    public void setResources(Resources r)
    {
        res=r;
    }
    public void setImageIDs(ArrayList <Integer> restLogoIDs)
    {
        this.restLogoIDs=restLogoIDs;
    }
    ////////////////////////////////////////////////////////
    public void setBitmaps(Vector <Bitmap> restLogos)
    {
        this.restLogos=restLogos;
        System.out.println("$$$$$$$$$$$$$$$$$$$$"+restLogos.size());
    }
    ////////////////////////////////////////////////////////
    public int getCount() {
        return restLogoIDs.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("=============xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx---"+position);
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(180, 180));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        //BURASI DEĞİŞECEK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //imageView.setImageResource(restLogoIDs.get(position));
        imageView.setImageBitmap(restLogos.get(position));
        return imageView;
    }
    // references to our images
}

Bitmaps当我在 main 中设置它们之后在适配器中打印数量和大小时Activity,我看到数量Bitmaps是正确的。由于某种原因,getView()没有从适配器调用。我可以看到,GridView但它是空的。我能得到一些帮助吗?

4

1 回答 1

1

适配器的方法getCount返回的0意思是适配器没有元素并且不调用该getView方法:

public int getCount() {
        return restLogoIDs.size(); // you only instantiate restLogoIds, you never add some values to it
}

取而代之的是restLogoIds,您从不分配任何值(其大小为0),而是使用restLogos您实际分配的一些值,以便restLogoIds在设置适配器时它不为空或添加一些值。

于 2012-05-11T05:02:14.387 回答