Android:根据2个微调框的选择填充ListView(JSON)

时间:2018-11-27 08:35:49

标签: android json listview android-spinner

首先,请不要关闭/锁定问题或将其标记为重复。问题是与其他情况不同。我已经阅读了将近20天的所有内容,并立即询问。

我有2个来自JSON的微调器(基于1st的第二个微调器值)。如果在选择微调器值后单击提交按钮,则应该获取一个列表视图,其中包含来自JSON的项目。

所以我假设的概念是:

我填充了微调器,然后单击提交,将微调器的值发送到PHP Web服务,我编写了一个SQL查询以基于这些微调器检索数据。

问题是我无法获得结果。单击提交后,没有错误,但我无法进入列表视图。

这是与我的问题有关的代码。

PHP Web服务:

<?php
.......
.......
....... 

if(isset($_POST['converternumber']) && isset($_POST['zonenumber']))
{

  $n_converternumber = $_POST["converternumber"];
  $n_zonenumber = $_POST["zonenumber"];
//$n_converternumber= "2";

}
$sql_query= "select codeid from addcode where converternumber like '%".$n_converternumber."%' and zonenumber like '%".$n_zonenumber."%'";

$result= mysqli_query($connectionn,$sql_query);
..........
..........
?>

在mainActivity中,我可以填充2个微调器,我只粘贴了提交按钮的onClick方法的代码:

public void addflrsurvey(View view){

        st = stateSpinner.getSelectedItem().toString();
        ci = citiesSpinner.getSelectedItem().toString();

        String method = "listofcodes";
        BackgroundTaskcode backgroundTaskcode = new BackgroundTaskcode(this);
        backgroundTaskcode.execute(method, st, ci);
        finish();
}

BackgroundTaskcode类具有如下的doInBackground类

 try {
                URL url = new URL(flr_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

                String data = URLEncoder.encode("converternumber", "UTF-8")+"="+URLEncoder.encode(st,"UTF-8")+"&"+
                        URLEncoder.encode("zonenumber", "UTF-8")+"="+URLEncoder.encode(ci,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();

                return "wait....";
.................}

还有一个名为codelist的Java类,它具有用于从json文件填充listview的代码,效果很好,但是我希望它基于Spinners值选择。

我再次恳切地要求不要阻止我或关闭它。预先感谢

编辑: Logcat

11-28 14:58:32.040 3831-3880/com.example.app.spinnerdemo E/Json response: []����������������������������������������������
11-28 14:58:32.065 3831-3881/com.example.app.spinnerdemo E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
    Process: com.example.app.spinnerdemo, PID: 3831
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:309)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:200)
        at android.os.Handler.<init>(Handler.java:114)
        at android.widget.Toast$TN.<init>(Toast.java:345)
        at android.widget.Toast.<init>(Toast.java:101)
        at android.widget.Toast.makeText(Toast.java:259)
        at com.example.app.spinnerdemo.codelist$FetchDataTask.doInBackground(codelist.java:61)
        at com.example.app.spinnerdemo.codelist$FetchDataTask.doInBackground(codelist.java:43)
        at android.os.AsyncTask$2.call(AsyncTask.java:295)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
11-28 14:58:32.338 1664-3178/system_process W/ActivityManager:   Force finishing activity com.example.app.spinnerdemo/.codelist
11-28 14:58:32.393 1664-3178/system_process W/ActivityManager:   Force finishing activity com.example.app.spinnerdemo/.MainActivity

链接至完成 CODE

2 个答案:

答案 0 :(得分:0)

您遇到此问题是因为您试图创建一个Toast,但是Toast只能从ui线程/主线程显示,而不能从后台线程显示。将ToastdoInbackground移到onPostExecute

答案 1 :(得分:0)

您对下面的代码段有疑问。

BackgroundTaskcode backgroundTaskcode = new BackgroundTaskcode(this);
backgroundTaskcode.execute(method, st, ci);
finish();

开始后台任务后,您就是finishing Activity。您应该等待asyncTask完成并触发onPostExecute方法。