Daftar dan Belajar Sekarang!

Cara membuat method getfilter pada list adapter

foto hendrywijaya98
1 Kontribusi 0 Coto
6 bulan yang lalu
Saya ingin buat fitur search pada aplikasi mobile bernama medic-u , aplikasi di medis yang fitur utamanya mencari nama rumah sakit, puskesmas kemudian saya ingin menambah fitur search dengan menggunakan Edit Text pada ListView Pada tutorial dan documentation di internet, Adapter yang digunakan adalah Array Adapter, sedangkan pada Projek Saya menggunakan List Adapter, dan List yang di Cari itu ngambil data dari API url Jakarta Maka pada getFilter di dokumentasi hanya berlaku pada Array Adapter, maka disemua tutorial dan dokumentasi cuma ada Array Adapter Sedangkan yang Saya butuhkan disini adalah method getFilter untuk List Adapter Cara membuat method getFilter untuk List Adapter nya gimana ya? Tanpa harus ngubah Codingan List Adapter nya ke Adapter lain, soalny kalo program nya diubah malah tambah repot nantinya
package com.pemmoba.andikaandreashendrykennywillsen.medic_u;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Filter;
import java.util.logging.Handler;


public class RumahSakitMain extends AppCompatActivity {

    ProgressBar pbMain;
    ArrayList<HashMap<String, String>> listrs;
    //ori "http://api.jakarta.go.id/v1/rumahsakitumum/"
    //cadangan "https://api.jakarta.go.id/v1/rumahsakitumum/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30?format=geojson"
    String url = "http://api.jakarta.go.id/v1/rumahsakitumum/";
    SearchView svs;

    // Edit Search Text
    private EditText searchText;

    ListAdapter ladapter ;



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

        // date
        TextView tanggal = (TextView) findViewById(R.id.tanggal);
        SimpleDateFormat formattgl = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
        String currentDateandTime = formattgl.format(new Date());
        tanggal.setText("Penarikan Data Terakhir :\n"+currentDateandTime);

        // hospital search text
        searchText = (EditText)findViewById(R.id.edtSearch);

        // hospital list
        listrs = new ArrayList<>();
        final ListView lv = (ListView) findViewById(R.id.listViewDaftar);
        pbMain = (ProgressBar) findViewById(R.id.loadingBar);
        pbMain.setVisibility(View.VISIBLE);

        final AlertDialog.Builder dialog = new AlertDialog.Builder(RumahSakitMain.this);
        dialog.setCancelable(false);

        // refresh button
        Button btnRefreshRS = (Button)findViewById(R.id.btnRefreshRS);

