Guest User

Untitled

a guest
Feb 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. package com.hawkin.mycrud;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.AsyncTask;
  7. import android.support.annotation.NonNull;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.text.Editable;
  11. import android.text.TextWatcher;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.ViewGroup;
  15. import android.widget.AdapterView;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.BaseAdapter;
  18. import android.widget.Button;
  19. import android.widget.EditText;
  20. import android.widget.ListAdapter;
  21. import android.widget.ListView;
  22. import android.widget.SimpleAdapter;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. import org.json.JSONArray;
  27. import org.json.JSONException;
  28. import org.json.JSONObject;
  29.  
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.Locale;
  33.  
  34. import com.google.zxing.integration.android.IntentIntegrator;
  35. import com.google.zxing.integration.android.IntentResult;
  36.  
  37. public class TampilSemuaPgw extends AppCompatActivity implements ListView.OnItemClickListener{
  38.  
  39. EditText edittext1;
  40. Button button;
  41. private ListView listView;
  42.  
  43. private String JSON_STRING;
  44.  
  45. PegawaiListViewAdapter adapter;
  46.  
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_tampil_semua_pgw);
  51. edittext1 = (EditText) findViewById(R.id.edittext1);
  52. button = (Button) findViewById(R.id.button);
  53. listView = (ListView) findViewById(R.id.listView);
  54. listView.setOnItemClickListener(this);
  55. getJSON();
  56. }
  57.  
  58. public void onClick(View v){
  59. if(v.getId()==R.id.button){
  60. IntentIntegrator scanIntegrator = new IntentIntegrator(this);
  61. scanIntegrator.initiateScan();
  62. }
  63. }
  64.  
  65. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  66. IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
  67. if (scanningResult != null) {
  68. String scanContent = scanningResult.getContents();
  69. edittext1.setText(scanContent);
  70. }
  71. else{
  72. Toast toast = Toast.makeText(getApplicationContext(),
  73. "No scan data received!", Toast.LENGTH_SHORT);
  74. toast.show();
  75. }
  76. }
  77.  
  78. private void showEmployee(){
  79. JSONObject jsonObject = null;
  80. ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
  81. try {
  82. jsonObject = new JSONObject(JSON_STRING);
  83. JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
  84.  
  85. for(int i = 0; i<result.length(); i++){
  86. JSONObject jo = result.getJSONObject(i);
  87. String id = jo.getString(konfigurasi.TAG_ID);
  88. String name = jo.getString(konfigurasi.TAG_NAMA);
  89.  
  90. HashMap<String,String> employees = new HashMap<>();
  91. employees.put(konfigurasi.TAG_ID,id);
  92. employees.put(konfigurasi.TAG_NAMA,name);
  93. list.add(employees);
  94. }
  95.  
  96. } catch (JSONException e) {
  97. e.printStackTrace();
  98. }
  99.  
  100. // adapter = new SimpleAdapter(
  101. // TampilSemuaPgw.this, list, R.layout.list_item,
  102. // new String[]{konfigurasi.TAG_ID,konfigurasi.TAG_NAMA},
  103. // new int[]{R.id.id, R.id.name});
  104.  
  105. adapter = new PegawaiListViewAdapter(TampilSemuaPgw.this, list);
  106.  
  107. listView.setAdapter(adapter);
  108.  
  109. edittext1.addTextChangedListener(searchTextWatcher);
  110. }
  111.  
  112. private void getJSON(){
  113. class GetJSON extends AsyncTask<Void,Void,String>{
  114.  
  115. ProgressDialog loading;
  116. @Override
  117. protected void onPreExecute() {
  118. super.onPreExecute();
  119. loading = ProgressDialog.show(TampilSemuaPgw.this,"Mengambil Data","Mohon Tunggu...",false,false);
  120. }
  121.  
  122. @Override
  123. protected void onPostExecute(String s) {
  124. super.onPostExecute(s);
  125. loading.dismiss();
  126. JSON_STRING = s;
  127. showEmployee();
  128. }
  129.  
  130. @Override
  131. protected String doInBackground(Void... params) {
  132. RequestHandler rh = new RequestHandler();
  133. String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
  134. return s;
  135. }
  136. }
  137. GetJSON gj = new GetJSON();
  138. gj.execute();
  139. }
  140.  
  141. @Override
  142. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  143. Intent intent = new Intent(this, TampilPegawai.class);
  144. HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
  145. String empId = map.get(konfigurasi.TAG_ID).toString();
  146. intent.putExtra(konfigurasi.EMP_ID,empId);
  147. startActivity(intent);
  148. }
  149.  
  150. private final TextWatcher searchTextWatcher = new TextWatcher() {
  151.  
  152. @Override
  153. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  154.  
  155. }
  156.  
  157. @Override
  158. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  159. String text = edittext1.getText().toString().toLowerCase(Locale.getDefault());
  160. adapter.filter(text);
  161. // System.out.println(text);
  162. }
  163.  
  164. @Override
  165. public void afterTextChanged(Editable editable) {
  166. // System.out.println("tes2");
  167. }
  168. };
  169.  
  170. class PegawaiListViewAdapter extends ArrayAdapter {
  171. Context context;
  172. ArrayList<HashMap<String,String>> pegawaiList;
  173. ArrayList<HashMap<String,String>> pegawaiListBackup;
  174. LayoutInflater inflater;
  175.  
  176. public PegawaiListViewAdapter(Context context, ArrayList<HashMap<String,String>> pegawaiList) {
  177. super(context, R.layout.list_item, pegawaiList);
  178. this.context = context;
  179. this.pegawaiList = pegawaiList;
  180. this.pegawaiListBackup = new ArrayList<HashMap<String,String>>();
  181. this.pegawaiListBackup.addAll(pegawaiList);
  182. }
  183.  
  184. public View getView(int position, View convertView, ViewGroup parent) {
  185. LayoutInflater inflater = (LayoutInflater) context
  186. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  187. View rowView = inflater.inflate(R.layout.list_item, parent, false);
  188. TextView tvId = (TextView) rowView.findViewById(R.id.id);
  189. TextView tvName = (TextView) rowView.findViewById(R.id.name);
  190. tvId.setText(pegawaiList.get(position).get(konfigurasi.TAG_ID));
  191. tvName.setText(pegawaiList.get(position).get(konfigurasi.TAG_NAMA));
  192. return rowView;
  193. }
  194.  
  195. // Filter Class
  196. public void filter(String charText) {
  197. charText = charText.toLowerCase(Locale.getDefault());
  198. pegawaiList.clear();
  199. if (charText.length() == 0) {
  200. pegawaiList.addAll(pegawaiListBackup);
  201. } else {
  202. for (int i = 0; i < pegawaiListBackup.size(); i++){
  203. String nama = pegawaiListBackup.get(i).get(konfigurasi.TAG_NAMA).toLowerCase();
  204. // System.out.println(pegawaiListBackup.get(i).get(konfigurasi.TAG_NAMA));
  205. if (nama.contains(charText)){
  206. pegawaiList.add(pegawaiListBackup.get(i));
  207. }
  208. }
  209. }
  210. notifyDataSetChanged();
  211. }
  212. }
  213. }
Add Comment
Please, Sign In to add comment