我有一个gridView,它显示默认的addImage按钮作为最后一项。当gridView中显示某些内容时它很有效,但是如果我将空列表传递给适配器,则默认图像不会显示。为什么是这样?
适配器代码:
public class GridImageAdapter extends ArrayAdapter<String> {
private Context mContext;
private LayoutInflater mInflater;
private int layoutResource;
private ArrayList<String> imageURLs;
private static final int NUM_GRID_COLUMNS = 3;
public GridImageAdapterSettingFragment(Context context, int layoutResource, ArrayList<String> imageURLs) {
super(context, layoutResource, imageURLs);
this.mContext = context;
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.layoutResource = layoutResource;
this.imageURLs = imageURLs;
}
private static class ViewHolder{
SquareImageView image;
}
@Override
public int getCount() {
return imageURLs.size()+1;
}
public void removeItem(int position){
imageURLs.remove(position);
notifyDataSetChanged();
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final GridImageAdapterSettingFragment.ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(layoutResource, parent, false);
holder = new GridImageAdapterSettingFragment.ViewHolder();
holder.image = convertView.findViewById(R.id.gridImageView);
convertView.setTag(holder);
} else {
holder = (GridImageAdapterSettingFragment.ViewHolder) convertView.getTag();
}
if(position==imageURLs.size()){
holder.image.setImageResource(R.drawable.ic_add_image);
} else {
String imageURL = getItem(position);
final ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(imageURL, holder.image, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}
return convertView;
}
在片段中,我正在查询Firebase数据库,以便将图像添加到数据列表中。
private void setupGridView(){
final List<Photo> photos = new ArrayList<Photo>();
final ArrayList<String> imgUrls = new ArrayList<String>();
myRef = FirebaseDatabase.getInstance().getReference();
Query query = myRef.child(getString(R.string.dbname_user_photos))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
//Add images into the list and call adapter to set up GridView.
} else {
//problem here!
GridImageAdapter adapter = new GridImageAdapter(getActivity(), R.layout.layout_grid_imageview, imgUrls);
Log.d(TAG, "onDataChange: emptylist is " + imgUrls);
mGridView.setAdapter(adapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "onCancelled: query cancelled.");
}
});
}
我的gridView项目视图布局文件和片段布局文件中的GridView如下:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.aaron.xxxxxxxx.Utils.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridImageView"
android:scaleType="centerCrop"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/photo_tv"
android:numColumns="3"
android:layout_marginLeft="10dp"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="none"
android:gravity="center"
android:layout_marginTop="3dp"
android:id="@+id/gridView"/>
答案 0 :(得分:0)
而不是使用此代码,
if(position==imageURLs.size()){
holder.image.setImageResource(R.drawable.ic_add_image);
} else {
....
}
只需将默认图片添加到该列表(您将传递给gridview)作为最后一项。
答案 1 :(得分:0)
试试这个
<com.aaron.xxxxxxxx.Utils.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gridImageView"
android:src="@drawable/ic_add_image"
android:scaleType="centerCrop"/>