Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

android - How To Implement Getfilter () On A Custom Adapter

I want recyclerview to display the data contained in my database. I'm confused about what to declare in getfilter () if it's created in my adapter file. if there are other suggestions how to solve this problem I will be very grateful.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    RecyclerView recView;
    TextView textView;
    ArrayList<HashMap<String, String>> arrayList;
    Dialog dialog;

    public static final String url = "http://xxx/cobalistuser/menu.php";

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

        textView = findViewById(R.id.textView);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(MainActivity.this);

                dialog.setContentView(R.layout.dialog_searchable_spinner);

                dialog.getWindow().setLayout(850, 1000);

                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

                dialog.show();

                EditText editText = dialog.findViewById(R.id.editText);

                var();

                RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);

                arrayList = new ArrayList<>();

                StringRequest stringRequest = new StringRequest(Request.Method.GET, url, response -> {
                    Log.d("response ", response);
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("users");
                        for (int a = 0; a < jsonArray.length(); a++) {
                            JSONObject json = jsonArray.getJSONObject(a);

                            HashMap<String, String> map = new HashMap<>();
                            map.put("id_users", json.getString("id_users"));
                            map.put("username", json.getString("username"));
                            map.put("user_type", json.getString("user_type"));
                            arrayList.add(map);

                            AdapterList adapter = new AdapterList(MainActivity.this, arrayList);
                            recView.setAdapter(adapter);

                            editText.addTextChangedListener(new TextWatcher() {
                                @Override
                                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                }

                                @Override
                                public void onTextChanged(CharSequence s, int start, int before, int count) {
                                    adapter.getFilter().filter(s);
                                }

                                @Override
                                public void afterTextChanged(Editable s) {

                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }, error -> Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show());

                requestQueue.add(stringRequest);



            }
        });
    }

    private void var() {


        RecyclerView recView = dialog.findViewById(R.id.recView);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recView.setLayoutManager(llm);
    }


}

AdapterList.java

public class AdapterList extends RecyclerView.Adapter<AdapterList.ViewHolder> {

    Context context;
    ArrayList<HashMap<String, String>> arrayList;

    public AdapterList(MainActivity mainActivity, ArrayList<HashMap<String, String>> arrayList) {
        this.context = mainActivity;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        // Get position of ViewHolder class
        holder.user_type.setText(arrayList.get(position).get("user_type"));
        holder.username.setText(arrayList.get(position).get("username"));
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }


    // This ViewHolder of RecycleView
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // deklarasi variabel yang ada di row.xml
        TextView username, user_type;

        public ViewHolder(View itemView) {
            super(itemView);

            username = itemView.findViewById(R.id.username);
            user_type = itemView.findViewById(R.id.user_type);
        }
    }
}

Actually I have difficulty implementing the searchable spinner (toptoche), so I switched to editText which includes a listview based on tutorials from the internet. but I faced difficulties

question from:https://stackoverflow.com/questions/65884631/how-to-implement-getfilter-on-a-custom-adapter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...