ArrayAdapter: Los datos de Json se enviarán a spinner

Hola, he creado una actividad que extrae datos de texto en formato json y se muestra en una vista de spinner. Pero estoy un poco confundido con la última parte. El contactList es un tipo ArrayList, ArrayAdapter no está aceptando contactList como su arugument. Este es mi codigo

public class RegisterForEventActivity extends Activity { private static String url = "http://10.0.2.2/Contacts.txt"; private static final String TAG_NAME = "name"; private static final String TAG_CONTACTS = "contacts"; JSONArray jsonArray = null; Spinner areaspinner; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.register_layout); // Hashmap for ListView ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); // Creating JSON Parser instance JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONObject json = jParser.getJSONFromUrl(url); try { // Getting Array of Contacts jsonArray = json.getJSONArray(TAG_CONTACTS); final String[] array_spinner = new String[jsonArray.length()]; // looping through All Contacts for(int i = 0; i < jsonArray.length(); i++){ JSONObject c = jsonArray.getJSONObject(i); // Storing each json item in variable String name = c.getString(TAG_NAME); // creating new HashMap HashMap<String, String> map = new HashMap<String, String>(); // adding each child node to HashMap key => value map.put(TAG_NAME, name); // adding HashList to ArrayList contactList.add(map); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, contactList); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); areaspinner.setAdapter(adapter); } } catch (JSONException e) { e.printStackTrace(); } } } 

Esto se debe a que está pasando una lista de HashMap , y no una matriz de String . Haga una matriz de String , añada sus datos de contacto y pase a la matriz.

En lugar de usar este

  ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

utilizar esta

  ArrayList<String> contactList = new ArrayList<String>(); 

¡Sí, lo descubrí!

 sp=(Spinner)findViewById(R.id.spinner1); // Hashmap for ListView // Creating JSON Parser instance JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONObject json = jParser.getJSONFromUrl(url); System.out.println("Hello"); try { // Getting Array of Contacts jsonArray = json.getJSONArray(TAG_CONTACTS); final String[] items = new String[jsonArray.length()]; // looping through All Contacts for(int i = 0; i < jsonArray.length(); i++){ JSONObject c = jsonArray.getJSONObject(i); // Storing each json item in variable String name = c.getString(TAG_NAME); items[i]=c.getString(TAG_NAME); System.out.println("Hello events "+items); } ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, items); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } 
  • Cargar imagen en ImageView de la URL almacenada en JSONString en Android
  • Cómo convertir el objeto de cursor en XML o JSON en android
  • Análisis del archivo json con gson
  • Android - cómo analizar json con el objeto en el índice
  • No se puede seleccionar una cuenta de Google Analytics diferente al generar el archivo de configuración de Google Analytics.
  • Cómo probar si un JSONObject es nulo o no existe
  • Http Get Request devuelve html?
  • Cómo analizar datos de 2 URL diferentes mediante el método asyncTask
  • Generar listview desde JSON en android
  • Obtención de imagen de la matriz de bytes en el objeto JSON a la aplicación de Android
  • Android Volley varias solicitudes
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.