Dialog Android中的GridView Gallery

时间:2017-05-16 08:55:24

标签: android gridview dialog gallery android-gallery

您好我想使用GridView构建简单的Gallery。我想把它放在Dialog里面。我写了一些代码但是它不起作用。我得到适配器为空的错误。它应该怎么样?现在我的代码看起来像:

GuidePhotoAlbum代码:

    LayoutInflater inflater;
    View v;
    GridView gV;
    public ArrayList<File> list;

    public class GuidePhotoAlbum extends Activity
    {
        @Override public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.guide_photo_album);
            list = imageReader(Environment.getExternalStorageDirectory());
    }
private ArrayList<File> imageReader(File root)
{
    ArrayList<File> a = new ArrayList<>();
    File[] files = root.listFiles();
    for(int i=0;i<files.length;i++)
    {
        if(files[i].isDirectory())
        {
            a.addAll(imageReader(files[i]));
        }
        else
        {
            if (files[i].getName().endsWith(".jpg"))
            {
                a.add(files[i]);
            }
        }
    }
    return a;
}

    public void onClickk(View v)
{
   openDialog2();
}

    public void openDialog2() {


            inflater = this.getLayoutInflater();
            v = inflater.inflate(R.layout.dialog_choice, null);
            gV = new GridView(this);
            gV = (GridView)findViewById(R.id.gridView);
            gV.setAdapter(new GridAdapter());
            final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
            builder2.setTitle("MY GALLERY");
            builder2.setView(v).setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).setNegativeButton("BACK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder2.setCancelable(false);
            builder2.create().show();
        }

GridAdapter:

public class GridAdapter extends BaseAdapter {
    GuidePhotoAlbum guidePhotoAlbum = new GuidePhotoAlbum();
    @Override
    public int getCount() {
        return guidePhotoAlbum.list.size();
    }

    @Override
    public Object getItem(int position) {
        return guidePhotoAlbum.list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = guidePhotoAlbum.getLayoutInflater().inflate(R.layout.my_grid,parent,false);
        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
        iv.setImageURI(Uri.parse(getItem(position).toString()));
    return convertView;
}

}

Xml dialog_choice文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_choice"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">


    <GridView
        android:id="@+id/gridView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:columnWidth="100dp"
        android:drawSelectorOnTop="true"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp"
        android:focusable="true"
        android:clickable="true"/>


</RelativeLayout>

xml my_grid:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageView"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

更新GridAdapter,如下所示:

public class GridAdapter extends BaseAdapter {

    Context mContext;
    ArrayList<File> listFiles;

    public GridAdapter(Context context, ArrayList<File> files) {

        this.mContext = context;
        this.listFiles = files;
    }

    @Override
    public int getCount() {
        return listFiles.size();
    }

    @Override
    public Object getItem(int position) {
        return listFiles.get(position);
    }

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

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

        if(convertView == null)
        {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.my_grid, parent, false);
        }

        ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
        iv.setImageURI(Uri.parse(listFiles.get(position).toString()));

        return convertView;
    }
}

GuidePhotoAlbum活动传递contextlistGridAdapter

    public void openDialog2() {

        inflater = this.getLayoutInflater();

        // Dialog layout
        v = inflater.inflate(R.layout.dialog_choice, null);

        // Get gridView from dialog_choice
        gV = (GridView) v.findViewById(R.id.gridView);

        // GridAdapter (Pass context and files list)
        GridAdapter adapter = new GridAdapter(this, list);

        // Set adapter
        gV.setAdapter(adapter);

        final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        builder2.setTitle("MY GALLERY");
        builder2.setView(v);
        builder2.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("BACK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder2.setCancelable(false);
        builder2.create().show();
    }

希望这会有所帮助〜