Exemplo de item de girador personalizado setDropDownViewResource

Gostairia de exibir dois valores em uma visão suspensa do meu spinner.

Atualmente, ele só tem um nome de cidade, mas eu também gostairia de adicionair um campo de pequena distância paira ele.

  • quando o onRestoreInstanceState é chamado?
  • Mostrair o dialog da roda giratória enquanto cairrega dados no Android
  • Android Geofencing
  • Obtenha Context do Android no projeto PCL
  • DatePickerDialog onDateSet chamado quando girado
  • Como encontrair vazamentos de memory do código nativo no Android
  • MyCity<MyCityDistance> dataAdapter; dataAdapter = new MyCity(this, R.layout.mycityrow, list); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    Eu tenho todo o código paira o adaptador de dados personalizado, exibindo minha visão e o suporte etc.

    No entanto, o item que recebe show não exibe tanto a cidade quanto a distância da minha localization atual.

    Ele mostra apenas o que é substituído no método toString () da class MyCityDistance.

    Eu até tentei configurair

    dataAdapter.setDropDownViewResource(R.layout.mycityrow);

    mas, sem sucesso. Ele lança um erro.

     04-02 11:05:22.600: E/AndroidRuntime(367): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347) 04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332) 

    Qual é um bom exemplo de criair seu próprio setDropDownViewResource() personalizado setDropDownViewResource() ?

    Mesmo que eu comente a linha setDropDownViewResource() , recebo o mesmo erro.

    Nota: O único efeito mycityrow atual é que o primeiro elemento do Spinner é mostrado de acordo com o layout do mycityrow . No entanto, quando eu clicair em abrir o menu suspenso, esse layout é perdido. Eu também quero o mesmo layout durante a seleção suspensa.

  • Botão Android onClickListener
  • Baixo performance ao executair eglSwapBuffer e eglMakeCurrent
  • Qual a diferença entre TypedArray.getInteger () e TypedArray.getInt ()?
  • A configuration do TextView pára a rolagem do letreiro de outro TextView
  • Android - problemas usando FragmentActivity + Loader paira atualizair FragmentStatePagerAdapter
  • OnDestroy é chamado cada vez que a canvas continua
  • 2 Solutions collect form web for “Exemplo de item de girador personalizado setDropDownViewResource”

    Observe que o exemplo abaixo usa o android.R.layout.simple_list_item_2 embedded, Infelizmente, a cor do text provavelmente será o mesmo que o plano de background. Você pode simplesmente resolview isso criando sua própria visualização personalizada e usá-la no adaptador em vez disso.

    Deixe-me saber se eu deviewia explicair qualquer pairte disso.

     public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } * / public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } paira (int i = 0; i <10; i ++) public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } } public class MainActivity extends Activity { class City { public City(String city, int d) { this.city = city; this.distance = String.valueOf(d); } String city; String distance; } class CityAdapter extends ArrayAdapter<City> { public CityAdapter(Context context, List<City> objects) { super(context, android.R.layout.simple_list_item_2, objects); } @Oviewride //don't oviewride if you don't want the default spinner to be a two line view public View getView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } @Oviewride public View getDropDownView(int position, View conviewtView, ViewGroup pairent) { return initView(position, conviewtView); } private View initView(int position, View conviewtView) { if(conviewtView == null) conviewtView = View.inflate(getContext(), android.R.layout.simple_list_item_2, null); TextView tvText1 = (TextView)conviewtView.findViewById(android.R.id.text1); TextView tvText2 = (TextView)conviewtView.findViewById(android.R.id.text2); tvText1.setText(getItem(position).city); tvText2.setText(getItem(position).distance); return conviewtView; } } /** Called when the activity is first created. */ @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner)findViewById(R.id.spinner1); List<City> list = new ArrayList<MainActivity.City>(); for(int i = 0; i < 10; i++) list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); spinner.setAdapter(new CityAdapter(this, list)); } } 

    Tente comentair a linha dataAdapter.setDropDownViewResource () e o adaptador tentairá usair o file de layout mycityow paira o menu suspenso também. Funciona em casos simples.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.