简体   繁体   中英

Add a header to a listview with SimpleAdapter android

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM