我一直在处理由远程数据库填充的多级列表视图近一周,我仍然无法解决列表视图第二级的错误。
这是错误:
org.json.JSONException: No value for id
at org.json.JSONObject.get(JSONObject.java:354)
但它已经返回了我想要检索的正确数据。
{"success":1,"message":"Patient Available","post":[{"id":"29","date":"2014-02-06 21:15:29","description":"Healthy"}]}
这是我的第二级java类
History.java
private Button create;
private ProgressDialog pDialog;
private static final String READ_HISTORY_URL = "http://192.168.43.15:8080/DoctorScheduler/activities/history.php";
private static final String TAG_POST = "post";
private static final String TAG_DATE = "date";
private static final String TAG_DESC = "description";
private static final String TAG_ID = "id";
JSONParser jsonParser = new JSONParser();
JSONArray history = null;
String patient_id , remarks_id;
ArrayList<HashMap<String, String>> historyList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
//get patient_id
Intent i = getIntent();
patient_id = i.getStringExtra("id");
//hashmap for history listview
historyList = new ArrayList<HashMap<String, String>>();
new LoadHistory().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// INSERT ALL PREVIOUS CONSULTATIONS OF THE PATIENT HERE
Intent viewhistory = new Intent(History.this, Singlehistory.class);
String patient_id = ((TextView) view.findViewById(R.id.txtPid)).getText().toString();
String remarks_id = ((TextView) view.findViewById(R.id.txtRid)).getText().toString();
Toast.makeText(getApplicationContext(), "p_id: " + patient_id + ", r_id: " + remarks_id, Toast.LENGTH_LONG).show();
viewhistory.putExtra("patient_id", patient_id);
viewhistory.putExtra("remarks_id", remarks_id);
startActivity(viewhistory);
}
});
create = (Button) findViewById(R.id.BtnNew);
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent createInfo = new Intent(History.this, Addhistory.class);
startActivity(createInfo);
}
});
}
class LoadHistory extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(History.this);
pDialog.setMessage("Loading all patient consultations....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", patient_id));
String json = jsonParser.getJSONFromURL(READ_HISTORY_URL, "GET", params);
Log.d("Response: ", json);
try{
JSONObject jObj = new JSONObject(json);
if(jObj!=null){
String patient_id = jObj.getString(TAG_ID);
history = jObj.getJSONArray(TAG_POST);
if(history != null){
//looping through all patient's remarks
for(int i=0; i<history.length(); i++){
JSONObject c = history.getJSONObject(i);
//storing into json variable
String remarks_id = c.getString(TAG_ID);
String date = c.getString(TAG_DATE);
String description = c.getString(TAG_DESC);
//creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put("patient_id", patient_id);
map.put(TAG_ID, remarks_id);
map.put(TAG_DATE, date);
map.put(TAG_DESC, description);
historyList.add(map);
}
}else{
Log.d("History:", "null");
}
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
//super.onPostExecute(result);
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
History.this, historyList, R.layout.mainhistory,
new String[] {"patient_id", "TAG_ID", TAG_DATE, TAG_DESC},
new int[] {R.id.txtPid, R.id.txtRid, R.id.txtRdate, R.id.txtRdesc});
setListAdapter(adapter);
}
}
}
这是我的php文件
$query = "SELECT * FROM remarks WHERE patient_id = :id";
$query_params = array(':id' => $_GET['id']);
try{
$stmt = $dbname->prepare($query);
$result = $stmt->execute($query_params);
$id = $_GET["id"];
// check if p.patient_id = r.patient_id ($remarks' patient_id)
$query = "SELECT date, description FROM remarks WHERE patient_id = $id";
$q = $dbname->prepare($query);
$q->execute();
}catch(PDOException $ex){
$response["success"] = 0;
$response["message"] = $ex;
die(json_encode($response));
}
//retrieve all the rows
$rows = $stmt->fetchAll();
if($rows){
$response["success"] = 1;
$response["message"] = "Patient Available";
$response["post"] = array();
foreach($rows as $row){
$post = array();
$post["id"] = $row["patient_id"];
$post["date"] = $row["date"];
$post["description"]= $row["description"];
//update json response
array_push($response["post"], $post);
}
echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "No available patient's record";
die(json_encode($response));
}
我对listview的第一级没有任何问题,但每当我尝试检索我在第一个listview中点击的患者的所有评论时,它都不会在第二个列表视图中显示任何内容。