我有一个从parse.com获取条目的代码。当用户第一次输入片段时,它可以正常工作,但是当用户第二次输入片段时,会多次显示一个条目。
第一次: -
第二次: -
这是我的代码: -
public class ReportHistoryFragment extends Fragment {
View reportHistory;
ParseUser parseUser;
ParseQuery<ParseObject> parseQuery,pq;
ParseObject parseObject;
String title,desc;
Bitmap bitmap;
ParseFile parseFile;
Context context;
Card card;
ArrayList<Card> cards;
CardListView cardListView;
CardArrayAdapter cardArrayAdapter;
int size,i=0;
ProgressDialog progressDialog;
Iterator<ParseObject> iterator;
public ReportHistoryFragment() {
**}
@SuppressLint("ValidFragment")
public ReportHistoryFragment(Context context) {
this.context=context;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
reportHistory = inflater.inflate(R.layout.report_history_layout,container,false);
cardListView = (CardListView) reportHistory.findViewById(R.id.listcard);
parseUser = ParseUser.getCurrentUser();
progressDialog = ProgressDialog.show(context,"Downloading Data","Please wait...",true);
progressDialog.setCancelable(false);
downloadData();
return reportHistory;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
}
public void downloadData() {
parseQuery=null;
pq=null;
cards = new ArrayList<>();
parseQuery = ParseQuery.getQuery("Reports");
parseQuery.whereEqualTo("user",parseUser);
parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
iterator = parseObjects.iterator();
size = parseObjects.size();
i=0;
while (iterator.hasNext()) {
parseObject = iterator.next();
pq = ParseQuery.getQuery("Reports");
pq.getInBackground(parseObject.getObjectId(), new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
title = object.getString("title");
desc = object.getString("description");
parseFile = (ParseFile) object.get("image");
parseFile.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, ParseException e) {
if(e==null) {
bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
} else {
e.printStackTrace();
}
card = new HistoryItemCard(getActivity(),title,desc,bitmap);
cards.add(card);
i++;
if(i==size) {
cardArrayAdapter = new CardArrayAdapter(getActivity(),cards);
cardArrayAdapter.notifyDataSetChanged();
cardListView.setAdapter(cardArrayAdapter);
progressDialog.dismiss();
}
}
});
} else {
e.printStackTrace();
}
}
});
}
} else {
e.printStackTrace();
}
}
});
}
}
请有人帮助我!