如何在按钮点击时获取图像ID并在按钮上单击关闭图像ID

时间:2013-05-09 11:55:33

标签: android android-button android-image

当我点击按钮时,我想从我的画廊添加一张图片到最喜欢的类别,当我点击按钮时减去喜欢的类别。我真的不知道如何做到这一点。我是android.I的新手我想第一个我需要获取背景图像的ID,当我点击按钮时,在数据库中发送图片ID后,它可以安全和减少。我是这样的吗?当我点击她时按钮开关。 (灰色和黄色) 有我的代码     final String MY_LOG =“mylog”;     画廊画廊;     ImageView imageView;

Button sportButton;

int[] imgID = { R.drawable.sport_0, R.drawable.sport_1, R.drawable.sport_2,
        R.drawable.sport_3, R.drawable.sport_4, R.drawable.sport_5,
        R.drawable.sport_6, R.drawable.sport_7, R.drawable.sport_8,
        R.drawable.sport_9, };

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sport);
    imageView = (ImageView) findViewById(R.id.ImageSportView);
    imageView.setImageResource(imgID[0]);

    gallery = (Gallery) findViewById(R.id.SportGallery);
    gallery.setAdapter(new AddImgAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> perent, View view,
                int position, long id) {
            imageView.setImageResource(imgID[position]);
            Log.d(MY_LOG, "img Id"+position);

        }
    });

}

public void onClickButton(View button) {

    if (button.isSelected()) {
        button.setSelected(false);
    } else {
        button.setSelected(true);
        gallery.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> perent, View view,
                    int position, long id) {

                Log.d(MY_LOG, "img Id"+position);
            }
        });
    }
    Log.d(MY_LOG, "on button sportClick");
}

public class AddImgAdapter extends BaseAdapter {
    int GalItemBg;
    Context count;

    public AddImgAdapter(Context c) {
        count = c;
        TypedArray typeArray = obtainStyledAttributes(R.styleable.GalleryTheme);
        GalItemBg = typeArray.getResourceId(
                R.styleable.GalleryTheme_android_galleryItemBackground, 0);
        typeArray.recycle();
    }

    @Override
    public int getCount() {

        return imgID.length;
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {

        ImageView newImageView = new ImageView(count);
        newImageView.setImageResource(imgID[position]);
        newImageView.setLayoutParams(new Gallery.LayoutParams(130, 100));
        newImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        newImageView.setBackgroundResource(GalItemBg);

        return newImageView;

    }

}

0 个答案:

没有答案