I have one Spinner
that works like this, I select an option and it sends the text to editTextZodiaco
, the texts are displayed correctly in my Spinner
, but when I select an option to send it to editTextZodiaco
the application it stops, logcat
it marks the following:
java.lang.ArrayIndexOutOfBoundsException: length=0; index=4
clase
String[] zodiaco = {};
int flags[] = {R.drawable.icon_zod_elig, R.drawable.icon_zod_no, R.drawable.icon_zod_acuario, R.drawable.icon_zod_piscis, R.drawable.icon_zod_aries, R.drawable.icon_zod_tauro, R.drawable.icon_zod_geminis, R.drawable.icon_zod_cancer, R.drawable.icon_zod_leo, R.drawable.icon_zod_virgo, R.drawable.icon_zod_libra, R.drawable.icon_zod_escorpio, R.drawable.icon_zod_sagitario, R.drawable.icon_zod_capricornio};
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position == 1){
editTextZodiaco.setText(" ");
}else if(position !=0){
String description = zodiaco[position];
editTextZodiaco.setText(description.split(" ")[0]);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), flags, zodiaco);
spin.setOnItemSelectedListener(this);
spin.setAdapter(customAdapter);
CustomAdapter
:
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] titulo_zodiaco;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags, String[] titulo_zodiaco) {
this.context = applicationContext;
this.flags = flags;
this.titulo_zodiaco = titulo_zodiaco;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return flags.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imagenspinner);
TextView names = (TextView) view.findViewById(R.id.textspinner);
//////// carga los textos de array
String[] titulo_zodiaco = context.getResources().getStringArray(R.array.titulos_zodiaco);
icon.setImageResource(flags[i]);
names.setText(titulo_zodiaco[i]);
return view;
}
}
array
:
<array name="titulos_zodiaco">
<item>Elige un Zodiaco</item>
<item>No quiero ninguno</item>
<item>Acuario (Ene. 20 - Feb. 18)</item>
<item>Piscis (Feb. 19 - Marzo 20)</item>
<item>Aries (Marzo 31 - Abril 19)</item>
<item>Tauro (Abril 20 - Mayo 20)</item>
<item>Géminis (Mayo 21 - Junio 20)</item>
<item>Cáncer (Junio 21 - Julio 22)</item>
<item>Leo (Julio 23 - Agosto 22)</item>
<item>Virgo (Agosto 23 - Sep. 22)</item>
<item>Libra (Sep. 23 - Oct. 22)</item>
<item>Escorpio (Oct. 23 - Nov. 21)</item>
<item>Sagitario (Nov. 22 - Dic. 21)</item>
<item>Capricornio (Dic. 22 - Ene. 19)</item>
</array>
EDIT2 : @Jorgesys
Clase
public class SeccionActivity extends Activity implements AdapterView.OnItemSelectedListener {
private TextView editTextZodiaco;
String[] zodiaco = getResources().getStringArray(R.array.titulos_zodiaco);
int flags[] = {R.drawable.icon_zod_elig, R.drawable.icon_zod_no, R.drawable.icon_zod_acuario, R.drawable.icon_zod_piscis, R.drawable.icon_zod_aries, R.drawable.icon_zod_tauro, R.drawable.icon_zod_geminis, R.drawable.icon_zod_cancer, R.drawable.icon_zod_leo, R.drawable.icon_zod_virgo, R.drawable.icon_zod_libra, R.drawable.icon_zod_escorpio, R.drawable.icon_zod_sagitario, R.drawable.icon_zod_capricornio};
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 1) {
editTextZodiaco.setText(" ");
} else if (position != 0) {
String description = zodiaco[position];
editTextZodiaco.setText(description.split(" ")[0]);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activit_seccion);
editTextZodiaco = (TextView) findViewById(R.id.editTextZodiaco);
Spinner spin = (Spinner) findViewById(R.id.spinnerzodiaco);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), flags, zodiaco);
spin.setOnItemSelectedListener(this);
spin.setAdapter(customAdapter);
}
}
Your array is empty:
and try to get an element:
Eliminate obtaining the data from the array within
getView()
, the correct thing is thatAdapter
when it is instantiated it has the necessary data.In your code you would make the following change: