2012-04-20 4 views
0

私はアプリケーション内に位置を示すスピナーを持っています。あなたの現在の場所か自分が入力しなければならない場所のいずれか。それは、Googleプレイスの素晴らしい例です。それは私が達成したいと思っているものです。Androidで選択したオプションの代わりにスピナーで位置を表示するにはどうすればよいですか?

アプリが最初に作成された場合、私はユーザーの現在の場所を取得し、自分のカスタム配列アダプターのlocation-propertyをその場所に設定します。その後、私は落ち着きをリフレッシュします。私のアダプタのgetViewでは、テキストを白に変更し、テキストをlocationプロパティに設定します。これは正常に動作します。

別の場所を選択すると、入力場所を入力するダイアログが表示されます。完了したら、私は自分のアダプタの位置プロパティを更新して、スピナーを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; 
    } 

} 
関連する問題