如何使gridView可点击

时间:2014-09-22 10:35:39

标签: android image gridview directory

我正在研究gridView,目的是从SdCard中的特定文件夹中获取图像并将它们填充到GridView中。我已经在下面写了这个代码,但是它不可点击,我可以获取图像并将它们上传到gridview,但是当我点击图像时,它无法正常工作。请看看,告诉我我在这里做了什么错。

public class MainActivity extends Activity {

    public class ImageAdapter extends BaseAdapter {

        private Context mContext;
        ArrayList <String> itemList = new ArrayList <String> ();

        public ImageAdapter(Context c) {
            mContext = c;
        }

        void add(String path) {
            itemList.add(path);
        }

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

        @Override
        public Object getItem(int arg0) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) { // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView)convertView;
            }

            Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220, 220);

            imageView.setImageBitmap(bm);
            return imageView;
        }

        public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {

            Bitmap bm = null;
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(path, options);

            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            bm = BitmapFactory.decodeFile(path, options);

            return bm;
        }

        public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;

            if (height > reqHeight || width > reqWidth) {
                if (width > height) {
                    inSampleSize = Math.round((float)height / (float)reqHeight);
                } else {
                    inSampleSize = Math.round((float)width / (float)reqWidth);
                }
            }

            return inSampleSize;
        }
    }

    ImageAdapter myImageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_main);

        GridView gridview = (GridView)findViewById(R.id.gridview);
        myImageAdapter = new ImageAdapter(this);
        gridview.setAdapter(myImageAdapter);

        String ExternalStorageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath();

        String targetPath = ExternalStorageDirectoryPath + "/camtest/";

        Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
        File targetDirector = new File(targetPath);

        File[]files = targetDirector.listFiles();
        for (File file : files) {
            myImageAdapter.add(file.getAbsolutePath());
        }

        OnItemClickListener myOnItemClickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView <?> parent, View view, int position,
                long id) {
                String prompt = (String)parent.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_LONG).show();

            }
        };

    }
}

4 个答案:

答案 0 :(得分:0)

 gridview.setOnItemClickListener(myOnItemClickListener);

 gridView.setOnItemClickListener(new OnItemClickListener()
 {
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id)
  {
   //try this code
  }
});

答案 1 :(得分:0)

更改此代码。

gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
    //try this code
}
});

答案 2 :(得分:0)

您已创建了侦听器myOnItemClickListener,但未将其注册到gridview.

这是你的listerner:

OnItemClickListener myOnItemClickListener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView <?> parent, View view, int position,
                long id) {
                String prompt = (String)parent.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), prompt, Toast.LENGTH_LONG).show();

            }
        }; 

将您的myOnItemClickListener听众设为gridview.

gridview.setOnItemClickListener(myOnItemClickListener);

答案 3 :(得分:0)

尝试以下代码

YOUR_GRID_VIEW.setOnItemClickListener(new OnItemClickListener()
{
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id)
     {

     }
});