Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / RequestingActivity.java
k-fujii on 6 Jul 2023 4 KB RequestingActivityの更新
package com.example.nemophila;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
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.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class RequestingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_requesting);

        //左上のボタンでマイページへ遷移
        Button returnButton = (Button)findViewById(R.id.requestingAcReturnButton);
        returnButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(getApplication(), MyPageActivity.class);
                startActivity(intent);
            }
        });

        //フレンド検索ボタン
        Button requestingButton = (Button)findViewById(R.id.requestingSearchButton);
        requestingButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                //入力されたIDを取得
                EditText editSearchId = (EditText) findViewById(R.id.inputRequestingId);
                String searchingId = editSearchId.getText().toString();
                //取得したIDのユーザー名を受け取る
                String requestingName;
                //申請を送るダイアログを表示
                RequestingDialogFragment dialog = new RequestingDialogFragment();
                dialog.show(getSupportFragmentManager(), "Requesting_dialog");
            }
        });

        //RecyclerView(表示の設定、直接表示するところはLivedataを購読しているonChanged内にしたい)
        List<RequestingActivity.RequestingDataModel> RequestingDataList = new ArrayList<>();

        //xmlからrvにRecyclerViewを取得、アダプターを宣言
        RecyclerView rv = (RecyclerView) findViewById(R.id.requestingList);
        RequestingActivity.RequestingAdapter adapter = new RequestingActivity.RequestingAdapter(RequestingDataList);

        //一行ずつを縦に(LinearLayout)表示するLayoutManagerを宣言
        LinearLayoutManager llm = new LinearLayoutManager(this);
        //
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        rv.addItemDecoration(itemDecoration);

        rv.setHasFixedSize(true);
        rv.setLayoutManager(llm);

    }


    //RecyclerViewのための内部クラス
    //とりあえずnameのみで仮作成

    //Adapter
    public class RequestingAdapter extends RecyclerView.Adapter<RequestingActivity.RequestingViewHolder> {

        private List<RequestingActivity.RequestingDataModel> list;

        public RequestingAdapter(List<RequestingActivity.RequestingDataModel> list) {
            this.list =list;
        }

        public void setList(List<RequestingActivity.RequestingDataModel> list) {
            this.list = list;
        }

        @NonNull
        @Override
        public RequestingActivity.RequestingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            //ShopActivityのレイアウトのままなので、ここを作って反映させる
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_shop_activity_posts, parent,false);
            RequestingActivity.RequestingViewHolder vh = new RequestingActivity.RequestingViewHolder(inflate);
            return vh;
        }

        @Override
        public void onBindViewHolder(@NonNull RequestingActivity.RequestingViewHolder holder, int position) {
            holder.nameView.setText(list.get(position).getName());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    //ViewHolder
    public class RequestingViewHolder extends RecyclerView.ViewHolder {
        public TextView nameView;

        public RequestingViewHolder(@NonNull View itemView) {
            super(itemView);
            nameView = (TextView) itemView.findViewById(R.id.userName);
        }
    }

    //DataModel
    public static class RequestingDataModel {
        private String name;

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

    }

}