我从网络服务器上获取了一个PHP脚本作为JSON数组。我想在Android Hive enter link description here
中的本教程中查看列表视图中的数据首先,应用程序没有响应,但现在它正在工作但未在列表视图中填充。
这是我的代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class Event extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> eventList;
private static String url_all_events = "http://examplesite.lk/sports/data.php";
// JSON Node names
private static final String TAG_ID = "id";
private static final String TAG_EVENTS = "events";
private static final String TAG_SID = "sportsid";
private static final String TAG_VS = "vs";
private static final String TAG_DATE = "date";
private static final String TAG_VENUE = "venue";
// products JSONArray
JSONArray events = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_events);
eventList = new ArrayList<HashMap<String, String>>();
new LoadAllEvents().execute();
}
class LoadAllEvents extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Event.this);
pDialog.setMessage("Loading events. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_events, "GET", params);
Log.d("All Products: ", json.toString());
try {
events = json.getJSONArray(TAG_EVENTS);
for (int i = 0; i < events.length(); i++) {
JSONObject c = events.getJSONObject(i);
String id = c.getString(TAG_ID);
String sid = c.getString(TAG_SID);
String vs = c.getString(TAG_VS);
String date = c.getString(TAG_DATE);
String venue = c.getString(TAG_VENUE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_SID, sid);
map.put(TAG_VS, vs);
map.put(TAG_DATE, date);
map.put(TAG_VENUE, venue);
eventList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
Event.this, eventList,
R.layout.list_item, new String[] { TAG_VS,
TAG_DATE},
new int[] { R.id.opponent, R.id.time });
setListAdapter(adapter);
}
});
}
}
}
主要列表视图是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
,列表项布局为:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/borders"
android:layout_margin="5dp"
>
<TextView
android:id="@+id/opponent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_color"
android:textSize="14sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/vs" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/school"
android:layout_centerHorizontal="true"
android:textColor="@color/text_color"
android:textSize="42sp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp" />
</RelativeLayout>
Json数组是
{"events":[{"id":"2","sportid":"1","vs":"CollegeA","date":"2014-05-02 17:30:00","venue":"Grounds"},{"id":"1","sportid":"1","vs":"CollegeB","date":"2014-04-17 17:30:00","venue":"Grounds"}]}
谁能告诉我哪里出错了。以及为什么数据不在列表视图中生成
答案 0 :(得分:0)
我知道错误。我的服务器中存在一些错误和一些语法错误。无论如何谢谢你们。 只需将此代码添加到工作
ListView lv = getListView();