Java 通过微调器使用setOnItemSelectedListener和onNothingSelected
我只是想知道是否有人可以帮助我解决我目前面临的问题,因为我正在尝试使用spinner实现setOnItemSelectedListener和onNothingSelected事件 一旦用户从spinner中选择选项,它将向他们显示google地名,然后当用户单击一个地方时,它将显示另一个名为details.java的页面,其中包含该地方的详细信息以及mapview 我想这两个选项我都用错了,所以请有人帮我一下,给我一些建议Java 通过微调器使用setOnItemSelectedListener和onNothingSelected,java,android,drop-down-menu,spinner,android-spinner,Java,Android,Drop Down Menu,Spinner,Android Spinner,我只是想知道是否有人可以帮助我解决我目前面临的问题,因为我正在尝试使用spinner实现setOnItemSelectedListener和onNothingSelected事件 一旦用户从spinner中选择选项,它将向他们显示google地名,然后当用户单击一个地方时,它将显示另一个名为details.java的页面,其中包含该地方的详细信息以及mapview 我想这两个选项我都用错了,所以请有人帮我一下,给我一些建议 public class PlaceActivity extends A
public class PlaceActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
public ArrayList<GetterSetter> myArrayList;
private Toolbar toolbar;
private Spinner spinner;
ListView myList;
private String[] places;
ProgressDialog dialog;
TextView nodata;
PlaceAdapter adapter;
GetterSetter addValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place);
if (!isNetworkAvailable()) {
Toast.makeText(getApplicationContext(), "Enable internet connection and RE-LAUNCH!!",
Toast.LENGTH_LONG).show();
return;
}
myList = (ListView) findViewById(R.id.myList);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
places = getResources().getStringArray(R.array.places);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setTitle("Title");
spinner = (Spinner) findViewById(R.id.spinnerplaces);
// Create an ArrayAdapter using the string array and a default spinner
ArrayAdapter<CharSequence> listAdapter = ArrayAdapter
.createFromResource(this, R.array.places,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
listAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(listAdapter);
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(PlaceActivity.this, R.array.places,android.R.layout.simple_spinner_item);
list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(this);
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int itemPosition, long itemId) {
dialog = ProgressDialog.show(this, "", "Please wait", true);
// Google Places Access key and location values.
new readFromGooglePlaceAPI()
.execute("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
+ "location=53.795984,-1.759398&radius=900&sensor=true&"
+ "key=AIzaSyA7CzOa10AOfBAU9FvaIrVlVyCKzXxiCc0&types="
+ places[itemPosition]);
myList.OnItemSelectedListener(this);
return true;
}
public class readFromGooglePlaceAPI extends AsyncTask<String, Void, String> {
@Override protected String doInBackground(String... param) {
return readJSON(param[0]);
}
protected void onPostExecute(String str) {
myArrayList = new ArrayList<GetterSetter>();
try {
JSONObject root = new JSONObject(str);
JSONArray results = root.getJSONArray("results");
for (int i = 0; i < results.length(); i++) {
JSONObject arrayItems = results.getJSONObject(i);
JSONObject geometry = arrayItems.getJSONObject("geometry");
JSONObject location = geometry.getJSONObject("location");
addValues = new GetterSetter();
addValues.setLat(location.getString("lat"));
addValues.setLon(location.getString("lng"));
addValues.setName(arrayItems.getString("name").toString());
addValues.setRating(arrayItems.getString("rating").toString());
addValues.setVicinity(arrayItems.getString("vicinity").toString());
myArrayList.add(addValues);
Log.d("Before", myArrayList.toString());
}
} catch (Exception e) {
}
System.out
.println("############################################################################");
Log.d("After:", myArrayList.toString());
nodata = (TextView) findViewById(R.id.nodata);
nodata.setVisibility(View.GONE);
adapter = new PlaceAdapter(PlaceActivity.this, R.layout.list_row, myArrayList);
myList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
}
}
public String readJSON(String URL) {
StringBuilder sb = new StringBuilder();
HttpGet httpGet = new HttpGet(URL);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} else {
Log.e("JSON", "Couldn't find JSON file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// no-op
}
公共类PlaceActivity扩展了AppCompatActivity实现
AdapterView.OnItemSelectedListener{
公共ArrayList myArrayList;
专用工具栏;
私人纺纱机;
列表视图myList;
私人字符串[]个位置;
进程对话;
文本视图节点数据;
放置适配器;
GetterSetter附加值;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、活动地点);
如果(!isNetworkAvailable()){
Toast.makeText(getApplicationContext(),“启用internet连接并重新启动!!”,
Toast.LENGTH_LONG).show();
返回;
}
myList=(ListView)findViewById(R.id.myList);
工具栏=(工具栏)findViewById(R.id.tool\u栏);
places=getResources().getStringArray(R.array.places);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
设定所有权(“所有权”);
微调器=(微调器)findViewById(R.id.spinnerplaces);
//使用字符串数组和默认微调器创建ArrayAdapter
ArrayAdapter listAdapter=ArrayAdapter
.createFromResource(此,R.array.places,
android.R.layout.simple\u微调器(项目);
//指定显示选项列表时要使用的布局
列表适配器
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//将适配器应用于微调器
spinner.setAdapter(listAdapter);
ArrayAdapter list=ArrayAdapter.createFromResource(PlaceActivity.this,R.array.places,android.R.layout.simple\u微调器\u项);
list.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spinner.setOnItemSelectedListener(此);
}
专用布尔值isNetworkAvailable(){
ConnectivityManager ConnectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo activeNetworkInfo=connectivityManager.getActiveNetworkInfo();
返回activeNetworkInfo!=null;
}
@凌驾
已选择公共项(AdapterView父项、视图、int-itemPosition、long-itemId){
dialog=ProgressDialog.show(此“,”请稍候“,true);
//Google放置访问键和位置值。
新readFromGooglePlaceAPI()
.执行(”https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
+“位置=53.795984,-1.759398&半径=900&传感器=真实&”
+“key=AIzaSyA7CzOa10AOfBAU9FvaIrVlVyCKzXxiCc0&types=”
+地点[项目位置];
myList.OnItemSelectedListener(此);
返回true;
}
公共类readFromGooglePlaceAPI扩展异步任务{
@覆盖受保护的字符串doInBackground(字符串…参数){
返回readJSON(参数[0]);
}
受保护的void onPostExecute(字符串str){
myArrayList=新建ArrayList();
试一试{
JSONObject根=新的JSONObject(str);
JSONArray results=root.getJSONArray(“结果”);
对于(int i=0;i @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent details = new Intent(MainActivity.this, Details.class);
details.putExtra("name", myArrayList.get(arg2).getName());
details.putExtra("rating", myArrayList.get(arg2).getRating());
details.putExtra("vicinity", myArrayList.get(arg2).getVicinity());
details.putExtra("lat", myArrayList.get(arg2).getLat());
details.putExtra("lon", myArrayList.get(arg2).getLon());
startActivity(details);
}
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
//If you're wondering, int pos is the position of the item selected (starting with 0)
// what you want onClick
}
@Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent details = new Intent(MainActivity.this, Details.class);
details.putExtra("name", myArrayList.get(arg2).getName());
details.putExtra("rating", myArrayList.get(arg2).getRating());
details.putExtra("vicinity", myArrayList.get(arg2).getVicinity());
details.putExtra("lat", myArrayList.get(arg2).getLat());
details.putExtra("lon", myArrayList.get(arg2).getLon());
startActivity(details);
}