使用Spinner

时间:2016-08-15 15:16:40

标签: android listview mysqli spinner

我是一名Android编程新手,标题中提到的问题令我感到不安 我在localhost中的MySQL数据库中有一个Groceries表:

id----name---------type
1     apple        fruit
2     banana       fruit
3     lettuce      vegetable
4     apricot      fruit
5     cauliflower  vegetable

我的Android应用程序中也有一个Spinner和一个ListView。 Spinner有一个包含项fruit& vegetable
我想当用户从Spinner中选择类型为fruitvegetable时,ListView将填充MySQL中相应类型的名称。

这是我到目前为止所做的。

MainActivity上课:

package com.example.mine.application;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

public Spinner groceryTypes;
public ListView groceries;

public static List<String> arrayGroceries;
ArrayAdapter<String> adapterGroceries;

@Override
protected void onCreate(Bundle savedInstanceState) {

    groceryTypes = (Spinner) findViewById(R.id.groceryTypes_spinner);
    groceries = (ListView) findViewById(R.id.groceries_listView);

    arrayGroceries = new ArrayList<>();
    adapterGroceries = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayGroceries);
    groceries.setAdapter(adapterGroceries);

    groceryTypes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            BackgroundWorker backgroundWorker = new BackgroundWorker(MainActivity.this);
            backgroundWorker.execute(groceryTypes.getItemAtPosition(position).toString());
            adapterGroceries.notifyDataSetChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //TODO Auto-generated method stub
        }
    });
}

BackgroundWorker上课:

package com.example.mine.application;
import android.content.Context;
import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;

BackgroundWorker(Context ctx){
    context = ctx;
}

@Override
protected String doInBackground(String... params) {
    String groceries_URL = "http://10.0.2.2/groceries.php";
    String groceryType = params[0];
    try {
        URL url = new URL(groceries_URL);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);

        OutputStream outputStream = httpURLConnection.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter
                (new OutputStreamWriter(outputStream, "UTF-8"));
        String post_data = URLEncoder.encode("grocery_type", "UTF-8")+"="+URLEncoder.encode(groceryType, "UTF-8");
        bufferedWriter.write(post_data);
        bufferedWriter.flush();
        bufferedWriter.close();
        outputStream.close();

        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader
                (new InputStreamReader(inputStream, "iso-8859-1"));

        MainActivity.arrayGroceries.clear();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            MainActivity.arrayGroceries.add(line);
        }
        bufferedReader.close();
        inputStream.close();
        httpURLConnection.disconnect();

        return null;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
}

这是我的groceries.php

<?php
require "conn.php";

$grocery_type = $_POST["grocery_type"];
$qry = "SELECT name FROM groceries WHERE type LIKE '$grocery_type';";

$result = mysqli_query($conn, $qry);

while($row = mysqli_fetch_row($result)) {
    echo $row[1];
}

mysqli_free_result($result);

$conn->close();
?>

我认为问题在于我如何处理查询中的多个项目,但任何解释和帮助来实现这一点都将受到极大的重视。

1 个答案:

答案 0 :(得分:1)

假设您从服务器获取了正确的数据并且正在填充杂货数组,则需要向onPostExecute AsyncTask添加BackgroundWorker并通知adapterGroceries数据已经改变。

你在AysncTask.execute()之后执行此操作,但它在那里没有任何好处,execute()立即返回并且通知运行但是没有任何更新。它需要在数据返回后运行。