0

我的应用程序打算根据两个选定的单选按钮之一选择图像切换器内容。单击单选按钮时,应用程序没有选择所需的选项,最后应用程序崩溃了。

最大的问题是我想根据选中的单选按钮释放保存图像的数组以供切换器使用。

此外,还有两个带有图像引用的数组。我想删除画廊内的所有数组并添加一个参考。这是我的代码。

public void onCheckedChanged(RadioGroup group, int checkedId) {

    // TODO Auto-generated method stub

    //check which id is selected

    radioCheckedId= checkedId;


    //radio button checked

    switch(radioCheckedId){

        case R.id.radBtnAccident:

//删除所有数组 if (pics.length>0){

                for (int arr=0;arr<pics.length;arr++){

                    pics[arr]=null;

                }
            }

            //populates array

            pics = new Integer[]{ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
                                  R.drawable.e,R.drawable.exit, R.drawable.plan, R.drawable.icon, R.drawable.plan };

            chooseImgArray(pics);

        break;

        case R.id.radBtnOthers:

            //populates other array

            pics = new Integer[]{R.drawable.exit, R.drawable.plan,

R.drawable.icon,R.drawable.plan,R.drawable.c};

            chooseImgArray(pics);
        break;

    default:

    break;


    }

}

public void chooseImgArray(final Integer[] array){


    iSwitcher = (ImageSwitcher) findViewById(R.id.ImgSwith);

    iSwitcher.setFactory(this);
    iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
    iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));

    gallery = (Gallery) findViewById(R.id.galImage);

    gallery.setAdapter(new ImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int count,long arg3) {
            iSwitcher.setImageResource(array[count]);

        }

    });




}
4

1 回答 1

0
我将 ImageSwitcher 的示例代码与 Gallery Check 放在了一起。
它对你有用。
public class TestActivity extends Activity implements ViewFactory,
        OnItemSelectedListener {

    private Gallery gallery;
    private ImageSwitcher iSwitcher;
    private RadioGroup rdgSelection;
    private Boolean flag = true;

    private Integer[] Array1 = { R.drawable.arrow, R.drawable.arrow1,
            R.drawable.arrow2, R.drawable.arrow3 };
    private Integer[] Array2 = { R.drawable.camera, R.drawable.camera1,
            R.drawable.camera2, R.drawable.camera3 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gallery = (Gallery) findViewById(R.id.gallery);
        iSwitcher = (ImageSwitcher) findViewById(R.id.imgSwitcher);
        rdgSelection = (RadioGroup) findViewById(R.id.rdgSelection);

        iSwitcher.setFactory(this);
        rdgSelection.check(rdgSelection.getChildAt(
                rdgSelection.getChildCount() - 1).getId());

        if (rdgSelection.getCheckedRadioButtonId() == R.id.rdb1) {
            flag = true;
            gallery.setAdapter(new ImageAdapter(TestActivity.this));
        } else {
            flag = false;
            gallery.setAdapter(new ImageAdapter(TestActivity.this));
        }
        rdgSelection.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                gallery.setAdapter(new ImageAdapter(TestActivity.this));
                if (rdgSelection.getCheckedRadioButtonId() == R.id.rdb1)
                    flag = true;
                else
                    flag = false;
            }
        });
        gallery.setOnItemSelectedListener(this);

    }

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            if (flag)
                return Array1.length;
            else
                return Array2.length;
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);
            if (flag)
                i.setImageResource(Array1[position]);
            else
                i.setImageResource(Array2[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            return i;
        }

        private Context mContext;

    }

    @Override
    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return i;
    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View v, int position,
            long id) {
        if (flag)
            iSwitcher.setImageResource(Array1[position]);
        else
            iSwitcher.setImageResource(Array2[position]);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }

}
于 2012-05-24T06:04:02.043 回答