2012-04-20 16 views
0

我在顯示位置的應用程序中有一個微調器。無論是您當前的位置還是您必須輸入的自己選擇的位置。或者如此,應該可以找到一個很好的例子,就是Google Places。這是我想要實現的,或多或少......如何在微調器中顯示位置而不是在Android中選擇的選項?

因此,如果應用程序是第一次創建,我抓取用戶的當前位置,並將我的自定義arrayadapter的位置屬性設置爲該位置。之後,我刷新drawablestate。在適配器的getView中,我將文本更改爲白色,並將文本設置爲位置屬性。這工作正常。

當我選擇不同的位置時,我得到一個輸入對話框來輸入我的位置。一旦完成,我更新適配器的位置屬性,並使微調器刷新它的drawableState。完成後,我再次在適配器中輸入我的getView函數,編輯文本的顏色和文本本身,但微調器中的文本保持不變。只有當我再次擊中微調器並擊中「當前位置」時,它才顯示出我之前給出的位置。如果我再次點擊「當前位置」,它什麼都不做(它提取位置,但不更新視圖)。當我再次擊中「另一個位置」時,它會顯示您當前的位置並要求找到其他位置,並且重新進行一次。

有沒有人有如何解決這個問題的想法?請參閱下面的我的代碼:

public class FooActivity extends Activity { 

//UI-elements 
private Spinner _locationSpinner; 
private LocationArrayAdapter _locationAdapter; 

//Location 
private String[] _locationArray = {"Current location", "Different location"}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    //config spinner 
    configSpinner(); 
} 
//functions 
private void configSpinner() { 
    _locationSpinner = (Spinner)findViewById(R.id.main_location); 

    _locationAdapter = new LocationArrayAdapter(this, R.layout.spinner_item, _locationArray); 
    _locationAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item); 

    showCurrentLocationInSpinner(); 
    _locationSpinner.setAdapter(_locationAdapter); 
    _locationSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
      if (pos == 0) 
       showCurrentLocationInSpinner(); 
      else 
       showOtherLocationInSpinner(); 
     } 

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

    }); 
} 

//view functions 
private void showCurrentLocationInSpinner() { 
    try { 
     Location loc = getCurrentLocation(); 
     if (loc == null) return; 

     List<Address> addresses = _geo.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); 
     Address curAddress = addresses.get(0); 

     _locationAdapter.setLocation(curAddress.getAddressLine(0) + ", " + curAddress.getLocality()); 
     _locationSpinner.refreshDrawableState(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (IndexOutOfBoundsException e){ 
     e.printStackTrace(); 
    } 
} 
protected void showOtherLocationInSpinner() { 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

alert.setMessage(getResources().getString(R.string.location_dialog_message)); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     Editable value = input.getText(); 
     _locationAdapter.setLocation(value.toString()); 
     _locationSpinner.refreshDrawableState(); 
     } 
    }); 

    alert.setNegativeButton(getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
     } 
    }); 

    alert.show(); 
} 
} 

我定製ArrayAdapter:

public class LocationArrayAdapter extends ArrayAdapter<CharSequence> { 

private String _location; 

public LocationArrayAdapter(Context context, int textViewResourceId, CharSequence[] objects) { 
    super(context, textViewResourceId, objects); 
} 

public String getLocation() { 
    return _location; 
} 
public void setLocation(String location) { 
    this._location = location; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 

    TextView t = (TextView) view.findViewById(android.R.id.text1); 
    t.setTextColor(Color.WHITE); 
    t.setText(_location); 

    return view; 
} 

} 

回答

0

可謂是相當愚蠢的錯誤......我不得不通知我的適配器,該數據集已經改變...

public class LocationArrayAdapter extends ArrayAdapter<CharSequence> { 

    private String _location; 

    public LocationArrayAdapter(Context context, int textViewResourceId, CharSequence[] objects){ 
     super(context, textViewResourceId, objects); 
    } 

    public String getLocation() { 
     return _location; 
    } 
    public void setLocation(String location) { 
     this._location = location; 
     notifyDataSetChanged(); //FIXES IT 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 

     TextView t = (TextView) view.findViewById(android.R.id.text1); 
     t.setTextColor(Color.WHITE); 
     t.setText(_location); 

     return view; 
    } 

} 
相關問題