从MySQL数据库填充Android Spinner

时间:2013-09-12 15:24:33

标签: android mysql json spinner

我正在尝试使用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”:“发布可用!”,“帖子”:[{“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);

    }
}

1 个答案:

答案 0 :(得分:0)

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

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

编辑:relevant我觉得你已经看过了