- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经浏览了这么多代码并试图找出我做错了什么,但我只是在浪费时间。我无法弄清楚..
我正在开发一个 Android 应用程序,需要在一项 Activity 中放置 2 个旋转器。第二个微调器将根据第一个微调器上选择的条目进行填充,并且所有数据都将从 json 中获取。
我有一个 mysql 表,其中有 2 列,即国家和城市。我已经实现了分别在 2 个旋转器中获取国家和城市的数据,现在当用户选择一个国家/地区名称时,该特定国家的城市应列在第二个旋转器中。
MainActivity.java
public class LinearLayout extends Activity implements
View.OnClickListener{
ArrayList<String> listItems=new ArrayList<>();
ArrayList<String> listItems1=new ArrayList<>();
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter1;
Button submit;
Spinner s1,s2;
String text="";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout);
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(this);
s1 = (Spinner)findViewById(R.id.spinner1);
adapter=new ArrayAdapter<>(this,R.layout.spinner_layout,R.id.txt,listItems);
s1.setAdapter(adapter);
s2 = (Spinner)findViewById(R.id.spinner2);
adapter1=new ArrayAdapter<>(this,R.layout.spinner_layout,R.id.txt,listItems1);
s2.setAdapter(adapter1);
}
public void onStart(){
super.onStart();
BackTask bt=new BackTask();
bt.execute();
}
private class BackTask extends AsyncTask<Void,Void,Void> {
ArrayList<String> list;
ArrayList<String> list1;
protected void onPreExecute(){
super.onPreExecute();
list=new ArrayList<>();
list1=new ArrayList<>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
String result1="";
InputStream is1=null;
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://192.168.3.2/countries.php");
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
HttpClient httpclient1=new DefaultHttpClient();
HttpPost httppost1= new HttpPost("http://192.168.3.2/cities.php");
HttpResponse response1=httpclient1.execute(httppost1);
HttpEntity entity1 = response1.getEntity();
// Get our response as a String.
is1 = entity1.getContent();
}catch(IOException e){
e.printStackTrace();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
BufferedReader reader1 = new BufferedReader(new InputStreamReader(is1,"utf-8"));
String line = null;
String line1= null;
while ((line = reader.readLine()) != null) {
result+=line;
}
while ((line1 = reader1.readLine()) != null) {
result1+=line1;
}
is.close();
//result=sb.toString();
}catch(Exception e){
e.printStackTrace();
}
// parse json data
try{
JSONArray jArray =new JSONArray(result);
//MANUFACTURER = new String[jArray.length()];
for(int i=0;i<jArray.length();i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
// add interviewee name to arraylist
list.add(jsonObject.getString("country"));
}
JSONArray jArray1 =new JSONArray(result1);
//MANUFACTURER = new String[jArray.length()];
for(int j=0;j<jArray.length();j++){
JSONObject jsonObject1=jArray1.getJSONObject(j);
// add interviewee name to arraylist
list1.add(jsonObject1.getString("city"));
}
//spinner_fn();
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
adapter.notifyDataSetChanged();
listItems1.addAll(list1);
adapter1.notifyDataSetChanged();
//adapter1.notifyDataSetChanged();
}
}
这里我有 2 个 php 用于获取国家和城市的值
国家.php
<?php
$DB_USER='root';
$DB_PASS='mysql';
$DB_HOST='localhost';
$DB_NAME='xxxx';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("SET NAMES 'utf8'");
$sql="SELECT DISTINCT country from ssss";
$result=$mysqli->query($sql);
while($e=mysqli_fetch_assoc($result)){
$output[]=$e;
}
print(json_encode($output));
$mysqli->close();
?>
城市.php
<?php
$DB_USER='root';
$DB_PASS='mysql';
$DB_HOST='localhost';
$DB_NAME='xxxx';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("SET NAMES 'utf8'");
$sql1="select city from ssss";
$result1=$mysqli->query($sql1);
while($f=mysqli_fetch_assoc($result1)){
$output1[]=$f;
}
print (json_encode($output1));
$mysqli->close();
?>
这是我的 json 输出
[{"country":"india"},{"country":"england"},{"country":"australia"}]
[{"city":"bangalore"},{"city":"kolkata"},{"city":"mumbai"},{"city":"london"},{"city":"manchester"},{"city":"southampton"},{"city":"canberra"},{"city":"sydney"},{"city":"melbourne"}]
现在我知道我必须添加一个onitemclicklistener,但我不知道如何在第二个旋转器中获取依赖的城市
这是我的数据库
ID 国家/地区城市
1 印度类加罗尔
1 印度加尔各答
1 印度孟买
2英格兰伦敦
2英格兰曼彻斯特
2英格兰南安普顿
3澳大利亚堪培拉
3澳大利亚悉尼
3 澳大利亚墨尔本
最佳答案
假设您的城市位于以下 json 数组中
[{"country":"india", "city":"city1"},{"country":"england", "city":"city2"},{"country":"australia", "city":"city3"}]
尝试以下操作
// map to keep each country and its cities
final HashMap<String, ArrayList<String>> data = new HashMap<>();
// add your json array
JSONArray array;
try {
array = new JSONArray("");
} catch (JSONException e) {
e.printStackTrace();
return;
}
try {
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String country = item.getString("country");
// get the country from the map according to the country in json
ArrayList<String> dataList = data.get(country);
// if the list is null "the map doesn't have the list", create new one and add it to the map
// else add the city to the country list
if (dataList == null) {
dataList = new ArrayList<>();
data.put(country, dataList);
}
dataList.add(item.getString("city"));
}
} catch (JSONException e) {
e.printStackTrace();
}
final ArrayList<String> citiesList = new ArrayList<>();
final ArrayAdapter<String> citiesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, citiesList);
Spinner countriesSpinner = (Spinner) findViewById(R.id.spCountries);
// create the countries list to add it to the countries spinner
final ArrayList<String> countriesList = new ArrayList<>();
countriesList.add("india");
countriesList.add("england");
countriesList.add("australia");
countriesSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, countriesList));
countriesSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// get the selected country from spinner
String selectedCountry = countriesList.get(position);
// get the cities in the selected country
ArrayList<String> cities = data.get(selectedCountry);
// clear the list then add new cities
citiesList.clear();
for (String city : cities)
citiesList.add(city);
citiesAdapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Spinner citiesSpinner = (Spinner) findViewById(R.id.spCities);
citiesSpinner.setAdapter(citiesAdapter);
希望对你有帮助
关于java - 使用 json 数据的依赖 android spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337147/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!