我正在尝试实现微调器,以便在项目选择更改时,它还会根据所选项目更改一些TextView-s。但是,当前微调器不会调用其侦听器,也不显示所选项,但它会在下拉列表中显示它们。
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 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]
答案 0 :(得分:0)
尝试这样做:
无需 addListenerToSpinner()方法。
<强>的onCreate:强>
onCreate{
spinner = (Spinner)findViewById(R.id.date);
spinner.setOnItemSelectedListener(this);
}
<强> 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 强>
public void onItemSelected{
Toast.makeText(getApplicationContext(), "Clicked on: " + list.get(pos), Toast.LENGTH_SHORT).show();
....
}
答案 1 :(得分:0)
添加此修改后的代码。
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) {
}
}