有人可以告诉我我的代码有什么问题。我无法将数据库中的数据检索到listview中。 Logcat说"公告没有价值"。请新来的。提前谢谢
Announcement.java
package com.example.androidtablayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class Announcements extends ListActivity {
// Variables
JSONArray jsonarray;
ListView listview;
ArrayList<HashMap<String, String>> arraylist;
ProgressDialog mProgressDialog;
JSONParser jsonParser = new JSONParser();
String email;
SessionManager session;
String[] services;
Button btnLogout;
String username;
private String url = "http://10.0.3.2/sunshine-ems/announcement.php";
// ALL JSON node names
private static final String ANNOUNCEMENT_ID = "announcement_id";
private static final String ANNOUNCEMENT_DATE = "date";
private static final String ANNOUNCEMENT_TITLE = "title";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.announc);
session = new SessionManager(getApplicationContext());
username = session.getUsername();
// get listview
ListView lv = getListView();
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
Intent i = new Intent(getApplicationContext(),Single_List.class);
String transaction_id = ((TextView) view.findViewById(R.id.transac_id)).getText().toString();
i.putExtra("announcement_id", transaction_id);
startActivity(i);
}
});
btnLogout = (Button) findViewById(R.id.btnLogout);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching All products Activity
session.logoutUser();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(Announcements.this);
mProgressDialog.setTitle("Loading Services");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected String doInBackground(String... params) {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("username", username));
JSONObject json = jsonParser.makeHttpRequest(url, "POST", params1);
Log.e("Check JSON ", json.toString());
// Create the array
arraylist = new ArrayList<HashMap<String, String>>();
try {
int success = json.getInt("success");
if (success == 1) {
// Locate the array name
jsonarray = json.getJSONArray("announcements");
for (int i = 0; i < jsonarray.length(); i++) {
json = jsonarray.getJSONObject(i);
String a_id = json.optString(ANNOUNCEMENT_ID);
String a_date = json.getString(ANNOUNCEMENT_DATE);
String a_title = json.getString(ANNOUNCEMENT_TITLE);
// Retrive JSON Objects
HashMap<String, String> map = new HashMap<String, String>();
map.put(ANNOUNCEMENT_ID, a_id);
map.put(ANNOUNCEMENT_TITLE, a_title);
map.put(ANNOUNCEMENT_DATE, a_date);
// Set the JSON Objects into the array
arraylist.add(map);
}
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String file_url) {
mProgressDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
Announcements.this, arraylist,
R.layout.listview_services, new String[] {
ANNOUNCEMENT_ID, ANNOUNCEMENT_TITLE, ANNOUNCEMENT_DATE },
new int[] { R.id.transac_id, R.id.txt_service,
R.id.txt_date });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
announcement.php
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$username = $_POST['username'];
$query_dept = mysql_query("SELECT department FROM accounts WHERE username = $username") or die(mysql_error());
$row = mysql_fetch_assoc($query_dept);
$dept = $row['department'];
$result = mysql_query("SELECT * FROM announcement WHERE receiver=$dept") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["announcements"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$announcement = array();
$announcement["announcement_id"] = $row["announcement_id"];
$announcement["title"] = $row["title"];
$announcement["date"] = $row["date"];
// push single product into final response array
array_push($response["announcements"], $announcement);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No Announcement found";
// echo no users JSON
echo json_encode($response);
}
?>
答案 0 :(得分:0)
“SELECT * FROM announcement WHERE receiver ='”。$ dept。“';”
检查查询是否正确执行