我从Google的文档中获得了一个基本的Gridview示例,该示例运行良好。
我也有一些代码,以ArrayList的形式提供结果,并带有Android外部存储中最后创建的图片的位图(授予清单中的权限),并且该代码在其他项目上也能正常工作。
当我将这些概念合并到一个简单的应用程序代码中根本无法正常工作时,活动始终为空,直到应用程序崩溃为止。但是我注意到了一个有趣的事情:有一个for循环,它通过int capacity
定义了迭代次数,但是在Logcat中,我可以看到该循环中的迭代次数超过了声明的int值,基本上这就是应用程序崩溃的原因(outOfBoundsException)。
所以我有两个问题,坐了两个小时……这是我的代码:
MainActivity.java
package com.example.patryk.hellogridview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
PhotosReader.java
package com.example.patryk.hellogridview;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;
import java.util.ArrayList;
public class PhotosReader {
private int capacity = 5;
private ArrayList<Bitmap> photos = new ArrayList<Bitmap>(capacity);
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public void setPhotos(ArrayList<Bitmap> photos) {
this.photos = photos;
}
public PhotosReader() {
}
public ArrayList<Bitmap> getPhotos(Context context) {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE
};
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
cursor.moveToFirst();
for(int i = 1; i<=capacity; i++){
photos.add(BitmapFactory.decodeFile(cursor.getString(1)));
cursor.moveToNext();
Log.d("photos.add", "iteration");
}
return photos;
}
public Bitmap getPhotosBitmap(ArrayList photos, int position){
Bitmap bm = (Bitmap) photos.get(position);
return bm;
}
}
ImageAdapter.java
package com.example.patryk.hellogridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 0;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
PhotosReader PR = new PhotosReader();
imageView.setImageBitmap(PR.getPhotosBitmap(PR.getPhotos(mContext), position));
return imageView;
}
}
最佳答案
ImageAdapter
中的此函数不应返回0。它应返回要在网格中显示的图像数,因此应返回capacity
。 GridView假定要显示0件事
public int getCount() {
return 0;
}
更改为
public int getCount() {
return capacity;
}
实际上,执行此操作的正确方法是使用
PhotoReader
从ImageAdapter
构造函数传递ImageAdapter(context,photoReader)
对象,然后在photoReader.getCapacity()
函数中使用getCount()