Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hawkin.mycrud;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Locale;
- import com.google.zxing.integration.android.IntentIntegrator;
- import com.google.zxing.integration.android.IntentResult;
- public class TampilSemuaPgw extends AppCompatActivity implements ListView.OnItemClickListener{
- EditText edittext1;
- Button button;
- private ListView listView;
- private String JSON_STRING;
- PegawaiListViewAdapter adapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tampil_semua_pgw);
- edittext1 = (EditText) findViewById(R.id.edittext1);
- button = (Button) findViewById(R.id.button);
- listView = (ListView) findViewById(R.id.listView);
- listView.setOnItemClickListener(this);
- getJSON();
- }
- public void onClick(View v){
- if(v.getId()==R.id.button){
- IntentIntegrator scanIntegrator = new IntentIntegrator(this);
- scanIntegrator.initiateScan();
- }
- }
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
- if (scanningResult != null) {
- String scanContent = scanningResult.getContents();
- edittext1.setText(scanContent);
- }
- else{
- Toast toast = Toast.makeText(getApplicationContext(),
- "No scan data received!", Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- private void showEmployee(){
- JSONObject jsonObject = null;
- ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
- try {
- jsonObject = new JSONObject(JSON_STRING);
- JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
- for(int i = 0; i<result.length(); i++){
- JSONObject jo = result.getJSONObject(i);
- String id = jo.getString(konfigurasi.TAG_ID);
- String name = jo.getString(konfigurasi.TAG_NAMA);
- HashMap<String,String> employees = new HashMap<>();
- employees.put(konfigurasi.TAG_ID,id);
- employees.put(konfigurasi.TAG_NAMA,name);
- list.add(employees);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- // adapter = new SimpleAdapter(
- // TampilSemuaPgw.this, list, R.layout.list_item,
- // new String[]{konfigurasi.TAG_ID,konfigurasi.TAG_NAMA},
- // new int[]{R.id.id, R.id.name});
- adapter = new PegawaiListViewAdapter(TampilSemuaPgw.this, list);
- listView.setAdapter(adapter);
- edittext1.addTextChangedListener(searchTextWatcher);
- }
- private void getJSON(){
- class GetJSON extends AsyncTask<Void,Void,String>{
- ProgressDialog loading;
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- loading = ProgressDialog.show(TampilSemuaPgw.this,"Mengambil Data","Mohon Tunggu...",false,false);
- }
- @Override
- protected void onPostExecute(String s) {
- super.onPostExecute(s);
- loading.dismiss();
- JSON_STRING = s;
- showEmployee();
- }
- @Override
- protected String doInBackground(Void... params) {
- RequestHandler rh = new RequestHandler();
- String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
- return s;
- }
- }
- GetJSON gj = new GetJSON();
- gj.execute();
- }
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Intent intent = new Intent(this, TampilPegawai.class);
- HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);
- String empId = map.get(konfigurasi.TAG_ID).toString();
- intent.putExtra(konfigurasi.EMP_ID,empId);
- startActivity(intent);
- }
- private final TextWatcher searchTextWatcher = new TextWatcher() {
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- String text = edittext1.getText().toString().toLowerCase(Locale.getDefault());
- adapter.filter(text);
- // System.out.println(text);
- }
- @Override
- public void afterTextChanged(Editable editable) {
- // System.out.println("tes2");
- }
- };
- class PegawaiListViewAdapter extends ArrayAdapter {
- Context context;
- ArrayList<HashMap<String,String>> pegawaiList;
- ArrayList<HashMap<String,String>> pegawaiListBackup;
- LayoutInflater inflater;
- public PegawaiListViewAdapter(Context context, ArrayList<HashMap<String,String>> pegawaiList) {
- super(context, R.layout.list_item, pegawaiList);
- this.context = context;
- this.pegawaiList = pegawaiList;
- this.pegawaiListBackup = new ArrayList<HashMap<String,String>>();
- this.pegawaiListBackup.addAll(pegawaiList);
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- View rowView = inflater.inflate(R.layout.list_item, parent, false);
- TextView tvId = (TextView) rowView.findViewById(R.id.id);
- TextView tvName = (TextView) rowView.findViewById(R.id.name);
- tvId.setText(pegawaiList.get(position).get(konfigurasi.TAG_ID));
- tvName.setText(pegawaiList.get(position).get(konfigurasi.TAG_NAMA));
- return rowView;
- }
- // Filter Class
- public void filter(String charText) {
- charText = charText.toLowerCase(Locale.getDefault());
- pegawaiList.clear();
- if (charText.length() == 0) {
- pegawaiList.addAll(pegawaiListBackup);
- } else {
- for (int i = 0; i < pegawaiListBackup.size(); i++){
- String nama = pegawaiListBackup.get(i).get(konfigurasi.TAG_NAMA).toLowerCase();
- // System.out.println(pegawaiListBackup.get(i).get(konfigurasi.TAG_NAMA));
- if (nama.contains(charText)){
- pegawaiList.add(pegawaiListBackup.get(i));
- }
- }
- }
- notifyDataSetChanged();
- }
- }
- }
Add Comment
Please, Sign In to add comment