如何将图库中的图像添加到列表视图中

时间:2012-08-21 10:46:39

标签: android android-listview android-imageview android-gallery

我想在图库中添加一张图片,当我按下创建播放器时,我希望将该图片添加到列表视图中的播放器名称旁边。

现在,我的代码从drawables获取图片,这些图片在一个类型化的数组中被访问。

如何更改我的代码以实现它以从图库中获取图片并将其显示在列表视图中?

任何帮助将受到高度赞赏!三江源

我的自定义适配器的代码是:

 public class PlayerImageAdapter extends ArrayAdapter<Player> {

        private LayoutInflater mInflater;

            private ArrayList<Player> players;
            private TypedArray mIcons;

            private int mViewResourceId;

            public PlayerImageAdapter(Context ctx, int viewResourceId,
                    ArrayList<Player> players, TypedArray icons) {
                super(ctx, viewResourceId, players);

                mInflater = (LayoutInflater)ctx.getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);

                this.players = players;
                mIcons = icons;

                mViewResourceId = viewResourceId;
            }


            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = mInflater.inflate(mViewResourceId, null);

                ImageView iv = (ImageView)convertView.findViewById(R.id.playerPic);
                iv.setImageDrawable(mIcons.getDrawable(position));

                TextView tv = (TextView)convertView.findViewById(R.id.playerName);
                tv.setText(this.players.get(position).toString());

                return convertView;
            }
        }


        //and my code that calls it:


    TypedArray icons = res.obtainTypedArray(R.array.player_pics);

                adapter = new PlayerImageAdapter(ctx, R.layout.player_customlist,
                        dataStore.getPlayers(), icons);


        My code that access the gallery is:

         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);

                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };

                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();

                    ImageView imageView = (ImageView) findViewById(R.id.imagePlayer);
                    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                }


            }

0 个答案:

没有答案