現在、PhotoクラスのRecyclerAdapterにgetItem
という問題が発生しています。私は追加しようとしましたRecyclerAdapterのオブジェクトにgetItemを取得する際に問題が発生しました
public int getItem(int pos){
return getItem(pos);
}
しかし、それは動作していないようです。どのようにすればgetItem(position)
にエラーがありますか? (私が十分精緻化していない場合は、)
ここに私が間違っていることを教えてください。
ViewProfilePostsAdapter.java
public class ViewProfilePostsAdapter extends RecyclerView.Adapter<ViewProfilePostsAdapter.ViewHolder> {
private static final String TAG = "CustomAdapter";
private String[] mDataSet;
private int[] getItemIn;
private LayoutInflater mInflater;
private int mLayoutResource;
private Context mContext;
private DatabaseReference mReference;
private String currentUsername;
private Photo mPic;
public static class ViewHolder extends RecyclerView.ViewHolder {
//private final TextView textView;
ImageView mProfileImage;
String likesString;
TextView username, timeDelta, caption, likes, liked;
ImageView postImage;
ImageView heartRed, heartWhite;
UserAccountSettings settings = new UserAccountSettings();
User user = new User();
StringBuilder users;
String mLikesString;
boolean likeByCurrentUser;
Heart heart;
GestureDetector detector;
Photo photo;
public ViewHolder(View convertView) {
super(convertView);
if(convertView == null) {
username = (TextView) convertView.findViewById(R.id.display_name);
postImage = (ImageView) convertView.findViewById(R.id.post_image);
caption = (TextView) convertView.findViewById(R.id.image_caption);
timeDelta = (TextView) convertView.findViewById(R.id.image_time_posted);
likes = (TextView) convertView.findViewById(R.id.tvLikesString);
mProfileImage = (ImageView) convertView.findViewById(R.id.f_profile_image);
heartRed = (ImageView) convertView.findViewById(R.id.image_heart_red);
heartWhite = (ImageView) convertView.findViewById(R.id.image_heart_white);
heart = new Heart(heartWhite, heartRed);
liked = (TextView) convertView.findViewById(R.id.likeText);
}
}
}
public ViewProfilePostsAdapter(String[] dataSet, int[] intSet) {
mDataSet = dataSet;
getItemIn = intSet;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.layout_post_listitem, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position)
{
Log.d(TAG, "Element " + position + " set.");
viewHolder.photo = getItem(position);
viewHolder.detector = new GestureDetector(mContext, new GestureListener(viewHolder));
getCurrentUsername();
getLikesString(viewHolder);
viewHolder.caption.setText(getItem(position).getCaption());
String timestampDifference = getTimestampDifference(getItem(position));
if(!timestampDifference.equals("0")){
viewHolder.timeDelta.setText(timestampDifference + " DAYS AGO");
}else{
viewHolder.timeDelta.setText("TODAY");
}
final ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(getItem(position).getImage_path(), viewHolder.postImage);
//get the profile image and username
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_user_account_settings))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: OnDataChange ******************************************************************************");
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.e(TAG, "onDataChange: found user: "
+ singleSnapshot.getValue(UserAccountSettings.class).getUsername() + "****************************");
holder.username.setText(singleSnapshot.getValue(UserAccountSettings.class).getUsername());
imageLoader.displayImage(singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
holder.mProfileImage);
holder.settings = singleSnapshot.getValue(UserAccountSettings.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Query userQuery = mReference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.getValue(User.class).getUsername());
viewHolder.user = singleSnapshot.getValue(User.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public int getItemCount() {
return mDataSet.length;
}
public int getItem(int pos){
return getItem(pos);
}
public class GestureListener extends GestureDetector.SimpleOnGestureListener{
ViewHolder mHolder;
public GestureListener(ViewHolder holder) {
mHolder = holder;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.d(TAG, "onDoubleTap: double tap detected.");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren())
{
String keyID = singleSnapshot.getKey();
//case1: Then user already liked the photo
if(mHolder.likeByCurrentUser &&
singleSnapshot.getValue(Like.class).getUser_id()
.equals(FirebaseAuth.getInstance().getCurrentUser().getUid())){
mReference.child(mContext.getString(R.string.dbname_photos))
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(mHolder.photo.getUser_id())
.child(mHolder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mHolder.heart.toggleLike();
getLikesString(mHolder);
}
//case2: The user has not liked the photo
else if(!mHolder.likeByCurrentUser){
//add new like
addNewLike(mHolder);
break;
}
}
if(!dataSnapshot.exists()){
//add new like
addNewLike(mHolder);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
}
private void addNewLike(final ViewHolder holder){
Log.d(TAG, "addNewLike: adding new like");
String newLikeID = mReference.push().getKey();
Like like = new Like();
like.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(like);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(holder.photo.getUser_id())
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(like);
holder.heart.toggleLike();
getLikesString(holder);
}
private void getCurrentUsername(){
Log.d(TAG, "getCurrentUsername: retrieving user account settings");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
currentUsername = singleSnapshot.getValue(UserAccountSettings.class).getUsername();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void getLikesString(final ViewHolder holder){
Log.d(TAG, "getLikesString: getting likes string");
try{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(holder.photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
holder.users = new StringBuilder();
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren())
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(mContext.getString(R.string.dbname_users))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(singleSnapshot.getValue(Like.class).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
Log.d(TAG, "onDataChange: found like: " +
singleSnapshot.getValue(User.class).getUsername());
holder.users.append(singleSnapshot.getValue(User.class).getUsername());
holder.users.append(",");
}
String[] splitUsers = holder.users.toString().split(",");
if(holder.users.toString().contains(currentUsername + ",")){
holder.likeByCurrentUser = true;
}else{
holder.likeByCurrentUser = false;
}
int length = splitUsers.length;
if(length == 1){
holder.likesString = "Liked by " + splitUsers[0];
}
else if(length == 2){
holder.likesString = "Liked by " + splitUsers[0]
+ " and " + splitUsers[1];
}
else if(length == 3){
holder.likesString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ " and " + splitUsers[2];
}
else if(length == 4){
holder.likesString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ ", " + splitUsers[2]
+ " and " + splitUsers[3];
}
else if(length > 4){
holder.likesString = "Liked by " + splitUsers[0]
+ ", " + splitUsers[1]
+ ", " + splitUsers[2]
+ " and " + (splitUsers.length - 3) + " others";
}
Log.d(TAG, "onDataChange: likes string: " + holder.likesString);
//setup likes string
setupLikesString(holder, holder.likesString);
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
}
if(!dataSnapshot.exists()){
holder.likesString = "";
holder.likeByCurrentUser = false;
//setup likes string
setupLikesString(holder, holder.likesString);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}catch (NullPointerException e){
Log.e(TAG, "getLikesString: NullPointerException: " + e.getMessage()
);
holder.likesString = "";
holder.likeByCurrentUser = false;
//setup likes string
setupLikesString(holder, holder.likesString);
}
}
private void setupLikesString(final ViewHolder holder, String likesString) {
Log.d(TAG, "setupLikesString: likes string:" + holder.likesString);
if (holder.likeByCurrentUser) {
Log.d(TAG, "setupLikesString: photo is liked by current user");
holder.liked.setText("UnLike");
holder.heartWhite.setVisibility(View.GONE);
holder.heartRed.setVisibility(View.VISIBLE);
holder.heartRed.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return holder.detector.onTouchEvent(event);
}
});
} else {
Log.d(TAG, "setupLikesString: photo is not liked by current user");
holder.liked.setText("Like");
holder.heartWhite.setVisibility(View.VISIBLE);
holder.heartRed.setVisibility(View.GONE);
holder.heartWhite.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return holder.detector.onTouchEvent(event);
}
});
}
holder.likes.setText(likesString);
}
//---------------------------------------------end of likes----------------------------
private String getTimestampDifference(Photo photo){
Log.d(TAG, "getTimestampDifference: getting timestamp difference.");
String difference = "";
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.CANADA);
sdf.setTimeZone(TimeZone.getTimeZone("Canada/Pacific"));//google 'android list of timezones'
Date today = c.getTime();
sdf.format(today);
Date timestamp;
final String photoTimestamp = photo.getDate_created();
try{
timestamp = sdf.parse(photoTimestamp);
difference = String.valueOf(Math.round(((today.getTime() - timestamp.getTime())/1000/60/60/24)));
}catch (ParseException e){
Log.e(TAG, "getTimestampDifference: ParseException: " + e.getMessage());
difference = "0";
}
return difference;
}
}
Photo.java
public class Photo implements Parcelable {
private String caption;
private String date_created;
private String photo_id;
private String user_id;
private String tags;
private String image_path;
private List<Like> likes;
public Photo(){
}
public Photo(String caption, String date_created, String photo_id, String user_id, String tags, String image_path, List<Like> likes) {
this.caption = caption;
this.date_created = date_created;
this.photo_id = photo_id;
this.user_id = user_id;
this.tags = tags;
this.image_path = image_path;
this.likes = likes;
}
protected Photo(Parcel in) {
caption = in.readString();
date_created = in.readString();
photo_id = in.readString();
user_id = in.readString();
tags = in.readString();
image_path = in.readString();
}
public static final Creator<Photo> CREATOR = new Creator<Photo>() {
@Override
public Photo createFromParcel(Parcel in) {
return new Photo(in);
}
@Override
public Photo[] newArray(int size) {
return new Photo[size];
}
};
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String getDate_created() {
return date_created;
}
public void setDate_created(String date_created) {
this.date_created = date_created;
}
public String getPhoto_id() {
return photo_id;
}
public void setPhoto_id(String photo_id) {
this.photo_id = photo_id;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public String getImage_path() {
return image_path;
}
public void setImage_path(String image_path) {
this.image_path = image_path;
}
public List<Like> getLikes() {
return likes;
}
public void setLikes(List<Like> likes) {
this.likes = likes;
}
@Override
public String toString() {
return "Photo{" +
"caption='" + caption + '\'' +
", date_created='" + date_created + '\'' +
", photo_id='" + photo_id + '\'' +
", user_id='" + user_id + '\'' +
", tags='" + tags + '\'' +
", image_path='" + image_path + '\'' +
", likes=" + likes +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(caption);
parcel.writeString(date_created);
parcel.writeString(photo_id);
parcel.writeString(user_id);
parcel.writeString(tags);
parcel.writeString(image_path);
}
}
この機能の機能は何ですか?それは自分自身を呼び出しているint getItem(int pos){ return getItem(pos); }関数は、intではなくobectを返さなければなりません。 – lib4
だから私はあなたが言っていることを理解していることは、publicの後で 'int'を変更していることを意味しています。public int getItem(int pos){ return getItem(pos); } 'を' Object'に渡して、オブジェクトを返します。その場合、動作しません。P –
この機能の機能を理解できますか? public int getItem(int pos){getItem(pos);戻り値を取得します。 } – lib4