0

Android レイアウトのグリッド ビューを実行していますが、グリッド ビューにインテントを設定する方法がわかりません。別のアクティビティを開始する意図を設定する方法はありますか? 私はすでに数日間試していますが、それを行う方法がわかりません。

public class PregnancyStages extends Activity implements OnItemClickListener {

private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private ImageAdapter imageAdapter;

// Some items to add to the GRID
private static final String[] CONTENT = new String[] { "Pregnancy Stages", "Complications", "Diet And Fitness", "Myths And Facts",
        "FAQ's", "Helplines" };
private static final int[] ICONS = new int[] { R.drawable.baby1, R.drawable.baby2,
        R.drawable.baby3, R.drawable.baby4, R.drawable.baby5, R.drawable.baby6 };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // get the photo size and spacing
    mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
    mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

    // initialize image adapter
    imageAdapter = new ImageAdapter();

    photoGrid = (GridView) findViewById(R.id.albumGrid);

    // set image adapter to the GridView
    photoGrid.setAdapter(imageAdapter);

    // get the view tree observer of the grid and set the height and numcols dynamically
    photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (imageAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                    imageAdapter.setNumColumns(numColumns);
                    imageAdapter.setItemHeight(columnWidth);

                }
            }
        }
    });
}

// ///////// ImageAdapter class /////////////////
public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private int mItemHeight = 0;
    private int mNumColumns = 0;
    private RelativeLayout.LayoutParams mImageViewLayoutParams;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }

    public int getCount() {
        return CONTENT.length;
    }

    // set numcols
    public void setNumColumns(int numColumns) {
        mNumColumns = numColumns;
    }

    public int getNumColumns() {
        return mNumColumns;
    }

    // set photo item height
    public void setItemHeight(int height) {
        if (height == mItemHeight) {
            return;
        }
        mItemHeight = height;
        mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
        notifyDataSetChanged();
    }

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

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

    public View getView(final int position, View view, ViewGroup parent) {

        if (view == null)
            view = mInflater.inflate(R.layout.photo_item, null);

        ImageView cover = (ImageView) view.findViewById(R.id.cover);
        TextView title = (TextView) view.findViewById(R.id.title);

        cover.setLayoutParams(mImageViewLayoutParams);

        // Check the height matches our calculated column width
        if (cover.getLayoutParams().height != mItemHeight) {
            cover.setLayoutParams(mImageViewLayoutParams);
        }

        cover.setImageResource(ICONS[position % ICONS.length]);
        title.setText(CONTENT[position % CONTENT.length]);

        return view;
    }
}

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // TODO Auto-generated method stub

    Intent myIntent = null;
    if(position == 0){
        myIntent = new Intent(v.getContext(), PregnancyStagesGrid1.class);
    }
    if(position == 1){
        myIntent = new Intent(v.getContext(), PregnancyStagesGrid2.class);
    }
    startActivity(myIntent);


}}

このソースコードをどこに置くことができるか知っている人はいますか?

GridView gridview = (GridView) findViewById (R.id.albumGrid);
    gridview.setAdapter (new ImageAdapter()); 

    gridview.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            // TODO Auto-generated method stub

            Intent myIntent = null;
            if(position == 0){
                myIntent = new Intent(v.getContext(), PregnancyStagesGrid1.class);
            }
            if(position == 1){
                myIntent = new Intent(v.getContext(), PregnancyStagesGrid2.class);
            }
            startActivity(myIntent);
        }
    });
}
4

1 に答える 1

0

setOnItemClickListener リスナーを追加するのを忘れました

photoGrid.setOnItemClickListener(this);
于 2013-09-18T17:20:25.580 に答える