diff --git a/app/src/main/java/com/example/nemophila/ShopActivity.java b/app/src/main/java/com/example/nemophila/ShopActivity.java index bd9b992..3b9d9bb 100644 --- a/app/src/main/java/com/example/nemophila/ShopActivity.java +++ b/app/src/main/java/com/example/nemophila/ShopActivity.java @@ -2,6 +2,7 @@ import static android.app.PendingIntent.getActivity; +import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; @@ -11,7 +12,9 @@ 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; @@ -25,7 +28,7 @@ import java.util.List; public class ShopActivity extends AppCompatActivity { - private ShopActivityAdapter adapter = null; + private ShopAcAdapter adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { @@ -74,12 +77,12 @@ } }); - List postsDataset = new ArrayList<>(); + List postsDataset = new ArrayList<>(); //RecyclerView(表示の設定、直接表示するところはLivedataを購読しているonChanged内) //xmlからrvにRecyclerViewを取得、アダプターを宣言 RecyclerView rv = (RecyclerView) findViewById(R.id.postsList); - adapter = new ShopActivityAdapter(postsDataset); + adapter = new ShopAcAdapter(postsDataset); //一行ずつを縦に(LinearLayout)表示するLayoutManagerを宣言 LinearLayoutManager llm = new LinearLayoutManager(this); @@ -101,11 +104,11 @@ public void onChanged(List ShopPosts) { //Nemophilaから選択中のジャンルを受け取る -// HashSet selectingGenre = ((Nemophila)getApplication()).getSelectGenres(); + HashSet selectingGenre = ((Nemophila)getApplication()).getSelectGenres(); //テスト用にHashSetにジャンルを入力 - HashSet selectingGenre = new HashSet<>(); - selectingGenre.add("ラーメン"); - selectingGenre.add("中華"); +// HashSet selectingGenre = new HashSet<>(); +// selectingGenre.add("ラーメン"); +// selectingGenre.add("中華"); //選択中に何かあれば表示 if(!(selectingGenre.isEmpty())) { @@ -114,12 +117,12 @@ } //Nemophilaから選択中のフレンドを受け取る -// HashSet selectingFriend = ((Nemophila)getApplication()).getSelectFriends(); + HashSet selectingFriend = ((Nemophila)getApplication()).getSelectFriends(); //テスト用にHashSetにフレンドを入力 - HashSet selectingFriend = new HashSet<>(); - selectingFriend.add("1111"); - selectingFriend.add("1113"); +// HashSet selectingFriend = new HashSet<>(); +// selectingFriend.add("1111"); +// selectingFriend.add("1113"); //選択したフレンドの表示のための宣言 HashSet selectingFriendName = new HashSet<>(); @@ -129,7 +132,7 @@ for (Post post: ShopPosts){ //dataを宣言 dataに投稿の必要なデータをセットし、それをpostsDatasetに追加してリストを作る - PostDataModel data = new PostDataModel(); + ShopAcDataModel data = new ShopAcDataModel(); //フィルター選択中かの判定のためにgenreとuidを先に読み込む String genre = post.getGenre(); String uid = post.getUid(); @@ -176,4 +179,105 @@ } }); } + + //RecyclerViewのための内部クラス + + //Adapter + public class ShopAcAdapter extends RecyclerView.Adapter { + + private List list; + + public ShopAcAdapter(List list) { + this.list =list; + } + + public void setList(List list) { + this.list = list; + } + + @NonNull + @Override + public ShopAcViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + + View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_shop_activity_posts, parent,false); + ShopAcViewHolder vh = new ShopAcViewHolder(inflate); + return vh; + } + + @Override + public void onBindViewHolder(@NonNull ShopAcViewHolder holder, int position) { + holder.nameView.setText(list.get(position).getName()); + 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 ShopAcViewHolder extends RecyclerView.ViewHolder { + public TextView nameView; + public TextView dateView; + public TextView rateView; + public TextView genreView; + public TextView commentView; + + public ShopAcViewHolder(@NonNull View itemView) { + super(itemView); + nameView = (TextView) itemView.findViewById(R.id.userName); + 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 static class ShopAcDataModel { + private String name; + private String date; + private String rate; + private String genre; + private String comment; + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + 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; + } + } + } \ No newline at end of file