Android/활용

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

섭섭입니다 2020. 8. 23. 18:42

이전 포스팅 

 

https://seopseop911.tistory.com/32?category=828715

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

이전 포스팅 https://seopseop911.tistory.com/31 안드로이드 - 로그인, 회원가입 관리자모드 (회원관리기능) 추가 (1) 이전 포스팅 https://seopseop911.tistory.com/30 안드로이드 - 로그인, 회원가입 간단 구현..

seopseop911.tistory.com

 

이전 관리자모드를 만들어서 회원들의 리스트를 보여준 뒤 회원들을 삭제하는 기능까지 해보았는데

 

이번엔 검색기능도 추가하는 것을 따라해보았다. 

 

 

먼저 검색기능을 하는 EditTextActivity_management.xml 화면에 추가할 것이다.

 

 

< Activity_management.xml >

 

[ 추가코드 ]

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/search"
        android:hint="검색"/>

 

 

 

< ManagementActivity.java > 

package com.hongdroid.registerloginexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
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;
    private List<user> saveList;   // ## 추가된 코드 ##

    @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>();
        saveList = new ArrayList<user>();  // ## 추가된 코드 ##

        adapter = new userListAdapter(getApplicationContext(), userList, this, saveList); 
        // ## saveListc 추가 ##
        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);
                if(!userID.equals("admin"))
                {
                    userList.add(user);
                    saveList.add(user);  // ## 추가된 코드 ##
                }

                count++;

            }

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

        // ## 추가된 코드 맨 아래까지 ##
        EditText search = (EditText) findViewById(R.id.search);
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                searchUser(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });  
						

    }

    public void searchUser(String search) {
        userList.clear();
        for(int i =0; i<saveList.size();   i++) {
            if(saveList.get(i).getUserID().contains(search))
            {
                userList.add(saveList.get(i));
            }
        }
        adapter.notifyDataSetChanged();
    }
}  

 

 

 

< userListAdapter.java >

package com.hongdroid.registerloginexample;

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

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.List;

public class userListAdapter extends BaseAdapter {

    private Context context;
    private List<user> userList;
    private Activity parentActivity;
    private List<user> saveList;

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

    @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(final int i, View convertView, final ViewGroup parent) {
        View v = View.inflate(context, R.layout.user, null);
        final 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(i).getUserID());
        userPassword.setText(userList.get(i).getUserPassword());
        userName.setText(userList.get(i).getUserName());
        userAge.setText(userList.get(i).getUserAge());

        v.setTag(userList.get(i).getUserID());

        Button deleteButton = (Button) v.findViewById(R.id.deleteButton);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                Response.Listener<String> responseListener = new Response.Listener<String>() {
                     @Override
                     public void onResponse(String response) {
                          try   {
                                JSONObject jsonResponse = new JSONObject(response);
                                boolean success = jsonResponse.getBoolean("success");
                                if(success) {
                                    userList.remove(i);
                                    for(int i =0; i<saveList.size(); i++)
                                    {
                                        if(saveList.get(i).getUserID().equals(userID.getText().toString()))
                                        {
                                            saveList.remove(i);
                                            break;
                                        }
                                    }
                                    notifyDataSetChanged();
                                }
                          }
                          catch (Exception e) {
                              e.printStackTrace();
                          }
                       }
                };
                DeleteRequest deleteRequest = new DeleteRequest(userID.getText().toString(), responseListener);
                RequestQueue queue = Volley.newRequestQueue(parentActivity);
                queue.add(deleteRequest);
            }
        });

        return v;
    }
}

 

 

 

검색 EditText , 그리고 회원 목록이 보인다.

 

 

 

 

 

c라고 검색 했을 때 나오는 검색 결과이다. 

 

 







 참고 : 동빈나 유튜브: https://www.youtube.com/channel/UChflhu32f5EUHlY7_SetNWw
홍드로이드 유튜브: https://www.youtube.com/watch?v=ktjJ8xtt2Hg&t=2998s