2013-10-08 126 views
0

我被困在一個局面。我正在嘗試使用旋鈕來讓用戶選擇位置。我通過sqlite填充spinners。這個想法有時候會有一個國家,省份,城市和分區,但是任何字段都可以是空白的(如果沒有填入數據庫)。Android開發人員 - 微調

我想紡紗是「隱藏的」,如果他們沒有存儲在數據庫中的值。然而,只有「下一步」(例如:國家到省)被隱藏,並不是所有的「其他步驟」(城市和子區域)

我希望我足夠清楚,如果不是我可以澄清。

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

    if (parent.getId() == R.id.spinner_query_country) 
    { 
     country.selected = (class_location)country.spinner.getSelectedItem(); 
     get_province(); 
    } 
    else if (parent.getId() == R.id.spinner_query_province) 
    { 
     province.selected = (class_location)province.spinner.getSelectedItem(); 
     get_city(); 
    } 
    else if (parent.getId() == R.id.spinner_query_city) 
    { 
     city.selected = (class_location)city.spinner.getSelectedItem(); 
     get_sub_area(); 
    } 
    else if (parent.getId() == R.id.spinner_query_sub_area) 
    { 
     sub_area.selected = (class_location)sub_area.spinner.getSelectedItem(); 
    } 

} 

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

void get_country() 
{  
    make_spinner(country, db.getAll("SELECT * FROM country")); 
} 
void get_province() 
{ 
    make_spinner(province, db.getAll("SELECT * FROM province WHERE country_key=" + country.selected._key)); 
} 
void get_city() 
{ 
    make_spinner(city, db.getAll("SELECT * FROM city WHERE province_key=" + province.selected._key)); 
} 
void get_sub_area() 
{ 
    make_spinner(sub_area, db.getAll("SELECT * FROM sub_area WHERE city_key=" + city.selected._key)); 
} 


void make_spinner(_structure structure, List<class_location> location_list) 
{ 
    if (location_list.size() > 0) 
    { 
     class_location location_array[] = location_list.toArray(new class_location[location_list.size()]); 

     ArrayAdapter<?> adapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, location_array); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     structure.spinner.setAdapter(adapter); 
     structure.spinner.setVisibility(View.VISIBLE); 
     structure.textview.setVisibility(View.VISIBLE); 
    } 
    else 
    { 
     structure.spinner.setAdapter(null); 
     structure.spinner.setVisibility(View.GONE); 
     structure.textview.setVisibility(View.GONE); 
    } 

} 

回答

0

您是hidding只有一個微調,不是所有的人,只是因爲你是在make_spinner()設置可見,你只檢查一個下臺的onItemSelected(如果其他人嚴格)。要清楚,你的代碼將被永遠不會稱其爲「別人下臺」的一個方法的組件的可見性和隱蔽性。

P.S:您的代碼是有點亂,不遵循了Android的慣例或準則。這不好。嘗試檢出的一些準則,幫助你的合作者:)