在片段中创建从服务器检索数据的ListView

时间:2013-03-04 00:11:46

标签: android listview android-asynctask fragment android-listfragment

我一直在尝试在从服务器获取数据的片段中创建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和适配器,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您在levelList中返回doInBackground,但您从未从检索到的JSON中添加任何项目。您拥有的只是空ArrayList,因此ListView为空。