0

我正在尝试将在复选框的 onCheck 方法期间制作的位图发送到 gridView,然后该位图将显示在 gridView 中。我正在尝试将位图转换为字节数组,然后将该数组传递给一个意图,然后我将在我的其他活动中设置位图(如这里),但我无法让意图工作。

这是我的复选框的 onClick 方法:

        addCheckbox
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    if (addCheckbox.isChecked()) {
                        System.out.println("Checked");
                        PackageManager pm = mContext.getPackageManager();
                        Drawable icon = null;
                        try {
                            icon = pm
                                    .getApplicationIcon(entry.packageName);
                        } catch (NameNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Drawable default_icon = pm.getDefaultActivityIcon();
                        if (icon instanceof BitmapDrawable
                                && default_icon instanceof BitmapDrawable) {
                            BitmapDrawable icon_bd = (BitmapDrawable) icon;
                            Bitmap icon_b = icon_bd.getBitmap();
                            BitmapDrawable default_bd = (BitmapDrawable) pm
                                    .getDefaultActivityIcon();
                            Bitmap default_b = default_bd.getBitmap();
                            if (icon_b == default_b) {
                                // It's the default icon

                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                default_b.compress(Bitmap.CompressFormat.PNG, 100, stream);
                                byte[] byteArray = stream.toByteArray();

                                Intent intent = new Intent(this, GridView.class);
                                intent.putExtra("picture", byteArray);
                                startActivity(intent);
                            }
                        }
                    } else {
                        System.out.println("Un-Checked");
                    }

                }

然后我试图在我的 GridView 类中设置它:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class GridView extends Activity implements OnItemLongClickListener, OnDragListener{

ArrayList<Integer> drawables = new ArrayList<Integer>();

private BaseAdapter adapter;
private int draggedIndex = -1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drag_and_drop_app);
    drawables = new ArrayList<Integer>();
    drawables.add(R.drawable.pattern1);
    drawables.add(R.drawable.pattern2);
    android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.grid_view);
    gridView.setOnItemLongClickListener(this);
    gridView.setAdapter(adapter = new BaseAdapter() {

        @Override
        // Get a View that displays the data at the specified position in
        // the data set.
        public View getView(int position, View convertView,
                ViewGroup gridView) {
            // try to reuse the views.
            ImageView view = (ImageView) convertView;
            // if convert view is null then create a new instance else reuse
            // it
            if (view == null) {
                view = new ImageView(GridView.this);
            }
            Bundle extras = getIntent().getExtras();
            byte[] byteArray = extras.getByteArray("picture");

            Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

            view.setImageBitmap(default_b);
            view.setImageResource(drawables.get(position));
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
            view.setTag(String.valueOf(position));
            return view;
        }

        @Override
        // Get the row id associated with the specified position in the
        // list.
        public long getItemId(int position) {
            return position;
        }

        @Override
        // Get the data item associated with the specified position in the
        // data set.
        public Object getItem(int position) {
            return drawables.get(position);
        }

        @Override
        // How many items are in the data set represented by this Adapter.
        public int getCount() {
            return drawables.size();
        }
    });
}

我会以正确的方式解决这个问题吗?如果是这样,我该如何解决使用 onCheck 方法的意图?如果没有,我该怎么做才能改变它?我已经查看了 stackoverflow 一段时间,这确实是我见过的唯一解决方案(转换为字节数组和其他东西),但我从未见过它带有复选框。

4

1 回答 1

0

我最终不得不从一个复合按钮更改为一个简单的 onClickListener。它仍然可以正常工作,并且可以更好地支持意图。

于 2013-11-01T22:36:21.320 回答