Wednesday, 6 September 2017

Search Data From Recycle View Using Volley

Menu.File
volly_menu.


<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item        android:id="@+id/actiob_search_item"        android:icon="@android:drawable/ic_search_category_default"        android:title="Search"        app:showAsAction="ifRoom|collapseActionView"        app:actionViewClass="android.support.v7.widget.SearchView"/>


</menu>


Xml File

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context=".recVi.ViewClass"    tools:showIn="@layout/activity_user_data">



    <android.support.v7.widget.RecyclerView        android:id="@+id/rview"        android:layout_width="match_parent"        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>




Java Class

public class UserData extends AppCompatActivity implements SearchView.OnQueryTextListener {

    private ArrayList<Getdata> list = new ArrayList<>();
    private RecyclerView recyclerView;
    private DataAdpter dataAdpter;
    private ProgressDialog pDialog;


    String JSON_NAME = "Name";
    String JSON_EMAIL = "EmailId";
    String JSON_NO = "MobileNo";
    String JSON_GENDER = "Gender";

    private String lat, log;
    String id;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        list = new ArrayList<>();

        if (id != null) {
            id = getIntent().getStringExtra("$id");
        }


        recyclerView = (RecyclerView) findViewById(R.id.rview);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        GetDetails();
    }

    private void GetDetails() {
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {

            jsonObject.put("RegistrationId", 0);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                "http://aeps.gear.host/api/Test/GetDetails", jsonObject, new Response.Listener<JSONObject>() {
            @Override            public void onResponse(JSONObject response) {
                try {
                    String Status = response.getString("Status");

                    JSONArray jsonArray = response.getJSONArray("Data");
                    for (int i = 0; i < jsonArray.length(); i++) {

                        Getdata GetDataAdapter2 = new Getdata();

                        try {
                            JSONObject json = jsonArray.getJSONObject(i);
                            GetDataAdapter2.setName(json.getString(JSON_NAME));

                            GetDataAdapter2.setEmailId(json.getString(JSON_EMAIL));

                            GetDataAdapter2.setMobileNo(json.getString(JSON_NO));

                            GetDataAdapter2.setGender(json.getString(JSON_GENDER));

                            lat = json.getString("Latitude");
                            log = json.getString("Longitude");


                        } catch (JSONException e) {

                            e.printStackTrace();
                        }
                        list.add(GetDataAdapter2);
                    }
                    dataAdpter = new DataAdpter(list);
                    recyclerView.setAdapter(dataAdpter);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                pDialog.dismiss();
            }
        },
                new Response.ErrorListener() {
                    @Override                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(UserData.this, error.toString(), Toast.LENGTH_LONG).show();
                        pDialog.hide();
                    }
                });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonObjReq);
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.volly_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.actiob_search_item);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        searchView.setOnQueryTextListener(this);


        return true;
    }

    @Override    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override    public boolean onQueryTextChange(String newText) {
        newText = newText.toLowerCase();
        ArrayList<Getdata> newList = new ArrayList<>();
        for (Getdata getdata : list) {
            String name = getdata.getName().toLowerCase();
            String gender = getdata.getGender().toLowerCase();
            if (name.contains(newText) || gender.contains(newText))
                newList.add(getdata);
        }
        dataAdpter.setFilter(newList);

        return true;
    }
}




Adapter Class 

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

    ArrayList<Getdata> arrayList = new ArrayList<>();

    public DataAdpter(ArrayList<Getdata> arrayList) {
        this.arrayList = arrayList;

    }

    @Override    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adpter, parent, false);
        return new ViewHolder(v);
    }

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

        Getdata getDataAdapter1 = arrayList.get(position);

        holder.textView_name.setText(getDataAdapter1.getName());

        holder.textView_email.setText(String.valueOf(getDataAdapter1.getEmailId()));

        holder.Mobile.setText(getDataAdapter1.getMobileNo());

        holder.gender.setText(getDataAdapter1.getGender());


    }

    @Override    public int getItemCount() {

        return arrayList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textView_name;
        public TextView textView_email;
        public TextView Mobile;
        public TextView gender;


        public ViewHolder(View itemView) {

            super(itemView);

            textView_name = (TextView) itemView.findViewById(R.id.textView_name);
            textView_email = (TextView) itemView.findViewById(R.id.textView_email);
            Mobile = (TextView) itemView.findViewById(R.id.Mobile);
            gender = (TextView) itemView.findViewById(R.id.gender);

        }
    }

    public void setFilter(ArrayList<Getdata> newList) {

        arrayList = new ArrayList<>();
        arrayList.addAll(newList);
        notifyDataSetChanged();

    }

}


Adapter xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:card_view="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@android:color/white"    android:orientation="vertical">

    <android.support.v7.widget.CardView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="5dp">

        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_margin="5dp"            android:orientation="vertical">


            <LinearLayout                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginTop="5dp"                android:orientation="horizontal">


                <TextView                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight=".6"                    android:text="Name : "                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView                    android:id="@+id/textView_name"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text=""                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>

            <LinearLayout                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView                    android:id="@+id/textView3"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight=".6"                    android:text="Email : "                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView                    android:id="@+id/textView_email"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text=""                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>

            <LinearLayout                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <TextView                    android:id="@+id/textView5"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight=".6"                    android:text="Mobile : "                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView                    android:id="@+id/Mobile"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text=""                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>

            <LinearLayout                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginTop="5dp"                android:orientation="horizontal">

                <TextView                    android:id="@+id/textView7"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight=".6"                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView                    android:id="@+id/gender"                    android:layout_width="0dp"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text=""                    android:textAppearance="?android:attr/textAppearanceLarge" />

            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

No comments:

Post a Comment