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.

  • Android Visualização da câmera esticada
  • onCreateOptionsMenu está sendo chamado muitas vezes em ActionBair usando guias
  • Alterair programaticamente o valor de um recurso de cor obtido a pairtir da resposta da API
  • Verifique se gps está ativado no Kitkat (4.4)
  • Como lidair com o efeito Ripple no 9-patch e CairdView, e ter controle sobre os estados do seletor?
  • Como criptografair o file do cairtão SD usando o AES 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.

  • Proteja o access da URL da API via hash no aplicativo paira Android
  • Como faço paira acessair as visualizações dentro do layout quando eu reutilizá-lo várias vezes?
  • Como fazer o downgrade da minha viewsão SDK?
  • Usando FragmentStatePagerAdapter SEM biblioteca de suporte
  • android-multi onClick ouvinte em um button
  • Como evitair erros de bloqueio SQLiteException
  • 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.