java – How to put Letter Section on Top of Cardview by Alphabetically?-ThrowExceptions

Exception or error:

i have a list of cardview, and i need to sort it alphabetically with letter section on top.

it will be look like this.

--------------------
A
--------------------
Alpha cardview
Aerial cardview


--------------------
B
--------------------
Beta cardview
Bunial cardview

the cardview should look like that, that is the idea, what i have right now is i can already display the cardview but without the sort and alphabetical section in top.

here is my current code:

Fragment_activity.xml


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp">

            <ImageView
                android:id="@+id/merchantImageView"
                android:layout_width="69dp"
                android:layout_height="70dp"
                android:padding="4dp" />

            <TextView
                android:id="@+id/merchantName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/merchantImageView"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                android:textColor="#000000"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/merchantDesc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/merchantName"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"
                android:layout_toRightOf="@id/merchantImageView"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />
        </RelativeLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

Fragment_activtiy.java


   public void loadDeals(){

        final Constant WebConfig = new Constant();

        queue = Volley.newRequestQueue(getContext());

        CustomRequest stringRequest = new CustomRequest(Request.Method.POST, WebConfig.api, null, new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response)
            {

                try {

                    JSONArray details = response.getJSONArray("details");

                    dealsList.clear();

                    for (int i=0; i<details.length(); i++) {

                        JSONObject object = details.getJSONObject(i);

                        dealsList.add(new DealsModel(
                                object.getInt("id"),
                                object.getString("name"),
                                object.getString("title"),
                                object.getString("desc"),
                                object.getString("image")

                        ));


                        adapter.notifyDataSetChanged();
                        swipeRefreshLayout.setRefreshing(false);

                        loading.setVisibility(View.GONE);
                        dealsRecyclerView.setVisibility(View.VISIBLE);
                        cons.setVisibility(View.VISIBLE);

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){

            //This is for Headers If You Needed
            @Override
            public Map< String, String > getHeaders() throws AuthFailureError {
                Map < String, String > params = new HashMap<>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                params.put("X-API-KEY", WebConfig.test);
                params.put("Authorization", WebConfig.test1);
                return params;
            }


            @Override
            protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {

                try {
                    Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
                    if (cacheEntry == null) {
                        cacheEntry = new Cache.Entry();
                    }
                    final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
                    final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
                    long now = System.currentTimeMillis();
                    final long softExpire = now + cacheHitButRefreshed;
                    final long ttl = now + cacheExpired;
                    cacheEntry.data = response.data;
                    cacheEntry.softTtl = softExpire;
                    cacheEntry.ttl = ttl;
                    String headerValue;
                    headerValue = response.headers.get("Date");
                    if (headerValue != null) {
                        cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    headerValue = response.headers.get("Last-Modified");
                    if (headerValue != null) {
                        cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    cacheEntry.responseHeaders = response.headers;
                    final String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    return Response.success(new JSONObject(jsonString), cacheEntry);
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (JSONException e) {
                    return Response.error(new ParseError(e));
                }
            }

            @Override
            protected void deliverResponse(JSONObject response) {
                super.deliverResponse(response);
            }

            @Override
            public void deliverError(VolleyError error) {
                super.deliverError(error);
            }

            @Override
            protected VolleyError parseNetworkError(VolleyError volleyError) {
                return super.parseNetworkError(volleyError);
            }
        };
        queue.add(stringRequest);
    }

How can i implement the alpahebtical list of section for each cardview? thanks for any help, i will really appreciate it.

https://blog.tallan.com/2014/01/31/android-letter-section-listview/

here is the link that i want to achieve but without using any libraries from it.
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *