2
我一直試圖在從服務器獲取數據的片段中創建ListView,但迄今爲止都不成功。我一直在使用以下網站:Vogella瞭解如何在片段中實現ListView,但是我在嘗試從服務器填充數據時遇到困難。在從服務器檢索數據的片段中創建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和適配器,任何幫助將不勝感激。
@Rob在這一點上,你正在提出有關編程和Java的基本問題。最簡潔的答案是不。你的ArrayList的類型是HashMap,並且由於json是一個JSONObject,所以它不能被添加到你的ArrayList中。似乎你在試圖遵循教程時正在克服自己的侷限性。我的建議是花一些時間瞭解一般的Collections和編程,然後再次嘗試教程。 –
LuxuryMode
2013-03-04 00:55:16
我已更新我的代碼以反映更改。 – Rob 2013-03-04 02:38:36