我正在尝试订购我的回收站视图的元素,这些照片是上传的,但它们从最旧到最后列出的顺序是我发现的最新的,但我不知道如何解决它
我的回收者类这是一个片段:
public class PostFragment extends Fragment {
private View mRootView;
private FirebaseRecyclerAdapter<Post, PostHolder> mPostAdapter;
private RecyclerView mPostRecyclerView;
public PostFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_post, container, false);
FloatingActionButton fab = (FloatingActionButton) mRootView.findViewById(R.id.fabM);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PostCreateDialog dialog = new PostCreateDialog();
dialog.show(getFragmentManager(), null);
}
});
init();
return mRootView;
}
private void init() {
mPostRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerview_post);
mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
setupAdapter();
mPostRecyclerView.setAdapter(mPostAdapter);
}
private void setupAdapter() {
mPostAdapter = new FirebaseRecyclerAdapter<Post, PostHolder>(
Post.class,
R.layout.row_post,
PostHolder.class,
FirebaseUtils.getPostRef()
) {
@Override
protected void populateViewHolder(final PostHolder viewHolder, final Post model, int position) {
viewHolder.setNumComments(String.valueOf(model.getNumComments()));
viewHolder.setNumLikes(String.valueOf(model.getNumLikes()));
viewHolder.setTime(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
viewHolder.postOwnerUsernameTextView.setText(model.getUsername());
if(viewHolder.postOwnerUsernameTextView == null){
viewHolder.postOwnerUsernameTextView.setText("Anonymous");
}
viewHolder.setPostText(model.getPostText());
final StorageReference storageReference = FirebaseStorage.getInstance()
.getReference(model.getPostImageUrl());
Glide.with(getActivity())
.using(new FirebaseImageLoader())
.load(storageReference).crossFade().fitCenter().into(viewHolder.postDisplayImageView);
viewHolder.postLikeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLikeClick(model.getPostId());
}
});
viewHolder.postCommentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), CommentActivity.class);
intent.putExtra(Constants.EXTRA_POST, model);
startActivity(intent);
}
});
}
};
}
private void onLikeClick(final String postId) {
FirebaseUtils.getPostLikedRef(postId)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null) {
//user liked
FirebaseUtils.getPostRef()
.child(postId)
.child("numLikes")
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
long num = mutableData.getValue(Long.class);
mutableData.setValue(num - 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
FirebaseUtils.getPostLikedRef(postId)
.setValue(null);
}
});
} else {
FirebaseUtils.getPostRef()
.child(postId)
.child("numLikes")
.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
long num = mutableData.getValue(Long.class);
mutableData.setValue(num + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
FirebaseUtils.getPostLikedRef(postId)
.setValue(true);
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public static class PostHolder extends RecyclerView.ViewHolder {
TextView postOwnerUsernameTextView;
TextView postTimeCreatedTextView;
ImageView postDisplayImageView;
TextView postTextTextView;
LinearLayout postLikeLayout;
TextView postNumLikesTextView;
LinearLayout postCommentLayout;
TextView postNumCommentsTextView;
public PostHolder(View itemView) {
super(itemView);
postOwnerUsernameTextView = (TextView) itemView.findViewById(R.id.tv_post_username);
postTimeCreatedTextView = (TextView) itemView.findViewById(R.id.tv_time);
postDisplayImageView = (ImageView) itemView.findViewById(R.id.iv_post_display);
postLikeLayout = (LinearLayout) itemView.findViewById(R.id.like_layout);
postCommentLayout = (LinearLayout) itemView.findViewById(R.id.comment_layout);
postNumLikesTextView = (TextView) itemView.findViewById(R.id.tv_likes);
postNumCommentsTextView = (TextView) itemView.findViewById(R.id.tv_comments);
postTextTextView = (TextView) itemView.findViewById(R.id.tv_post_text);
}
public void setUsername(String username) {
postOwnerUsernameTextView.setText(username);
}
public void setTime(CharSequence time) {
postTimeCreatedTextView.setText(time);
}
public void setNumLikes(String numLikes) {
postNumLikesTextView.setText(numLikes);
}
public void setNumComments(String numComments) {
postNumCommentsTextView.setText(numComments);
}
public void setPostText(String text) {
postTextTextView.setText(text);
}
}
}
和帖子对话框您可以在此处选择要上传的照片:
public class PostCreateDialog extends DialogFragment implements View.OnClickListener {
private static final int RC_PHOTO_PICKER = 1;
private View mRootView;
private Post mPost;
private ProgressDialog mProgressDialog;
private Uri mSelectedImageUri;
private ImageView mPostDisplay;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
mPost = new Post();
mProgressDialog = new ProgressDialog(getContext());
mRootView = getActivity().getLayoutInflater().inflate(R.layout.post_create_dialog, null);
mPostDisplay = (ImageView) mRootView.findViewById(R.id.post_dialog_display);
mRootView.findViewById(R.id.post_dialog_select_imageview).setOnClickListener(this);
mRootView.findViewById(R.id.post_dialog_send_imageview).setOnClickListener(this);
builder.setView(mRootView);
return builder.create();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.post_dialog_send_imageview:
sendPost();
break;
case R.id.post_dialog_select_imageview:
selectImage();
break;
}
}
private void selectImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);
}
private void sendPost() {
mProgressDialog.setMessage("Sending post...");
mProgressDialog.setCancelable(false);
mProgressDialog.setIndeterminate(true);
mProgressDialog.show();
FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
final String postUid = FirebaseUtils.getUid();
TextView postDialogTextView = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
String text = postDialogTextView.getText().toString();
RegisterActivity r = new RegisterActivity();
mPost.setNumComments(0);
mPost.setNumLikes(0);
mPost.setTimeCreated(System.currentTimeMillis());
mPost.setPostId(postUid);
mPost.setPostText(text);
if (mSelectedImageUri != null) {
FirebaseUtils.getImagesSRef()
.child(mSelectedImageUri.getLastPathSegment())
.putFile(mSelectedImageUri)
.addOnSuccessListener(getActivity(),
new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = Constants.POST_IMAGES + "/" + mSelectedImageUri.getLastPathSegment();
//String downloadUrl = taskSnapshot.getDownloadUrl().toString();
mPost.setPostImageUrl(url);
addToMyPostList(postUid);
}
});
} else {
Toast.makeText(getContext(), "No puedes postear sin imagen", Toast.LENGTH_LONG).show();
mProgressDialog.dismiss();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
mProgressDialog.dismiss();
}
});
}
private void addToMyPostList(final String postUid) {
FirebaseUtils.getPostRef().child(postUid)
.setValue(mPost);
FirebaseUtils.getMyPostRef().child(postUid).setValue(true)
.addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mProgressDialog.dismiss();
dismiss();
}
});
FirebaseUtils.addToMyRecord(Constants.POSTS_KEY, postUid);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER) {
if (resultCode == RESULT_OK) {
mSelectedImageUri = data.getData();
mPostDisplay.setImageURI(mSelectedImageUri);
}
}
}
}
和utlis:
public static DatabaseReference getUserRef(String email) {
return FirebaseDatabase.getInstance()
.getReference(Constants.USERS_KEYS)
.child(email);
}
public static FirebaseUser getCurrentUser() {
return FirebaseAuth.getInstance().getCurrentUser();
}
// POST REFERENCE
public static DatabaseReference getPostRef() {
return FirebaseDatabase.getInstance()
.getReference(Constants.POSTS_KEY);
}
public static Query getPostQuery() {
return getPostRef().orderByChild(Constants.TIME_CREATED_KEY);
}
public static DatabaseReference getMyPostRef() {
return FirebaseDatabase.getInstance().getReference(Constants.MY_POSTS)
.child(getCurrentUser().getEmail().replace(".", ","));
}
// POST LIKED REFERENCE
public static DatabaseReference getPostLikedRef() {
return FirebaseDatabase.getInstance()
.getReference(Constants.POSTS_LIKED_KEY);
}
public static DatabaseReference getPostLikedRef(String postId) {
return getPostLikedRef().child(getCurrentUser().getEmail()
.replace(".", ","))
.child(postId);
}
// USER ID
public static String getUid() {
String path = FirebaseDatabase.getInstance().getReference().push().toString();
return path.substring(path.lastIndexOf("/") + 1);
}
// POST IMAGE REFERENCE
public static StorageReference getImagesSRef() {
return FirebaseStorage.getInstance().getReference(Constants.POST_IMAGES);
}
// COMMENT REFERENCE
public static DatabaseReference getCommentRef(String postId) {
return FirebaseDatabase.getInstance().getReference(Constants.COMMENTS_KEY)
.child(postId);
}
// RECORD
public static DatabaseReference getMyRecordRef() {
return FirebaseDatabase.getInstance().getReference(Constants.USER_RECORD)
.child(getCurrentUser().getEmail().replace(".", ","));
}
public static void addToMyRecord(String node,final String id) {
FirebaseUtils.getMyRecordRef().child(node).runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
ArrayList<String> myPostCollection;
if (mutableData.getValue() == null) {
myPostCollection = new ArrayList<>(1);
myPostCollection.add(id);
mutableData.setValue(myPostCollection);
} else {
myPostCollection = (ArrayList<String>) mutableData.getValue();
myPostCollection.add(id);
mutableData.setValue(myPostCollection);
}
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
}
答案 0 :(得分:0)
试试这个:
<script>
$(document).ready(function () {
var something = "";
$.ajax({
type: "POST",
url: "../04_Entries/04_42_ReservationCalander.aspx/TestOnWebService",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (doc) {
alert("Success");
var events = [];
var docd = doc.d;
something = docd;
var obj = $.parseJSON(doc.d);
console.log(obj);
for (var k in obj) {// k represents the id keys 1,2,3
var d = obj[k]; // We assign a the reference to each id object to the d variable
for (var p in d) { //Next we access the nested objects by using the p variable
if (p.hasOwnProperty) { //Check to make sure p has its own properties
//alert(p + ":" + d[p]); //We alert the key (d) and the keys value (d[p])
events.push(p + ": " + d[p]);
}
}
}
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
eventClick: function () {
alert('a day has been clicked!');
},
events: obj
})
console.log(events);
},
error: function (xhr, status, error) {
alert(xhr.responseText);
}
});
});
</script>