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メインにセットした後、アダプターに数とサイズを印刷すると、数が正しいActivityことがわかります。Bitmaps何らかの理由でgetView()、アダプタから呼び出されていません。見えますGridViewが空です。これについて助けてもらえますか?

4

1 に答える 1

1

アダプタのメソッドは、アダプタの場合、要素がなく、メソッドを呼び出さないという意味をgetCount返します。0getView

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 に答える