[英]Android spinner not calling on listener method
I'm trying to implement spinner so that when item selected changes, it also changes couple of TextView-s depending on the selected item. 我正在尝试实现微调器,以便当所选项目更改时,它也根据所选项目更改了几个TextView-s。 But currently spinner doesn't call on its listener, nor does it show the selected item, but it shows them in the drop down list.
但是,当前微调器不会调用其侦听器,也不会显示所选项目,但会在下拉列表中显示它们。
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ShowData extends AppCompatActivity implements OnItemSelectedListener {
private TextView temp, mass, humid;
private ArrayList<String> list;
private Spinner spinner;
private JSONObject jObj;
private JSONArray array;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_show_data );
temp = (TextView) findViewById(R.id.temp);
mass = (TextView) findViewById(R.id.mass);
humid = (TextView) findViewById(R.id.humid);
list = new ArrayList<String>();
//fill() gets data from server and puts it into "list"
fill();
addItemsToSpinner();
addListenerToSpinner();
}
private void addItemsToSpinner(){
spinner = (Spinner)findViewById(R.id.date);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
private void addListenerToSpinner(){
spinner = (Spinner)findViewById(R.id.date);
spinner.setOnItemSelectedListener(this);
}
private void fill() {
//this portion works so i didnt include it
//gets data from the server and fills the list
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
Toast.makeText(getApplicationContext(), "Clicked on: " + list.get(pos), Toast.LENGTH_SHORT).show();
temp.setText("listener works!");
try {
String temperature = array.getJSONObject(pos).getString("temp");
String humidity = array.getJSONObject(pos).getString("humidity");
String massStr = array.getJSONObject(pos).getString("mass");
temp.setText(temperature);
humid.setText(humidity);
mass.setText(massStr);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
XML: XML:
[<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lukak.pcele_mobitel.ShowData">
<Spinner
android:id="@+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/mass"
android:layout_below="@+id/date"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="26dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/temp"
android:layout_below="@+id/mass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/humid"
android:layout_below="@+id/temp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="36dp" />
</RelativeLayout>][1]
not showing selected item on spinner 不显示微调框上的选定项目
showing available items when clicked on spinner 单击微调器时显示可用项目
Try doing it this way: 尝试以这种方式进行操作:
No need of addListenerToSpinner() method. 不需要addListenerToSpinner()方法。
onCreate: onCreate:
onCreate{
spinner = (Spinner)findViewById(R.id.date);
spinner.setOnItemSelectedListener(this);
}
addItemsToSpinner addItemsToSpinner
private void addItemsToSpinner(){
spinner = (Spinner)findViewById(R.id.date); // ****** remove this.....
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
onItemSelected onItemSelected
public void onItemSelected{
Toast.makeText(getApplicationContext(), "Clicked on: " + list.get(pos), Toast.LENGTH_SHORT).show();
....
}
add this modified code. 添加此修改后的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_show_data );
temp = (TextView) findViewById(R.id.temp);
mass = (TextView) findViewById(R.id.mass);
humid = (TextView) findViewById(R.id.humid);
spinner = (Spinner)findViewById(R.id.date);
list = new ArrayList<String>();
//fill() gets data from server and puts it into "list"
fill();
addItemsToSpinner();
spinner.setOnItemSelectedListener(this);
}
private void addItemsToSpinner(){
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
private void fill() {
//this portion works so i didnt include it
//gets data from the server and fills the list
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
Toast.makeText(getApplicationContext(), "Clicked on: " + list.get(pos), Toast.LENGTH_SHORT).show();
temp.setText("listener works!");
try {
String temperature = array.getJSONObject(pos).getString("temp");
String humidity = array.getJSONObject(pos).getString("humidity");
String massStr = array.getJSONObject(pos).getString("mass");
temp.setText(temperature);
humid.setText(humidity);
mass.setText(massStr);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.