2016-08-25 88 views
0

我試圖實現微調,這樣當選擇的項目發生變化時,它也會根據所選項目更改幾個TextView-s。但是當前微調不會呼叫其偵聽器,也不會顯示所選項目,但會在下拉列表中顯示它們。Android微調不調用偵聽器方法

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] 

not showing selected item on spinner

showing available items when clicked on spinner

+0

在onCreate中初始化微調器(findViewById語句)並且只有一次。你已經初始化了兩次。然後,只需使用該對象設置偵聽器並閱讀值 – DsD

回答

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(); 
.... 
} 
+0

嘿, 我按照你所說的完成了。 微調只是綁定一次現在在onCreate,但我仍然有同樣的問題 – Zeromcml

+0

檢查你的代碼在GitHub太。似乎很好。你可以在重新安裝應用程序後檢查一次嗎? – DsD

+0

我做了最初的改變後,我已經做了昨天。現在也試過了,但它沒有改變任何東西。此外,我剛剛找到了一種方法來製作一個按鈕下拉列表,所以我會嘗試一下 – Zeromcml

0

添加此修飾的C-頌。

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) { 
} 

} 
相關問題