2

sorry if the explanation is incomplete. I just learned about Android. and in my project this time, I made a filter feature using the radio button

I followed a totorial, but the tutorial uses static data, then I change it to dynamic using my data in the database. all works !! the data appears

but when I type something in the search bar that I make, suddenly my data disappears, I make 3 radio buttons to be selected by the user to be searched / filtered, namely type, price and facilities. the three radio buttons don't work, my data that appears are gone.

How to handle that problem?? if anyone can help me please help, thank you

this is the code

this fragment

public class FilterFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

private static final String data_url = "http://000.000.00.000/kos/get_kos.php";

RecyclerView mRecyclerView;
ProgressDialog pd;
private Context context;
private RecyclerViewAdapter adapter;
private ArrayList<UserModel> arrayList;
private RadioGroup searchViaRadioGroup;
private EditText searchEditText;
private TextView searchViaLabel;

/*  Filter Type to identify the type of Filter  */
private FilterType filterType;

/*  boolean variable for Filtering */
private boolean isSearchWithPrefix = false;

public FilterFragment() {
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_filter, container, false);

    pd = new ProgressDialog(getActivity());
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    arrayList = new ArrayList<>();
    mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), LinearLayoutManager.VERTICAL));
    adapter = new RecyclerViewAdapter(getActivity(), arrayList);
    mRecyclerView.setAdapter(adapter);

    loadjson();

    return view;

}

private void loadjson(){
    pd.setMessage("Mengambil Data");
    pd.setCancelable(false);
    pd.show();

    JsonArrayRequest arrayRequest = new JsonArrayRequest(Request.Method.POST, data_url, null, new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            pd.cancel();
            Log.d("volley", "response : " + response.toString());
            for (int i=0; i < response.length(); i++)
                try {
                    JSONObject data = response.getJSONObject(i);
                    UserModel md = new UserModel();
                    md.setJudul(data.getString("judul")); // memanggil nama array yang kita buat
                    md.setAlamat(data.getString("alamat"));
                    md.setHarga(data.getString("harga"));
                    md.setTipe_kos(data.getString("tipe_kos"));
                    md.setFasilitas(data.getString("fasilitas"));

                    arrayList.add(md);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            pd.cancel();
            Log.d("volley", "error : " + error.getMessage());
        }
    });
    Controller.getInstance().addToRequestQueue(arrayRequest);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    findViews(view);
    implementEvents();
}

//Bind all Views
private void findViews(View view) {
    filterType = FilterType.TIPE_KOS;
    searchViaRadioGroup = (RadioGroup) view.findViewById(R.id.search_via_radio_group);
    searchEditText = (EditText) view.findViewById(R.id.search_text);
    searchViaLabel = (TextView) view.findViewById(R.id.search_via_label);

}

//Populate recycler view

private void implementEvents() {
    searchViaRadioGroup.setOnCheckedChangeListener(this);
    searchViaLabel.setOnClickListener(this);

    searchEditText.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) {

            //On text changed in Edit text start filtering the list
            adapter.filter(filterType, charSequence.toString(), isSearchWithPrefix);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}


@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
    int pos = radioGroup.indexOfChild(radioGroup.findViewById(checkedId));//get the checked position of radio button
    switch (radioGroup.getId()) {
        case R.id.search_via_radio_group:
            switch (pos) {
                case 0:
                    filterType = FilterType.TIPE_KOS;//Change filter type to Name if pos = 0
                    break;
                case 1:
                    filterType = FilterType.NUMBER;//Change filter type to Number if pos = 1
                    break;
                case 2:
                    filterType = FilterType.EMAIL;//Change filter type to Email if pos = 2
                    break;
            }
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.search_via_label:
            //show hide the radio group
            if (searchViaRadioGroup.isShown()) {
                searchViaLabel.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up_dropdown, 0);
                searchViaRadioGroup.setVisibility(View.GONE);
            } else {
                searchViaLabel.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down_dropdown, 0);
                searchViaRadioGroup.setVisibility(View.VISIBLE);
            }
            break;
    }
}

}

this adapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder> {

static class RecyclerViewHolder extends RecyclerView.ViewHolder {

    private TextView judul, alamat, tipe_kos, fasilitas, harga;

    RecyclerViewHolder(View view) {
        super(view);

        judul = (TextView) view.findViewById(R.id.judul);
        alamat = (TextView) view.findViewById(R.id.alamat);
        tipe_kos = (TextView) view.findViewById(R.id.tipe_kos);
        fasilitas = (TextView) view.findViewById(R.id.fasilitas);
        harga = (TextView) view.findViewById(R.id.harga);

    }

}

private ArrayList<UserModel> arrayList;
private ArrayList<UserModel> filterArrayList;//duplicate list for filtering
private Context context;


public RecyclerViewAdapter(Context context, ArrayList<UserModel> arrayList) {
    this.arrayList = arrayList;
    this.context = context;

    this.filterArrayList = new ArrayList<>();//initiate filter list
    this.filterArrayList.addAll(arrayList);//add all items of array list to filter list
}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_list_filter, viewGroup, false);
    return new RecyclerViewHolder(v);
}

@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int i) {

    final UserModel model = arrayList.get(i);

    holder.judul.setText(model.getJudul());//menampilkan data
    holder.alamat.setText(model.getAlamat());
    holder.harga.setText(model.getHarga());
    holder.tipe_kos.setText(model.getTipe_kos());
    holder.fasilitas.setText(model.getFasilitas());
}


@Override
public int getItemCount() {
    return (null != arrayList ? arrayList.size() : 0);
}


// Filter Class to filter data
public void filter(FilterType filterType, String charText, boolean isSearchWithPrefix) {

    //If Filter type is TIPE_KOS and EMAIL then only do lowercase, else in case of NUMBER no need to do lowercase because of number format
    if (filterType == FilterType.TIPE_KOS || filterType == FilterType.EMAIL)
        charText = charText.toLowerCase(Locale.getDefault());

    arrayList.clear();//Clear the main ArrayList

    //If search query is null or length is 0 then add all filterList items back to arrayList
    if (charText.length() == 0) {
        arrayList.addAll(filterArrayList);
    } else {

        //Else if search query is not null do a loop to all filterList items
        for (UserModel model : filterArrayList) {

            //Now check the type of search filter
            switch (filterType) {
                case TIPE_KOS:
                    if (isSearchWithPrefix) {
                        //if STARTS WITH radio button is selected then it will match the exact TIPE_KOS which match with search query
                        if (model.getTipe_kos().toLowerCase(Locale.getDefault()).startsWith(charText))
                            arrayList.add(model);
                    } else {
                        //if CONTAINS radio button is selected then it will match the TIPE_KOS wherever it contains search query
                        if (model.getTipe_kos().toLowerCase(Locale.getDefault()).contains(charText))
                            arrayList.add(model);
                    }

                    break;
                case EMAIL:
                    if (isSearchWithPrefix) {
                        //if STARTS WITH radio button is selected then it will match the exact EMAIL which match with search query
                        if (model.getFasilitas().toLowerCase(Locale.getDefault()).startsWith(charText))
                            arrayList.add(model);
                    } else {
                        //if CONTAINS radio button is selected then it will match the EMAIL wherever it contains search query
                        if (model.getFasilitas().toLowerCase(Locale.getDefault()).contains(charText))
                            arrayList.add(model);
                    }

                    break;
                case NUMBER:
                    if (isSearchWithPrefix) {
                        //if STARTS WITH radio button is selected then it will match the exact NUMBER which match with search query
                        if (model.getHarga().startsWith(charText))
                            arrayList.add(model);
                    } else {
                        //if CONTAINS radio button is selected then it will match the NUMBER wherever it contains search query
                        if (model.getHarga().contains(charText))
                            arrayList.add(model);
                    }

                    break;
            }

        }
    }
    notifyDataSetChanged();
}

}

this FilterType

public enum FilterType {
TIPE_KOS, NUMBER, EMAIL;

}

and this user model

package com.example.asus.myapplication.fragment.filter;

public class UserModel { private String judul, alamat, harga, tipe_kos, fasilitas;

public String getJudul() {
    return judul;
}

public String getAlamat() {
    return alamat;
}

public String getHarga() {
    return harga;
}

public String getTipe_kos() {
    return tipe_kos;
}

public String getFasilitas() {
    return fasilitas;
}


public void setJudul(String mJudul) {
    judul = mJudul;
}

public void setAlamat(String mAlamat) {
    alamat = mAlamat;
}

public void setHarga(String mHarga) {
    harga = mHarga;
}

public void setTipe_kos(String mTipe_kos) {
    tipe_kos = mTipe_kos;
}

public void setFasilitas(String mFasilitas) { fasilitas= mFasilitas;
}

}

another one I do not understand at all for this filter API, I just pulled all the data in my database

My database is like this, which I circle, that's the data I want to filter later

enter image description here

PowerStat
  • 3,757
  • 8
  • 32
  • 57

0 Answers0