Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / RequestedActivity.java
package com.example.nemophila;

import androidx.annotation.NonNull;
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.Account;
import com.example.nemophila.entities.AccountNameJson;
import com.example.nemophila.viewmodels.FriendViewModel;

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

public class RequestedActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //初期化
        FriendViewModel friendViewModel = new ViewModelProvider(this).get(FriendViewModel.class);
        Nemophila nemophila = (Nemophila) this.getApplication();
        List<RequestedUserModel> requestedDataSet = new ArrayList<>();
        LinearLayoutManager llm = new LinearLayoutManager(this);
        String uid = nemophila.getUid();
        String token = nemophila.getToken();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_requested);

        Button returnButton = (Button)findViewById(R.id.CancelButton);
        returnButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(getApplication(), MyPageActivity.class);
                startActivity(intent);
            }
        });

        RecyclerView rv = findViewById(R.id.requestedList);
        RequestedAdapter adapter = new RequestedAdapter(requestedDataSet);

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

        friendViewModel.getRequested(uid);
        friendViewModel.getRequestedLiveData().observe(this, new Observer<Collection<AccountNameJson>>() {
            @Override
            public void onChanged(Collection<AccountNameJson> accountNameJson) {
                requestedDataSet.clear();
                for (AccountNameJson acj: accountNameJson) {
                    RequestedUserModel data = new RequestedUserModel();
                    data.setId(acj.getUid());
                    data.setName(acj.getName());
                    requestedDataSet.add(data);
                }
                adapter.setList(requestedDataSet);
                rv.setAdapter(adapter);
            }
        });
    }

    //Adapter
    public class RequestedAdapter extends RecyclerView.Adapter<RequestedViewHolder> {
        private List<RequestedUserModel> list;
        public RequestedAdapter(List<RequestedUserModel> list) {
            this.list = list;
        }
        public void setList(List<RequestedUserModel> list) {
            this.list = list;
        }

        @NonNull
        @Override
        public RequestedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_requested_activity, parent,false);
            RequestedViewHolder vh = new RequestedViewHolder(inflate);
            return vh;
        }

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

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

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

        public RequestedViewHolder(@NonNull View view) {
            super(view);
            nameView = (TextView) view.findViewById(R.id.textView3);
        }
    }
    
    // DataModel
    public static class RequestedUserModel {
        private String name;
        private String id;

        public String getName() {
            return name;
        }
        public String getId() {
            return id;
        }

        public void setName(String name) {
            this.name = name;
        }
        public void setId(String id) { this.id = id;}
    }
}