0

I'm trying to show header on my ListView with SimpleAdapter. I want to do something like this (image).

Edit: I want to show my list by categories (not header) ("lugar" as the text of categories)

Note: I did not use "http://example.com/myjson.json" in my real code (only for publication).

This is my code:

My json file content:

{"json":[{"lugar":"lugar 1","lista":[{"id":"1","nombre":"nombre 1"},{"id":"2","nombre":"nombre 2"}]},{"lugar":"lugar 2","lista":[{"id":"1","nombre":"nombre 1"},{"id":"2","nombre":"nombre 2"}]}]}

Java Code:

private class JSONParse extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog pDialog;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Espere...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        @Override
        protected JSONObject doInBackground(String... args) {

            JsonParser jParser = new JsonParser();
            JSONObject json;

            // Getting JSON from URL
            json = jParser.getJSONFromUrl("http://example.com/myjson.json");

            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {

            pDialog.dismiss();

            if(json != null) {

                try {
                    // obtener arrays
                    JsonArr = json.getJSONArray("json");

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

                        JSONObject c = JsonArr.getJSONObject(i);
                        String lugar = c.getString("lugar");

                       JsonArr2 = JsonArr.getJSONObject(i).getJSONArray("lista");
                        for (int j = 0; j < JsonArr2.length(); j++) {

                          JSONObject d = JsonArr2.getJSONObject(j);
                        String id = d.getString("id");
                        String nombre = d.getString("nombre");

                        // Adding value HashMap key => value
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("id", id);
                        map.put("nombre", nombre);

                        oslist.add(map);

                        list = (ListView) getView().findViewById(R.id.lista);

                        ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                                R.layout.list_item,
                                new String[]{"nombre"}, new int[]{
                                R.id.nombre});
                        list.setAdapter(adapter);
                        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Toast.makeText(getActivity(), "You Clicked at " + oslist.get(+position).get("id"), Toast.LENGTH_SHORT).show();
                            }
                        });




                         }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }else{
                Toast.makeText(getActivity(), "json: null", Toast.LENGTH_SHORT).show();
            }
        }

lista_medios.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:id="@+id/lista"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/abc_list_item_padding_horizontal_material">
    </ListView>

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/nombre"
        android:textSize="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

What I need to do to display "lugar" as Categories text and "nombre" for items?

Community
  • 1
  • 1
Arnaldo
  • 673
  • 6
  • 22
  • >I want to do something like this. Why don't you do it? It seems like there's plenty of options given in the answers to that question. – fweigl Aug 06 '15 at 00:18
  • Yes, but I do not want to change my code or use other options (as I read the answers ) . And I'm noy expert in java programming. I have seen and tried this: [link](https://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View, java.lang.Object, boolean)) Placing before `list.setAdapter(adapter);` but I get no result :( – Arnaldo Aug 06 '15 at 00:32
  • Well, it'll be real hard to change the behaviour of your app without changing your code ;) – fweigl Aug 06 '15 at 00:47
  • I did not mean that @Ascorbin, I want to know is what option I have ( most simple ) for not starting from scratch. – Arnaldo Aug 06 '15 at 00:55
  • Probably I am not getting what you mean by 'don't want to change my code', but if you want headers with list, then you must change the underlying adapter, if that is fine with you, this is your answer - http://stackoverflow.com/a/4777306/609782, which will lead to this tutorial - http://android.amberfog.com/?p=296 – Darpan Aug 06 '15 at 04:21

1 Answers1

0

To give a header to a listview i imagine you must extend the listview not the adapter. That way when your scrolling animate it in or out.

Daedalus
  • 255
  • 1
  • 2
  • 9