Android 为什么我的ListView不使用SimpleAdapter更新?
我正在从我的服务器检索JSON数据,我想用它更新我的ListView。检索数据时一切都很好,但在完成并尝试更新列表后,我只得到一个没有错误的空页面Android 为什么我的ListView不使用SimpleAdapter更新?,android,json,android-listview,simpleadapter,Android,Json,Android Listview,Simpleadapter,我正在从我的服务器检索JSON数据,我想用它更新我的ListView。检索数据时一切都很好,但在完成并尝试更新列表后,我只得到一个没有错误的空页面Log.d(“ListLocations”、“after”)也可以运行,因此我看不到SimpleAdapter的PostExecute和初始化有任何问题 我已经修改了本教程中的代码: 这是我的ListActivity类: public class ListLocations extends ListActivity { //Root url a
Log.d(“ListLocations”、“after”)
也可以运行,因此我看不到SimpleAdapter的PostExecute和初始化有任何问题
我已经修改了本教程中的代码:
这是我的ListActivity类:
public class ListLocations extends ListActivity
{
//Root url and controller
private static String root = "someURL/";
private static String locationsController = "locations";
// Progress Dialog
private ProgressDialog pDialog;
//JSON node names for locations
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_TERRAIN = "terrain";
private static final String TAG_DIFFICULTY = "difficulty";
private static final String TAG_RATINGS = "ratings";
private static final String TAG_UID = "uid";
private static final String TAG_LONG= "long";
private static final String TAG_LAT= "lat";
private static final String TAG_DISTANCE= "distance";
private static final String TAG_COMID= "comid";
//Will be used to contain the list of locations extracted from JSON
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_locations);
new GetLocations().execute(root + locationsController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_list_locations, menu);
return true;
}
class GetLocations extends AsyncTask<String, Void, Void>
{
// private ProgressDialog pDialog;
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(ListLocations.this);
pDialog.setMessage("Loading locations. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
public Void doInBackground(String... urls)
{
try
{
JSONHandler jsonHandler = new JSONHandler();
JSONObject json= jsonHandler.getJSONFromUrl(urls[0]);
Log.d("ListLocations",json.toString());
JSONArray locations = json.getJSONArray("locations");
int length = locations.length();
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < length; i++)
{
JSONObject loc = locations.getJSONObject(i);
//Create HashMap
HashMap<String, String> map = new HashMap<String, String>();
//Get and store values into map
map.put("id", loc.getString(TAG_ID));
map.put("name", loc.getString(TAG_NAME));
map.put("distance", loc.getString(TAG_DISTANCE));
map.put("difficulty", loc.getString(TAG_DIFFICULTY));
map.put("terrain", loc.getString(TAG_TERRAIN));
map.put("ratings", loc.getString(TAG_RATINGS));
map.put("long", loc.getString(TAG_LONG));
map.put("lat", loc.getString(TAG_LAT));
map.put("uid", loc.getString(TAG_UID));
map.put("comid", loc.getString(TAG_COMID));
//Add map to list
list.add(map);
}
System.out.println(list);
}
catch (Exception e)
{
e.printStackTrace();
}
Log.d("ListLocations", "Successfully finished HTTP Request");
return(null);
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
Log.d("ListLocations", "inside run");
ListAdapter adapter = new SimpleAdapter(
ListLocations.this, list,
R.layout.list_item, new String[] { TAG_ID,
TAG_NAME},
new int[] { R.id.id, R.id.name });
Log.d("ListLocations", "after");
// updating listview
setListAdapter(adapter);
}
});
}
}
}
这是activity\u list\u locations.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
您从未将
活动
中的数据放入与SimpleAdapter
一起使用的列表
字段,因为doInBackground
方法中的列表
变量是局部变量,而不是字段。而不是:
//...
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < length; i++) {
//...
/。。。
ArrayList=新建ArrayList();
for(int i=0;i
写:
//...
list = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < length; i++) {
//...
/。。。
列表=新的ArrayList();
for(int i=0;i
因此,您不需要创建局部变量并将元素放在正确的列表中。
onPostExecute()
在UI线程中运行,因此,runOnUIThread()
是多余的。如果可以的话,我应该在哪里调用runOnUIThread?@JohnathanAu您现在是信徒吗?:)我觉得这一切都结束了,世界就像是我的牡蛎。
//...
list = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < length; i++) {
//...