我有以下PHP代码,它将每三个条目添加为数组中的不同对象。
//Read the username and password from ServerRequest class
$username = $_POST["username"];
//Select all rows when the username match the input
$statement = mysqli_prepare($con, "SELECT * FROM Chore WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
//store the results of the previous query and create a variable for each column returned
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$chore_name ,$point_value, $parent_username, $child_username, $created, $points_given);
$chore = array();
//store the previous variables you created in the array (only the ones you want to use)
while(mysqli_stmt_fetch($statement)) {
$t = new stdClass();
$t->chore_name = $chore_name;
$t->child_username = $child_username;
$t->point_value = $point_value;
$chore[] = $t;
}
//encode the array in JSON format
echo json_encode($chore);
//close the statement
mysqli_stmt_close($statement);
//Close the database connection
mysqli_close($con);
但是我不确定如何将这些创建到活动中的列表视图中。我在xml文件中有这个
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/choreList"
android:textSize="20dp"
android:typeface="sans"
android:layout_centerHorizontal="true"
android:layout_below="@+id/currentChores"
android:paddingBottom="10dp"
android:paddingTop="50dp">
</ListView>
以下是我到目前为止的代码,但我非常确定它是完全错误的,并且不知道将代码的哪一部分放入其中。
Bundle extras = getIntent().getExtras();
final String user = extras.getString("username");
//The URL is the location of the PHP file to validate a user
String requestURL = SERVER_ADDRESS+"FetchChoreData.php";
URL url;
Chore returnedChore = null;
try {
//Opens the connection to the PHP files
//Sets the conditions of the connection
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(CONNECTION_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Opens an output stream to send the data to be verified
// and then closes the all output streams and flushes the output writer
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("username",user);
String query = builder.build().getEncodedQuery();
writer.write(query);
writer.flush();
writer.close();
os.close();
//saves the response code to ensure connection was succesful
int code = conn.getResponseCode();
Log.d("code", Integer.toString(code));
//Opens an input stream to retrieve verified data back from the server
//Starts a String Builder to read the data off the input
//Closes the BufferedReader once it has finished building the string
InputStream responseStream = new BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null)
stringBuilder.append(line + "/n");
responseStreamReader.close();
String response = stringBuilder.toString();
Log.d("response",response);
JSONObject jsonResponse = new JSONObject(response);
//Creates a JSON object from the string
ArrayList<String> chores = new ArrayList<String>();
for(int i=0; i < chores.length() ; i++) {
JSONObject json_data = jsonResponse.getJSONObject(i);
String chore=json_data.getString("chore_name");
String username=json_data.getString("child_username");
int points=json_data.getInt("point_value");
String currentChore = chore + "\t" + username + "\t" + points;
chores.add(currentChore);
Log.d("Output", currentChore);
ArrayAdapter<String> choreAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, chores));
setListAdapter(choreAdapter);
ListView listView = (ListView) findViewById(R.id.choreList);
listView.setAdapter(choreAdapter);
}
} catch (Exception e) {
e.printStackTrace();
} catch(JSONException e){
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}