我正在尝试从三个微调器获取数据,准备发布它。我遇到了一些我不明白的代码错误的问题...我是Java / Android的新手。我环顾四周,我正在使用的代码来自另一个Stackoverflow的答案,但确实有效:
//Spinner data
final Spinner findViewById(R.id.spinner_house);
String spinner_house_data = spinner_house.getSelectedItem().toString();
final Spinner findViewById(R.id.spinner_year);
String spinner_year_data = spinner_year.getSelectedItem().toString();
final Spinner spinner_name = (Spinner) findViewById(R.id.spinner_name);
String spinner_name_data = spinner_name.getSelectedItem().toString();
具体错误是:
(R.id.spinner_house);
不是声明 findViewById(R.id.spinner_year);
不是声明 感谢您的帮助,正如我所说,我是Android的新手,这有点令人困惑! (这个JSON请求需要5行jQuery!)
答案 0 :(得分:1)
您忘记为前两个微调器指定变量名称。 第三个微调器的代码似乎很好。
这是你应该拥有的:
final Spinner spinner_house = (Spinner) findViewById(R.id.spinner_house); String spinner_house_data = spinner_house.getSelectedItem().toString(); final Spinner spinner_year = (Spinner) findViewById(R.id.spinner_year); String spinner_year_data = spinner_year.getSelectedItem().toString(); final Spinner spinner_name = (Spinner) findViewById(R.id.spinner_name); String spinner_name_data = spinner_name.getSelectedItem().toString();
答案 1 :(得分:1)
我认为这可以解决您的问题:
final Spinner spinner_house = (Spinner) findViewById(R.id.spinner_house);
String spinner_house_data = spinner_house.getSelectedItem().toString();
final Spinner spinner_year = (Spinner) findViewById(R.id.spinner_year);
String spinner_year_data = spinner_year.getSelectedItem().toString();
final Spinner spinner_name = (Spinner) findViewById(R.id.spinner_name);
String spinner_name_data = spinner_name.getSelectedItem().toString();
但如果您的变量已经声明:
spinner_house = (Spinner) findViewById(R.id.spinner_house);
spinner_house_data = spinner_house.getSelectedItem().toString();
spinner_year = (Spinner) findViewById(R.id.spinner_year);
spinner_year_data = spinner_year.getSelectedItem().toString();
spinner_name = (Spinner) findViewById(R.id.spinner_name);
spinner_name_data = spinner_name.getSelectedItem().toString();
答案 2 :(得分:0)
考虑使用听众。
因此每个微调器都会跟踪成员变量中选择的内容。
let rect1 = CGRect(x: 0, y: 0, width: 100, height: 100)
let rect2 = CGRect(x: 40, y: 40, width: 150, height: 150)
CGRectUnion(rect1, rect2) // {x 0 y 0 w 190 h 190}
答案 3 :(得分:0)
1)首先在android manifest中添加权限
<uses-permission android:name="android.permission.INTERNET" />
2)加入gradle.build
useLibrary 'org.apache.http.legacy'
3)使用微调器
创建布局<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
4)然后接下来在src中使用类MainActivity
编写代码public class MainActivity extends AppCompatActivity {
String URL="YOUR URL HERE";
JSONArray Cities=null;
ArrayList<Cities> citiesList =new ArrayList<Cities>(); Spinner citiesspinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JSONAsyncTask().execute();
}
private class JSONAsyncTask extends AsyncTask<String, Void, JSONArray> {
private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
@Override
protected JSONArray doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(URL);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
Cities = jsono.getJSONArray("Cities");
return Cities;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return Cities;
}
protected void onPostExecute(JSONArray result) {
dialog.dismiss();
System.out.println(result);
for (int i = 0; i < result.length(); i++) {
JSONObject c = null;
try {
Cities cities=new Cities();
c = result.getJSONObject(i);
cities.setId(c.getString("CityID"));
cities.setCityname(c.getString("CityName"));
citiesList.add(cities);
} catch (JSONException e) {
e.printStackTrace();
}
}
citiesspinner = (Spinner) findViewById(R.id.spinner);
citiesspinner.setAdapter(new SpinnerAdapter(getApplicationContext(), citiesList));
citiesspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String value=citiesList.get(position).getCityname();
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
}
}
答案 4 :(得分:-1)
而不是:
out=Array
(
[0] => Maxima 5.35.1.2 http://maxima.sourceforge.net
[1] => using Lisp SBCL 1.2.7
[2] => Distributed under the GNU Public License. See the file COPYING.
[3] => Dedicated to the memory of William Schelter.
[4] => The function bug_report() provides bug reporting information.
[5] => (%i1)
)
res=(%i1)
status=0
使用此:
final Spinner findViewById(R.id.spinner_house);
而不是:
final Spinner spinner_house= (Spinner)findViewById(R.id.spinner_house);
使用此:
final Spinner findViewById(R.id.spinner_year);