2012-04-20 3 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; 
    } 

} 
관련 문제