ListAdapter不显示信息

时间:2015-08-05 21:40:38

标签: java android json

有人可以帮我解决这个问题吗?无法在我的ListView中获取“lugar”字符串。我不知道我做错了什么:(

我是新来的,也在开发Android应用程序。

最近开了一个问题:

https://stackoverflow.com/questions/31840052/why-the-result-always-be-null-while-parsing-json-java

这个问题已经解决,但我有一个新问题。 :(

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");

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

                        oslist.add(map);

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

                        ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                                R.layout.list_item,
                                new String[]{lugar}, 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("lugar"), 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>

1 个答案:

答案 0 :(得分:0)

你应该这样做:

String[] from = new String[]{ "lugar" };
int[] to = new int[]{ R.id.nombre };

ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                                R.layout.list_item,
                                from, to);

from子句应该包含列的名称,在本例中是HashMap中的键。