如何使用Listview android将Selected Item转换为新类

时间:2015-09-28 10:48:11

标签: android listview android-listview

这是我的主要活动类

所以我有活动从图库中选择图片并在GridView中显示,可以选择,如何在使用ListView开始我的新意图时获取我选择的图像和在新班级中查看?

所以这是我的安卓代码:

GridView mGridView;
ImageButton mIb;
private String[] arrPath;
private boolean[] thumbnailselection;
private int ids[];
private int count;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    loadApps();
    setContentView(R.layout.activity_main);

    mGridView = (GridView) findViewById(R.id.grid);
    mIb = (ImageButton) findViewById(R.id.done);
    mGridView.setAdapter(new ImageAdapter());

    mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
    mGridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
}

private void loadApps() {
    String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID};
    String orderBy = MediaStore.Images.Media._ID;
    Cursor imageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
    int image_column_index = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
    this.count = imageCursor.getCount();
    this.arrPath = new String[this.count];
    ids = new int[count];
    this.thumbnailselection = new boolean[this.count];
    for (int i = 0; i < this.count; i++){
        imageCursor.moveToPosition(i);
        ids[i] = imageCursor.getInt(image_column_index);
        int dataColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
        arrPath[i] = imageCursor.getString(dataColumnIndex);
    }
    imageCursor.close();
}

public void onBackPressed(){
    finish();
    super.onBackPressed();
}

public class ImageAdapter extends BaseAdapter{

    CheckableLayout l;
    ImageView i;

    @Override
    public int getCount() {
        return arrPath.length;
    }

    @Override
    public Object getItem(int position) {
        return arrPath[position];
    }

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

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

        if (convertView == null){
            i = new ImageView(MainActivity.this);
            i.setScaleType(ImageView.ScaleType.FIT_CENTER);
            i.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
            l = new CheckableLayout(MainActivity.this);
            l.setLayoutParams(new GridView.LayoutParams(
                    GridView.LayoutParams.WRAP_CONTENT,
                    GridView.LayoutParams.WRAP_CONTENT));
            l.addView(i);
        } else {
            l = (CheckableLayout) convertView;
            i = (ImageView) l.getChildAt(0);
        }

        try {
            setBitmap(i, ids[position]);
        } catch (Throwable e){

        }
        return l;
    }
}

private void setBitmap(final ImageView iv, final int id) {

    new AsyncTask<Void, Void, Bitmap> () {
        Bitmap myBitmap;
        @Override
        protected Bitmap doInBackground(Void... params) {
            return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
        }

        protected void onPostExecute(Bitmap result){
            super.onPostExecute(result);
            //iv.setImageBitmap(result)
            setMyBitmap(result);
        }
        public void setMyBitmap(Bitmap bitmap) {
            if (this.myBitmap != null) {
                this.myBitmap.recycle();
            }
            this.myBitmap = bitmap;
            iv.getLayoutParams().height = 300;
            iv.getLayoutParams().width = 300;
            iv.setImageBitmap(myBitmap);
        }

    }.execute();
}



private class CheckableLayout extends FrameLayout implements Checkable{

    private boolean mChecked;

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

    @Override
    public void setChecked(boolean checked) {
        mChecked = checked;
        setForeground(checked ? getResources().getDrawable(R.drawable.blue) : null);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

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

public class MultiChoiceModeListener implements
        GridView.MultiChoiceModeListener {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
        final int selectCount = mGridView.getCheckedItemCount();
        switch (selectCount){
            case 1:
                mode.setSubtitle("1 Item Selected" + " of 10");
                break;
            default:
                mode.setSubtitle(" " + selectCount + " items selected" + " of 10");

                    mIb.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            if (selectCount >= 10) {
                                Intent intent = new Intent(MainActivity.this, Show.class);
                                startActivity(intent);
                            }
                            else if (selectCount < 10) {
                                Toast.makeText(getApplicationContext(), "Please select at least 10 images", Toast.LENGTH_LONG).show();
                            }
                        }
                    });


        }




    }


    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        mode.setTitle("Select Items");
        mode.setSubtitle("One Item Selected");
        return true;
    }

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

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

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        Toast.makeText(getApplicationContext(), "text", 1);
    }

}

}

1 个答案:

答案 0 :(得分:0)

您的意思是想要查看所选图像的预览?