如何从网格视图中选择多个图像并将其显示给另一个活动?

时间:2015-12-15 13:20:55

标签: android

MainActivity看起来像这样我正在加载对话框内的网格视图。图像显示正确的网格视图我能够将单个图像传递给另一个活动但是无法将选定的多个图像从网格视图传递到另一项活动。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();
    setContentView(R.layout.activity_main);


    showDialog(CATEGORY_ID);
}
protected Dialog onCreateDialog(int id) {

    switch(id) {

        case CATEGORY_ID:

            AlertDialog.Builder builder;
            final Context mContext = this;
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.gridviewdialog,(ViewGroup) findViewById(R.id.layout_root));
            gridview = (GridView)layout.findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));
            gridview.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
            gridview.setMultiChoiceModeListener(new MultiChoiceModeListener());
            System.out.println("selecting Ids " + gridview.getCheckedItemIds());
            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                                        final int position, long id) {

                    System.out.println("inside gridview click");

                    switch (position) {

                        case 0:
                            System.out.println("Case 0");
                            Confirm.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent = new Intent(mContext, SelectedPosterImage.class);
                                    intent.putExtra("imageID", mThumbIds[position]);
                                    mContext.startActivity(intent);
                                }
                            });
                            break;
                        case 1:
                            System.out.println("Case 1");
                            Confirm.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent1 = new Intent(mContext, SelectedPosterImage.class);
                                    intent1.putExtra("imageID", mThumbIds[position]);
                                    mContext.startActivity(intent1);
                                }
                            });

                            break;
                        case 2:
                            System.out.println("Case 2");
                            Confirm.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent2 = new Intent(mContext, SelectedPosterImage.class);
                                    intent2.putExtra("imageID", mThumbIds[position]);
                                    mContext.startActivity(intent2);
                                }
                            });

                            break;
                        case 3:
                            System.out.println("Case 3");
                            Confirm.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent3 = new Intent(mContext, SelectedPosterImage.class);
                                    intent3.putExtra("imageID", mThumbIds[position]);
                                    mContext.startActivity(intent3);
                                }
                            });

                            break;
                    }


                }
            });

            Confirm=(Button)layout.findViewById(R.id.Okbutton);
            System.out.println("Confirm id" + Confirm);
            ImageView close = (ImageView) layout.findViewById(R.id.close);
            close.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){
                    dialog.dismiss();
                }
            });

            builder = new AlertDialog.Builder(mContext);
            builder.setView(layout);
            dialog = builder.create();
            break;
        default:
            dialog = null;
    }
    return dialog;
}


public class CheckableLayout extends FrameLayout implements Checkable {
    private boolean mChecked;

    public CheckableLayout(Context context) {
        super(context);
    }

    @SuppressWarnings("deprecation")
    public void setChecked(boolean checked) {
        mChecked = checked;
        setBackgroundDrawable(checked ? getResources().getDrawable(
                R.color.lime_A200) : null);
    }

    public boolean isChecked() {
        return mChecked;
    }

    public void toggle() {
        setChecked(!mChecked);
    }

}
public class MultiChoiceModeListener implements
        GridView.MultiChoiceModeListener {
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTitle("Select Items");
        mode.setSubtitle("One item selected");
        return true;
    }

    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return true;
    }

    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        return true;
    }

    public void onDestroyActionMode(ActionMode mode) {
    }

    public void onItemCheckedStateChanged(ActionMode mode, final int position,
                                          long id, boolean checked) {
       final int selectCount = gridview.getCheckedItemCount();
        System.out.println("selectedCount "+selectCount);
         final int[] selectIds = (int[]) gridview.getSelectedItem();
        System.out.println("selectIds " + selectIds);



        switch (selectCount) {
            case 1:
                mode.setSubtitle("One item selected");

                break;
            default:
                mode.setSubtitle("" + selectCount + " items selected");
                break;
        }

    }

}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    public ImageAdapter(Context c) {
        mInflater = LayoutInflater.from(c);
        mContext = c;
    }
    public int getCount() {
        return mThumbIds.length;
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    // create a new ImageView for each item referenced by the
    public View getView(final int imagPosition, View convertView, ViewGroup parent) {
        CheckableLayout l;
        ImageView i;
        if (convertView == null) {

            i = new ImageView(MainActivity.this);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new ViewGroup.LayoutParams(300, 400));
            l = new CheckableLayout(MainActivity.this);
            l.setLayoutParams(new GridView.LayoutParams(300, 400));
            l.addView(i);


       /*     i.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, SelectedPosterImage.class);
                    intent.putExtra("imageID", mThumbIds[imagPosition]);
                    mContext.startActivity(intent);
                }
            });*/



        } else {
            l = (CheckableLayout) convertView;
            i = (ImageView) l.getChildAt(0);
        }


        i.setImageResource(mThumbIds[imagPosition]);

        return l;




    }

    // references to our images


}

}

0 个答案:

没有答案