我有gridview,我将图像项从存储设备填充到firebase数据库中。当我将图像上传到firebase时,图像存储在我的FragmentProfile.java中的gridview中,我使用Picasso图像加载器加载图像,问题是当我清除应用程序的缓存或多次打开所有图像项更改的片段,并显示我上传的最后一张图像。
[] [1]查看图片
FragmentProfile.java
package com.example.baraka.audilite.Profile;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toolbar;
import com.example.baraka.audilite.Login.Register_Continue;
import com.example.baraka.audilite.R;
import com.example.baraka.audilite.Utilis.FirebaseMethods;
import com.example.baraka.audilite.Utilis.GridImageAdapter;
import com.example.baraka.audilite.Utilis.GridViewScrollable;
import com.example.baraka.audilite.Utilis.GridViewWithHeader;
import com.example.baraka.audilite.Utilis.UniversalImageLoader;
import com.example.baraka.audilite.model.Photo;
import com.example.baraka.audilite.model.User;
import com.example.baraka.audilite.model.UserSettings;
import com.example.baraka.audilite.model.UsersAccountSettings;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.Collections;
import de.hdodenhof.circleimageview.CircleImageView;
import in.srain.cube.views.GridViewWithHeaderAndFooter;
public class FragmentProfile extends Fragment {
private CircleImageView mprofilephoto;
private TextView mfullname,username,posts,followers,following,description,website,profilename;
private GridViewWithHeader gridView;
private Toolbar toolbar;
private ImageView profilemenu,imgback,backarrow;
private Button editprofile;
private ProgressBar progressbar;
private AppBarLayout appbar;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthlisterner;
private FirebaseDatabase mfirebasedatabase;
private DatabaseReference mref;
private FirebaseMethods firebaseMethods;
private int mfollowersCount=0;
private int mfollowingCount=0;
private int postsCount=0;
public interface onGridSelectedListerner{
void onGridImageSelected(Photo photo,int activityNumber);
}
onGridSelectedListerner monGridSelectedListerner;
static final int NUM_GRID_COLUMS=3;
public static final int ACTIVITY_NUM=2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
ProfileActivity.fragment_one=true;
gridView = (GridViewWithHeader) view.findViewById(R.id.gridview);
profilemenu = (ImageView) view.findViewById(R.id.profilemenu);
mfullname = (TextView) view.findViewById(R.id.full);
username = (TextView) view.findViewById(R.id.username);
posts = (TextView) view.findViewById(R.id.postcount);
followers = (TextView) view.findViewById(R.id.followerscount);
following = (TextView) view.findViewById(R.id.followingcount);
profilename = (TextView) view.findViewById(R.id.profilename);
description = (TextView) view.findViewById(R.id.mbio);
website = (TextView) view.findViewById(R.id.mwebsite);
mprofilephoto = (CircleImageView) view.findViewById(R.id.profilephoto);
editprofile=(Button) view.findViewById(R.id.editprof);
progressbar=(ProgressBar) view.findViewById(R.id.mprogressbar);
imgback=(ImageView) view.findViewById(R.id.imgbackground);
appbar=(AppBarLayout) view.findViewById(R.id.container);
backarrow=(ImageView) view.findViewById(R.id.backarrow);
firebaseMethods=new FirebaseMethods(getActivity());
progressbar.setVisibility(View.VISIBLE);
profilemenu.setEnabled(false);
appbar.setVisibility(View.GONE);
setuptoolbar();
setupfirebaseAuth();
setupgridview();
getFollowersCount();
getFollowingCount();
getPostsCount();
backarrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().finish();
}
});
editprofile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(getActivity(),AccountSettingsActivity.class);
intent.putExtra(getString(R.string.calling_activity),getString(R.string.profile_activity));
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
});
return view;
}
@Override
public void onAttach(Context context) {
try {
monGridSelectedListerner=(onGridSelectedListerner) getActivity();
}catch (ClassCastException e){}
super.onAttach(context);
}
private void setupgridview(){
final ArrayList<Photo> photos=new ArrayList<>();
DatabaseReference reference=FirebaseDatabase.getInstance().getReference();
Query query=reference
.child(getString(R.string.dbname_user_photos))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot singlesnapshot : dataSnapshot.getChildren()){
photos.add(singlesnapshot.getValue(Photo.class));
}
try {
//setup image grid
int gridwidth = getActivity().getResources().getDisplayMetrics().widthPixels;
int imagewidth = gridwidth / NUM_GRID_COLUMS;
gridView.setColumnWidth(imagewidth);
ArrayList<String> imgUrls = new ArrayList<String>();
for (int i = 0; i < photos.size(); i++) {
imgUrls.add(photos.get(i).getImage_path());
}
final GridImageAdapter adapter=new GridImageAdapter(getActivity(),R.layout.grid_imageview,"",imgUrls);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
monGridSelectedListerner.onGridImageSelected(photos.get(i),ACTIVITY_NUM);
}
});
}catch (NullPointerException e){}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void setprofilewidgets(UserSettings userSettings){
try {
User user = userSettings.getUser();
UsersAccountSettings settings = userSettings.getSettings();
UniversalImageLoader.setImage(settings.getProfile_photo(), mprofilephoto, null, "");
mfullname.setText(settings.getDisplay_name());
username.setText(settings.getUsername());
website.setText(settings.getWebsite());
description.setText(settings.getDescription());
posts.setText(String.valueOf(settings.getPosts()));
}catch (NullPointerException e){}
progressbar.setVisibility(View.GONE);
appbar.setVisibility(View.VISIBLE);
profilemenu.setEnabled(true);
}
private void setuptoolbar() {
profilemenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), AccountSettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
});
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void setupfirebaseAuth(){
mfirebasedatabase=FirebaseDatabase.getInstance();
mref=mfirebasedatabase.getReference();
mAuth = FirebaseAuth.getInstance();
mAuthlisterner=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user=firebaseAuth.getCurrentUser();
}
};
mref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//get user information from database
setprofilewidgets(firebaseMethods.getusersettings(dataSnapshot));
//get images from user information
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}