        btnRefreshRS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent refresh = new Intent(getApplicationContext(), tungguRS.class);
                startActivity(refresh);
                RumahSakitMain.this.finish();
            }
        });

        View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
        lv.addFooterView(footerView, null, false);



        //Volley, untuk penarikan data dari API, week 11
        RequestQueue q = Volley.newRequestQueue(RumahSakitMain.this);
        StringRequest sr = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {


            @Override
            public void onResponse(String response) {
                // get JSON data, week 11
                try {
                   // String judul = "Rumah Sakit";
                    JSONObject obj = new JSONObject(response);
                    JSONArray data = obj.getJSONArray("data");
                    for (int i = 0 ; i < data.length(); i++) {
                        JSONObject a = data.getJSONObject(i);
                        String judul = a.getString("jenis_rsu");
                        String namars = a.getString("nama_rsu");
                        String id = a.getString("id");
                        String kode_pos = a.getString("kode_pos");
                        String email = a.getString("email");
                        String website = a.getString("website");


                        JSONObject b = a.getJSONObject("location");
                        String alamatrs = b.getString("alamat");

                        JSONArray c = a.getJSONArray("telepon");
                        String telepon = (String) c.get(0);

                        // Week 11
                        HashMap<String, String> map = new HashMap<>();
                        map.put("id", id);
                        map.put("namaRS", namars);
                        map.put("jenis", judul);
                        map.put("alamatRS", alamatrs);
                        map.put("kodepos", kode_pos);
                        map.put("email", email);
                        map.put("website", website);
                        map.put("telepon", telepon);
                        map.put("id", id);
                        listrs.add(map);
                    }
                    pbMain.setVisibility(View.INVISIBLE);

                    ladapter = new SimpleAdapter(RumahSakitMain.this, listrs, R.layout.layoutdaftar, new String[]{"jenis", "namaRS", "alamatRS", "telepon"}, new int[]{R.id.judulnya, R.id.txtNama, R.id.txtAlamat, R.id.txtTelepon});
                    lv.setAdapter(ladapter);

                    Toast.makeText(RumahSakitMain.this, "Penarikan Data Berhasil!", Toast.LENGTH_LONG).show();

                } catch (Exception e) {
                    Log.e("Traffic API padat ", e.toString());
                    pbMain.setVisibility(View.INVISIBLE);

                    RumahSakitMain.this.finish();
                    Intent traffic = new Intent(getApplicationContext(), trafficRS.class);
                    startActivity(traffic);
/*
                    dialog.setTitle("Kendala Teknis!");
                    dialog.setMessage("Penarikan data dari server pusat gagal!");
                    dialog.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //finish();
                            //System.exit(0);
                            Intent refresh = new Intent(getApplicationContext(), RumahSakitMain.class);
                            startActivity(refresh);
                        }
                    });
                    dialog.setNegativeButton("Kembali ke Menu Utama", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent utama = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(utama);
                        }
                    });

                    dialog.show();
                    */

                }

            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Internet tidak aktif", error.toString());

                RumahSakitMain.this.finish();

                Intent internet = new Intent(getApplicationContext(), internetRS.class);
                startActivity(internet);

                /*
                dialog.setTitle("Gagal Terhubung!");
                dialog.setMessage("Cek Koneksi Internet Anda!");
                dialog.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //finish();
                        //System.exit(0);
                        Intent refresh = new Intent(getApplicationContext(), RumahSakitMain.class);
                        startActivity(refresh);
                    }
                });
                dialog.setNegativeButton("Kembali ke Menu Utama", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent utama = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(utama);
                    }
                });

                dialog.show();
                */

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/geojson");
                params.put("Authorization", "Jii+apzvYMt2DWzwEYUvLM4ClHAH1JbE5ro/9eQYpOyvVoaykaOGzwHrU2s/Xyr8\n");
                return params;
            }
        };
        q.add(sr);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String nama = listrs.get(position).get("namaRS");
                String alamat = listrs.get(position).get("alamatRS");
                String telp = listrs.get(position).get("telepon");
                String web = listrs.get(position).get("website");
                String email = listrs.get(position).get("email");
                String judul = listrs.get(position).get("jenis");



                //detail fraga = new detail();
                //Bundle args = new Bundle();
                //args.putString("nama",nama);
                //args.putString("alamat",alamat);
                //args.putString("telp",telp);
                //args.putString("web",web);
                //args.putString("email",email);
                //fraga.setArguments(args);

                Intent keDetail = new Intent(RumahSakitMain.this, RumahSakitDetail.class);
                keDetail.putExtra("jenis", judul);
                keDetail.putExtra("nama", nama);
                keDetail.putExtra("alamat", alamat);
                keDetail.putExtra("telp", telp);
                keDetail.putExtra("web", web);
                keDetail.putExtra("email", email);
                startActivity(keDetail);

                //    FragmentManager fm = getSupportFragmentManager();
                //   FragmentTransaction ft = fm.beginTransaction();

                // ft.replace(R.id.frame,fraga);
                //ft.addToBackStack(null);
                //ft.commit();

                //Intent intent = new Intent(RumahSakitMain.this, RumahSakitDetail.class);
                //intent.putExtra("data", json.toString());
                //startActivity(intent);

            }
        });
searchText.addTextChangedListener(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) {
                // errornya ada di getFilter(),karena ini berwarna merah
                // disini pas disorot muncul pesan "cannot resolve method getFilter"
                RumahSakitMain.this.ladapter.getFilter().filter(charSequence);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }



    @Override
    public void onBackPressed() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(RumahSakitMain.this);
        dialog.setTitle("Menu ");
        dialog.setMessage("Apa yang ingin Anda lakukan?");
        dialog.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //finish();
                //System.exit(0);
                Intent refresh = new Intent(getApplicationContext(), tungguRS.class);
                startActivity(refresh);
                Toast.makeText(RumahSakitMain.this, "Refresh", Toast.LENGTH_LONG).show();
                RumahSakitMain.this.finish();
            }
        });
        dialog.setNegativeButton("Kembali ke Menu Utama", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent utama = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(utama);
                RumahSakitMain.this.finish();
            }
        });

        dialog.setNeutralButton("Batal", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(RumahSakitMain.this, "Batal", Toast.LENGTH_LONG).show();
            }
        }).show();
    }
}


Login untuk diksusi di forum sekolah koding