gpt4 book ai didi

android - 从 MySQL 数据库填充 Android Spinner

转载 作者:行者123 更新时间:2023-11-30 01:18:59 25 4
gpt4 key购买 nike

我正在尝试使用 php/JSON 将 MySQL 数据库中单个字段的值(费用类别名称)填充到 spinner 中。这是我的代码。有人知道我做错了什么吗?

PHP:

<?php
require("config.inc.php");

$cat_query = "Select * from expense_categories" ;

try {
$cat_stmt = $db ->prepare($cat_query);
$cat_result = $cat_stmt -> execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$cat_rows = $cat_stmt->fetchAll();

if ($cat_rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();

foreach ($cat_rows as $row) {
$post = array();
$post ["category_id"] = $row["category_id"];
$post["Category_Name"] = $row["Category_Name"];

//update our repsonse JSON data
array_push($response["posts"], $post);
}

// echoing JSON response
echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>

当我运行它时,它返回:

{"success":1,"message":"有帖子可用!","posts":[{"category_id":"1","Category_Name":"旅行"},{"category_id":"2 ","Category_Name":"膳食"}]}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Spinner
android:id="@+id/spincat"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="99dp" />

</RelativeLayout>

JAVA

package;

imports;

public class Spinner_Category extends Activity {


private static final String LOGIN_URL = "http://192.168.10.253:88/expenses/spinner_category.php";

private JSONArray jArray = null;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_category);

}



public void updateJSON () {

String POSTS = "posts";
String ID = "category_id";
String CATEGORY = "Category_Name";

JSONParser jsonParser = new JSONParser();

Spinner spin = (Spinner) findViewById (R.id.spincat);

JSONObject json = jsonParser.getJSONFromUrl(LOGIN_URL);

final String[] items = new String[jArray.length()];



try{
jArray = json.getJSONArray(POSTS);


for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);

//gets the content of each tag

items[i]=c.getString(CATEGORY);
items [i]=c.getString(ID);

}

}
catch (JSONException e) {
e.printStackTrace();
}

ArrayAdapter<String> adapter =
new ArrayAdapter<String> (Spinner_Category.this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) ;


spin.setAdapter(adapter);

}
}

最佳答案

items[i]=c.getString(CATEGORY);
items [i]=c.getString(ID);

似乎有点奇怪,因为您正在用 id 覆盖类别放置。这是故意的吗?另外,如果您记录 c.getString(CATEGORY) ,它读取 json 是否正常?

编辑:relevant虽然我想你已经看过了

关于android - 从 MySQL 数据库填充 Android Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18768319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com