2016-04-28 116 views
1

我從json字符串獲取值並將其添加到arraylist getSpinArrList.I已發佈相關代碼。微調項目不顯示在微調框和微調框onClick監聽不工作

我的問題是,微調不顯示第一項和OnItemClick不在微調工作。

但是,如果我單擊微調框,我只能查看我從arraylist中獲得的項目。

MainActivity.java:

Spinner spinCrePage; 

spinCrePage = (Spinner) findViewById(R.id.sp_create_page); 

ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, getSpinArrList); 

adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

spinCrePage.setAdapter(adapter_state); 

spinCrePage.setOnItemSelectedListener(this); 


@Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, 
      long id) { 

     Toast.makeText(getApplicationContext(),"Working" , Toast.LENGTH_SHORT).show(); 

     spinCrePage.setSelection(position); 
     String selState = (String) spinCrePage.getSelectedItem(); 
    // textView.setText("Selected Android OS:" + selState); 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 

    } 
+1

getSpinArrList中有什麼? –

回答

1

onItemSelected,你必須創建一個使用位置的開關情況。對於每一個位置建立的情況下,用UR desiered代碼來實現它...

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
          long id) { 
    switch (position) { 
     case 0: 
      Toast.makeText(getApplicationContext(),"Working" , Toast.LENGTH_SHORT).show(); 
      break; 
     case 1: 
      //code 
      break; 

    } 
} 
0

請使用此代碼,請按照下列步驟項目

  1. 名單微調 打開「RES /值/strings.xml「文件,定義將顯示在Spinner(下拉列表)中的項目列表。

文件:RES /值/ strings.xml中

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="app_name">MyAndroidApp</string> 
<string name="country_prompt">Choose a country</string> 
<string-array name="country_arrays"> 
    <item>Malaysia</item> 
    <item>United States</item> 
    <item>Indonesia</item> 
    <item>France</item> 
    <item>Italy</item> 
    <item>Singapore</item> 
    <item>New Zealand</item> 
    <item>India</item> 
</string-array> 

  • 創建xml文件RES /佈局/ main.xml中 在Spinner條目中使用country_arrays
  • <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    
    <Spinner 
        android:id="@+id/spinner1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:entries="@array/country_arrays" 
        android:prompt="@string/country_prompt" /> 
    
    <Spinner 
        android:id="@+id/spinner2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
    
    <Button 
        android:id="@+id/btnSubmit" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Submit" /> 
    </LinearLayout> 
    
  • 代碼 創建Java文件MyAndroidAppActivity.java
  • package com.webastral.drinkin.home; 
    
    import java.util.ArrayList; 
    import java.util.List; 
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.Spinner; 
    import android.widget.Toast; 
    
    public class MyAndroidAppActivity extends Activity { 
    
    private Spinner spinner1, spinner2; 
    private Button btnSubmit; 
    
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    
        addItemsOnSpinner2(); 
        addListenerOnButton(); 
        addListenerOnSpinnerItemSelection(); 
    } 
    
    // add items into spinner dynamically 
    public void addItemsOnSpinner2() { 
    
        spinner2 = (Spinner) findViewById(R.id.spinner2); 
        List<String> list = new ArrayList<String>(); 
        list.add("list 1"); 
        list.add("list 2"); 
        list.add("list 3"); 
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
          android.R.layout.simple_spinner_item, list); 
        dataAdapter 
          .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        spinner2.setAdapter(dataAdapter); 
    } 
    
    public void addListenerOnSpinnerItemSelection() { 
        spinner1 = (Spinner) findViewById(R.id.spinner1); 
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener()); 
    } 
    
    // get the selected dropdown list value 
    public void addListenerOnButton() { 
    
        spinner1 = (Spinner) findViewById(R.id.spinner1); 
        spinner2 = (Spinner) findViewById(R.id.spinner2); 
        btnSubmit = (Button) findViewById(R.id.btnSubmit); 
    
        btnSubmit.setOnClickListener(new OnClickListener() { 
    
         @Override 
         public void onClick(View v) { 
    
          Toast.makeText(
            MyAndroidAppActivity.this, 
            "OnClickListener : " + "\nSpinner 1 : " 
              + String.valueOf(spinner1.getSelectedItem()) 
              + "\nSpinner 2 : " 
              + String.valueOf(spinner2.getSelectedItem()), 
            Toast.LENGTH_SHORT).show(); 
         } 
    
        }); 
    } 
    

    }

    創建Java檔案CustomOnItemSelectedListener.java

    package com.webastral.drinkin.home; 
    
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemSelectedListener; 
    import android.widget.Toast; 
    
    public class CustomOnItemSelectedListener implements OnItemSelectedListener { 
    
    public void onItemSelected(AdapterView<?> parent, View view, int pos, 
         long id) { 
        Toast.makeText(
          parent.getContext(), 
          "OnItemSelectedListener : " 
            + parent.getItemAtPosition(pos).toString(), 
          Toast.LENGTH_SHORT).show(); 
    } 
    
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 
    } 
    

    }

    Hope This wil Help you 
    
    0

    在您的應用程序,我認爲唯一的時候敬酒不出現是當你選擇同一項目連續兩次。我試過你的代碼,它對我來說工作得很好。您可能需要從代碼中刪除

    spinCrePage.setSelection(position); 
    

    。它根本沒有任何用處。 只有當您從Spinner中的前一項中選擇不同的項目時,纔會觸發OnItemSelected()。

    至於「微調不顯示第一項」的問題,你應該檢查你的數組列表的內容。看看在執行過程中是否有任何東西被移除或什麼。檢查將數據添加到arraylist中的循環。

    「OnClick not working」:我很確定Spinner沒有OnClickListener,Spinners使用OnItemSelectedListener()。