Spinner未显示所选项目或默认值

时间:2015-01-06 00:10:56

标签: android spinner

您好我有一个微调器被JSON / PHP查询填充到Mysql。这有效,但Spinner在加载应用程序时显示为空白,当我点击微调器时我可以看到值但是如果我选择一个值则它不显示所选值。

items.add("Choose Island");
//Get data from Database using JSON and Populate spinner
    new DownloadJSONAsync().execute();
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // attaching data adapter to spinner
    Island.setAdapter(spinnerAdapter);

Island.setOnItemSelectedListener(new OnItemSelectedListener(){
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            CIsland=Island.getSelectedItemPosition();   
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
        }
    });

类别:

public class DownloadJSONAsync extends AsyncTask<String,Void,Boolean>{
    private ProgressDialog pDialog; 
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(String... params) {
        try{
            JSONObject jObj = new JSONObject(getJSONUrl(URL_ISLANDS));
            JSONArray data = jObj.getJSONArray("Islands");
            for(int i=0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);
                Country = new HashMap<Integer, String>();
                Country.put(c.getInt("CountryID"), c.getString("Country"));
                String ListItem = c.getString("Country");
                items.add(ListItem);
            }
            return true;
        }catch(JSONException e){
            e.printStackTrace();
        }           
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result){
    super.onPostExecute(result);
    if(result==false){
        pDialog.setMessage("Could not get data");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    else{
        pDialog.dismiss();
    }
    }   
}

Layout.xml

<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:background="#0404B4"
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.cadushideals.MainActivity" >

<TextView
    android:id="@+id/lblTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:text="Company Add"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/lblTitle">

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblTitle"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:background="#FFFFFF"
    android:ems="10"
    android:hint="Enter Company Name"
    android:inputType="text" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:ems="10"
    android:hint="Enter Description"
    android:inputType="textMultiLine" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:ems="10"
    android:hint="Enter Email"
    android:inputType="textEmailAddress" />

<EditText
    android:id="@+id/editText4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText3"
    android:layout_below="@+id/editText3"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:ems="10"
    android:hint="Enter Address"
    android:inputType="text" />

<EditText
    android:id="@+id/editText5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText4"
    android:layout_below="@+id/editText4"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:ems="10"
    android:hint="Enter Tel"
    android:inputType="phone" />

<EditText
    android:id="@+id/editText6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText5"
    android:layout_below="@+id/editText5"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:ems="10"
    android:hint="Enter Fax"
    android:inputType="phone" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText6"
    android:layout_below="@+id/editText6"
    android:layout_marginTop="15dp"
    android:text="Latitude"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="15dp"
    android:text="Longitude" 
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignRight="@+id/editText6"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="15dp" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_below="@+id/spinner1"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:text="Company Valid" />

<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/spinner1"
    android:layout_below="@+id/checkBox1"
    android:layout_marginTop="15dp"
    android:background="#FFFFFF"
    android:text="Tire Customer" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/spinner1"
    android:layout_below="@+id/checkBox1"
    android:layout_marginTop="15dp"
    android:minWidth="50dip"
    android:text="Add" />
</LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:3)

在&#34; onPostExecute &#34;您的 DownloadJSONAsync ,您需要拨打&#34; notifyDataSetChanged &#34;在你的spinnerAdapter上。此外,您还可以拨打&#34; setSelection &#34;在您的Spinner课程中,可以选择所需的项目。希望这会有所帮助。

注意:另一种方法是让您的DownloadJSONAsync返回字符串列表。并在你的postexecute中,执行 spinnerAdapter.clear()。addAll(Items); (这是一种更简洁的方法,你不必调用notifyDataSetChanged)