0

我想在单击按钮时将图片从我的画廊添加到最喜欢的类别,并在单击按钮时从最喜欢的类别中删除。我真的不知道该怎么做。我是 android 的新手。我认为第一个当我单击按钮时,我需要获取背景图像的 ID,然后在数据库中发送图片 ID,它可以安全和延迟。我就在这条路上?当我点击她时,按钮切换。(灰色和黄色)有我的源代码 final String MY_LOG = "mylog"; 画廊画廊;图像视图图像视图;

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;

    }

}
4

0 回答 0