Android/활용

안드로이드 - 로그인, 회원가입 관리자모드 (회원관리기능) 추가 (1)

섭섭입니다 2020. 8. 20. 17:42

이전 포스팅

https://seopseop911.tistory.com/30

 

안드로이드 - 로그인, 회원가입 간단 구현하기 (mysql, php 이용)

이 포스팅은 동빈나, 홍드로이드님의 유튜브를 참고하였습니다. 앞서 WAMP를 이용해서 다운로드한 MySQL, PHP를 이용한다. https://seopseop911.tistory.com/27 Windows에 WAMP 실행, 설치방법 WAMP 란? Windows ..

seopseop911.tistory.com

 

이번에는 관리자 기능을 추가하여 자신의 데이터베이스에 있는 모든 회원들의 정보들을

 

관리자로그인을 통해서만 정보를 얻을 수 있게끔 이전 프로젝트를 수정한다.

 

 

 

< MainActivity.java >

 

 

[ 추가된 코드 ]

if(!userID.equals("admin"))
        {
            ManagementButton.setVisibility(View.GONE);
        }

        ManagementButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                new BackgroundTask().execute();
            }
        });

    }

    class BackgroundTask extends AsyncTask<Void, Void, String>
    {
        String target;

        @Override
        protected void onPreExecute() {
            target = "http://172.30.1.45/userList.php";
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                URL url  = new URL(target);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String temp;
                StringBuilder stringBuilder = new StringBuilder();
                while((temp = bufferedReader.readLine()) != null)
                {
                    stringBuilder.append(temp +"\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();

            } catch (Exception e){
                e.printStackTrace();
            }
                return null;
        }

        @Override
                public void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
        }

        @Override
                public void onPostExecute(String result){
             Intent intent = new Intent(MainActivity.this, ManagementActivity.class);
             intent.putExtra("userList", result);
             MainActivity.this.startActivity(intent);
          }

    }

 

[ 전체 코드 ]

package com.hongdroid.registerloginexample;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tv_id, tv_pass;

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

        tv_id = findViewById(R.id.tv_id);
        tv_pass = findViewById(R.id.tv_pass);

        Button ManagementButton = (Button) findViewById(R.id.ManagementButton);


        Intent intent = getIntent();
        String userID = intent.getStringExtra("userID");
        String userPass = intent.getStringExtra("userPass");

        tv_id.setText(userID);
        tv_pass.setText(userPass);

        if(!userID.equals("admin"))
        {
            ManagementButton.setVisibility(View.GONE);
        }

        ManagementButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                new BackgroundTask().execute();
            }
        });

    }

    class BackgroundTask extends AsyncTask<Void, Void, String>
    {
        String target;

        @Override
        protected void onPreExecute() {
            target = "http://172.30.1.45/userList.php";
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                URL url  = new URL(target);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String temp;
                StringBuilder stringBuilder = new StringBuilder();
                while((temp = bufferedReader.readLine()) != null)
                {
                    stringBuilder.append(temp +"\n");
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();

            } catch (Exception e){
                e.printStackTrace();
            }
                return null;
        }

        @Override
                public void onProgressUpdate(Void... values) {
                super.onProgressUpdate(values);
        }

        @Override
                public void onPostExecute(String result){
             Intent intent = new Intent(MainActivity.this, ManagementActivity.class);
             intent.putExtra("userList", result);
             MainActivity.this.startActivity(intent);
          }

    }
}

 

 

 

추가 Activity

1. ManagementActivity.java

 

 

< ManagementActivity.java >

package com.hongdroid.registerloginexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONObject;

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

public class ManagementActivity extends AppCompatActivity {

    private ListView listView;
    private userListAdapter adapter;
    private List<user> userList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_management);
        Intent intent = getIntent();

        listView = (ListView) findViewById(R.id.listView);
        userList = new ArrayList<user>();

        adapter = new userListAdapter(getApplicationContext(), userList);
        listView.setAdapter(adapter);

        try {
            JSONObject jsonObject = new JSONObject(intent.getStringExtra("userList"));
            JSONArray jsonArray = jsonObject.getJSONArray(("response"));
            int count =0;
            String userID, userPassword, userName, userAge;
            while(count < jsonArray.length())
            {
                JSONObject object = jsonArray.getJSONObject(count);
                userID = object.getString("userID");
                userPassword = object.getString("userPassword");
                userName = object.getString("userName");
                userAge = object.getString("userAge");
                user user = new user(userID, userPassword, userName, userAge);
                userList.add(user);
                count++;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

 

 

 

Class 추가

 

1. user

2. userListAdapter 

 

 

< user.java >

package com.hongdroid.registerloginexample;

public class user {


    String userID;
    String userPassword;
    String userName;
    String userAge;

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserAge() {
        return userAge;
    }

    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }

    public user(String userID, String userPassword, String userName, String userAge) {
        this.userID = userID;
        this.userPassword = userPassword;
        this.userName = userName;
        this.userAge = userAge;
    }
}

 

 

< userListAdapter.java >

package com.hongdroid.registerloginexample;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;

public class userListAdapter extends BaseAdapter {

    private Context context;
    private List<user> userList;

    public userListAdapter(Context context, List<user> userList){
        this.context = context;
        this.userList = userList;
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = View.inflate(context, R.layout.user, null);
        TextView userID = (TextView) v.findViewById(R.id.userID);
        TextView userPassword = (TextView) v.findViewById(R.id.userPassword);
        TextView userName = (TextView) v.findViewById(R.id.userName);
        TextView userAge = (TextView) v.findViewById(R.id.userAge);

        userID.setText(userList.get(position).getUserID());
        userPassword.setText(userList.get(position).getUserPassword());
        userName.setText(userList.get(position).getUserName());
        userAge.setText(userList.get(position).getUserAge());

        v.setTag(userList.get(position).getUserID());
        return v;
    }
}

 

< activity_management.xml >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#d1d1d1"
    tools:context=".ManagementActivity">

    
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:dividerHeight="10dp"
        android:divider="#d1d1d1"></ListView>
</LinearLayout>

 

 

layout 추가

 

1. user.xml

 

 

< activity_user.xml >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:paddingRight="10dp"
    android:paddingLeft="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#ffffff">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userID"
        android:text="아이디"
        android:textSize="20dp"
        android:textColor="#00a7f5" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userPassword"
        android:text="비밀번호"
        android:textSize="15dp"
        android:textColor="#f92a3f" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userName"
        android:text="이름"
        android:textSize="26dp"
        android:textColor="#000" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userAge"
        android:text="나이"
        android:textSize="18dp"
        android:textColor="#00a7f5" />

</LinearLayout>

 

 

 

관리자 모드로 접속

 

 

 

 

접속한 뒤 회원관리 버튼이 생성된 것을 볼 수 있다. 클릭.

 

 

 

아래 사진과 같이 데이터베이스의 있는 정보들을 불러와 Listview를 통해 보여주고 있다.

 

 

 

참고 : 동빈나 유튜브: https://www.youtube.com/channel/UChflhu32f5EUHlY7_SetNWw

         홍드로이드 유튜브: https://www.youtube.com/watch?v=ktjJ8xtt2Hg&t=2998s