2

我一直在尝试在从服务器获取数据的片段中创建一个 ListView,但到目前为止还没有成功。我一直在使用以下站点:Vogella用于学习如何在片段中实现 ListView,但是在尝试从服务器填充数据时遇到了困难。

package com.example.ips_alpha;

import java.util.ArrayList;
import java.util.HashMap;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;

public class Gold_Visitors2 extends ListFragment{

//JSON Node names (level, sensorID, value)
String SPACES = "spaces"; //String array name of php
String LEVEL = "level";
String SENSORID = "sensorID";
String VALUE = "value";
//pspace JSONArray
JSONArray pspace = null;
String[] levels = new String[]{LEVEL, SENSORID, VALUE};

int [] ids = new int[]{};

private static String url = "http://www.example.com/getUsers12.php";


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_gold_visitors, null);


    new MyAsyncTask().execute();
    return view;

}

class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {


    //Hashmap for ListView
    ArrayList<HashMap<String, String>> levelList = new ArrayList<HashMap<String, String>>();

    @Override
    protected ArrayList<HashMap<String, String>>doInBackground(String... params) {

//Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        //getting JSON string from URL

        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            //Getting Array of spaces
            pspace = json.getJSONArray(SPACES);

            //looping through all spaces
            for(int i = 0; i < pspace.length(); i++){
                JSONObject p = pspace.getJSONObject(i);

            //Storing each json item in variable
                String level = p.getString(LEVEL);
                String sensorID = p.getString(SENSORID);
                String value = p.getString(VALUE);
            //creating new HashMap

                HashMap<String, String> map = new HashMap <String, String>();
            //adding each child node to HashMap Key =>
                map.put(LEVEL, level);
                map.put(SENSORID, sensorID);
                map.put(VALUE, value);

            //adding HashList to ArrayList
            }
        }catch(JSONException e){
            e.printStackTrace();

        }
        return levelList;

    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> levelList) {

        ListAdapter adapter = new SimpleAdapter(getActivity(), levelList, R.layout.fragment_gold_visitors, levels, ids);
        setListAdapter(adapter);


    }


}
}

以下是布局文件:

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

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

目前我的应用程序正在编译并且不会崩溃,但是在访问应该显示 ListView 的片段时,它是空白的。我相信我在 ASyncTask 和适配器中搞砸了,任何帮助将不胜感激。

4

1 回答 1

0

您返回levelListin doInBackground,但从未从检索到的 JSON 中向其中添加任何项目。你所拥有的只是一个空的ArrayList,所以ListView是空的。

于 2013-03-04T00:39:00.040 回答