package com.example.nemophila; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.example.nemophila.entities.Post; import com.example.nemophila.viewmodels.AccountViewModel; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class MyPageActivity extends AppCompatActivity { //MyPageActivity画面の作成 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mypage); Nemophila nemophila = (Nemophila) getApplication(); String uid = nemophila.getUid(); String name = nemophila.getName(); TextView myName = (TextView) findViewById(R.id.name); myName.setText(name); TextView id = (TextView) findViewById(R.id.id); id.setText(uid); // 「閉じる」ボタンを押したときにメイン画面へ遷移する Button closeButton = (Button) findViewById(R.id.close); closeButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, MapsActivity.class); startActivity(intent); } }); //「フレンドリスト」ボタンを押したときにフレンドリスト画面へ遷移する Button friendButton = (Button) findViewById(R.id.friend); friendButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, FriendActivity.class); startActivity(intent); } }); // 「フレンド申請する」ボタンを押したときにフレンド申請画面へ遷移する Button requestingButton = (Button) findViewById(R.id.requesting); requestingButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, RequestingActivity.class); startActivity(intent); } }); // 「フレンド許可する」ボタンを押したときにフレンド許可画面へ遷移する Button requestedButton = (Button) findViewById(R.id.requested); requestedButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, RequestedActivity.class); startActivity(intent); } }); // 「マイページ編集」ボタンを押したときにマイページ編集画面へ遷移する Button myPageEditButton = (Button) findViewById(R.id.myPageEdit); myPageEditButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MyPageActivity.this, MyPageEditorActivity.class); startActivity(intent); } }); //getNameLiveDataとの通信により今のユーザーネームを表示する AccountViewModel accountViewModel = new ViewModelProvider(this).get(AccountViewModel.class); accountViewModel.getNameLiveData().observe(this, new Observer <String>() { @Override public void onChanged(String name) { TextView myName = (TextView) findViewById(R.id.name); myName.setText(name); nemophila.setName(name); } }); /* ここからあんまわかっていない */ RecyclerView rv = (RecyclerView) findViewById(R.id.postsList); LinearLayoutManager llm = new LinearLayoutManager(this); List<MyPageActDataModel> myPageActDataset = new ArrayList<>(); MyPageActAdapter adapter = new MyPageActAdapter(myPageActDataset); rv.setHasFixedSize(true); rv.setLayoutManager(llm); rv.setAdapter(adapter); accountViewModel.getAccountPosts(uid); accountViewModel.getAccountPostsLiveData().observe(this, new Observer <Collection<Post>>() { @Override public void onChanged(Collection<Post> Posts) { //MyPageActDatasetを一旦クリアし、LiveDataから受け取ったデータを一つずつセット myPageActDataset.clear(); for (Post post: Posts){ MyPageActDataModel data = new MyPageActDataModel(); data.setDate(post.getDate()); //rateはintで受け取った評価に対応した星の数のStringに変換してセット int intRate = post.getRate(); String strRate = ""; for (int i = 0; i < 5; i++){ if(intRate > i ){ strRate += "★"; }else{ strRate += "☆"; } } data.setRate(strRate); data.setGenre(post.getGenre()); data.setComment(post.getComment()); myPageActDataset.add(data); } //RecyclerViewをonChangedが呼ばれるたび表示(アダプターを更新してRecyclerViewにセット) adapter.setList(myPageActDataset); rv.setAdapter(adapter); } }); } /* ここからMyPageActivityの内部クラス */ // Adapter public class MyPageActAdapter extends RecyclerView.Adapter<MyPageActViewHolder> { private List<MyPageActDataModel> list; public void setList(List<MyPageActDataModel> list) { this.list = list; } public MyPageActAdapter(List<MyPageActDataModel> list) { this.list = list; } @Override public MyPageActViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_my_page_activity_posts, parent,false); MyPageActViewHolder vh = new MyPageActViewHolder(inflate); return vh; } @Override public void onBindViewHolder(MyPageActViewHolder holder, int position) { holder.dateView.setText(list.get(position).getDate()); holder.rateView.setText(list.get(position).getRate()); holder.genreView.setText(list.get(position).getGenre()); holder.commentView.setText(list.get(position).getComment()); } @Override public int getItemCount() { return list.size(); } } // ViewHolder public class MyPageActViewHolder extends RecyclerView.ViewHolder { public TextView dateView; public TextView rateView; public TextView genreView; public TextView commentView; public MyPageActViewHolder(View itemView) { super(itemView); dateView = (TextView) itemView.findViewById(R.id.date); rateView = (TextView) itemView.findViewById(R.id.rate); genreView = (TextView) itemView.findViewById(R.id.genre); commentView = (TextView) itemView.findViewById(R.id.comment); } } // DataModel public class MyPageActDataModel { private String date; private String rate; private String genre; private String comment; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getRate() { return rate; } public void setRate(String rate) { this.rate = rate; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } } }