package com.link2me.android.recyclerviewjava;
import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.widget.CheckBox; import android.widget.RelativeLayout;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.SearchView; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.Volley; import com.link2me.android.recyclerviewjava.adapter.RecyclerViewAdapter; import com.link2me.android.recyclerviewjava.model.Address_Item;
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;
import java.util.ArrayList; import java.util.HashMap;
public class MainActivity extends AppCompatActivity { private final String TAG = this.getClass().getSimpleName(); Context context;
private ArrayList<Address_Item> addressItemList = new ArrayList<>(); // 서버 원본 데이터 리스트 private ArrayList<Address_Item> searchItemList = new ArrayList<>(); // 검색한 데이터 리스트 private RecyclerView mRecyclerView; private RecyclerViewAdapter mAdapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = MainActivity.this;
initView(); }
private void initView() { // Adapter에 추가 데이터를 저장하기 위한 ArrayList getServerData(Value.IPADDRESS+"getJSONData.php"); // 서버 데이터 가져오기 }
private void getServerData(String SERVER_URL) { HashMap<String, String> params = new HashMap<>(); params.put("keyword", Value.encrypt(Value.URLkey())); JSONObject jsonObject = new JSONObject(params);
// 1. RequestQueue 생성 및 초기화 RequestQueue requestQueue = Volley.newRequestQueue(context);
// 2. Request Obejct인 StringRequest 생성 JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, SERVER_URL,jsonObject, (Response.Listener<JSONObject>) response -> { Log.e("result", "" + response); try { if(response.getString("status").equals("success")){ String jsondata = response.getString("addrinfo"); showJSONList(jsondata); } else { showAlert(response.getString("status"),response.getString("message")); } } catch (JSONException e) { e.printStackTrace(); } }, error -> Log.d("error", "[" + error.getMessage() + "]")) {
};
// 3) 생성한 StringRequest를 RequestQueue에 추가 requestQueue.add(request); }
private void showJSONList(String JSONdata) { try { JSONArray peoples = new JSONArray(JSONdata); // [] 대괄호 addressItemList.clear(); // 서버에서 가져온 데이터 초기화 for(int i=0;i < peoples.length();i++){ JSONObject jsonObj = peoples.getJSONObject(i); final String uid = jsonObj.getString("uid"); final String name = jsonObj.getString("userNM"); final String mobileNO = jsonObj.getString("mobileNO"); final String telNO = jsonObj.getString("telNO"); final String photo = jsonObj.getString("photo");
getServerDataList(uid,name,mobileNO,telNO,photo,false); selectDataList(uid,name,mobileNO,telNO,photo,false); }
mRecyclerView = findViewById(R.id.address_listview); mAdapter = new RecyclerViewAdapter(context,addressItemList); LinearLayoutManager manager = new LinearLayoutManager(context); DividerItemDecoration decoration = new DividerItemDecoration(context,manager.getOrientation());
mRecyclerView.addItemDecoration(decoration); mRecyclerView.setLayoutManager(manager); mRecyclerView.setAdapter(mAdapter);
} catch (JSONException e) { e.printStackTrace(); } }
// 아이템 데이터 추가를 위한 메소드 public void getServerDataList(String uid, String name, String mobileNO, String officeNO, String photo_image, boolean checkItem_flag) { Address_Item item = new Address_Item(uid,name,mobileNO,officeNO,photo_image,checkItem_flag); addressItemList.add(item); }
public void selectDataList(String uid, String name, String mobileNO, String officeNO, String photo_image, boolean checkItem_flag) { Address_Item item = new Address_Item(uid,name,mobileNO,officeNO,photo_image,checkItem_flag); searchItemList.add(item); }
public void showAlert(String title, String message) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(title); builder.setMessage(message) .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }
}
|