简体   繁体   English

使用SimpleAdapter Android将标头添加到列表视图

[英]Add a header to a listview with SimpleAdapter android

I'm trying to show header on my ListView with SimpleAdapter. 我正在尝试使用SimpleAdapter在ListView上显示标题。 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) 编辑 :我想按类别(而不是标题)显示我的列表(“ lugar”作为类别的文本)

Note: I did not use " http://example.com/myjson.json " in my real code (only for publication). 注意:我没有在我的真实代码中使用“ http://example.com/myjson.json ”(仅用于发布)。

This is my code: 这是我的代码:

My json file content: 我的json文件内容:

{"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: Java代码:

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: 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 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? 我需要做什么才能将“ lugar”显示为类别文本,而将“ nombre”显示为项目?

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. 这样,当您滚动滚动时就可以对其进行动画处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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