0

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

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":"Travel"},{"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);

    }
}
4

1 回答 1

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

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

编辑:相关的,我想你已经看过了

于 2013-09-12T15:38:07.063 回答