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(); } } }
- El archivo google-services.json falta en la carpeta raíz del módulo. El complemento de servicios de Google no puede funcionar sin él
- Android cómo convertir json array a string array
- Crear cadena JSON con GSON
- Formato JSON Body for Retrofit de una sola cadena sin modelo
- Org.json.JSONObject no se puede convertir en JSONArray en android
- La manera más eficiente de analizar JSON con Android
- El análisis de Json con gson devuelve un objeto nulo
- ¿Objeto raíz o no? ¿Cuál es la mejor práctica para las respuestas de la API?
- ¿Cómo enviar un objeto JSON sobre solicitud con Android?
- Retrofit sigue olvidando mis cookies :( Android
- Google Analytic "GET A CONFIGURATION FILE" no me permite seleccionar mi cuenta
- ¿Cuál usar? JSONObject de org.json VS JsonObject de javax.json
- BufferedReader mientras lee datos Json
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(); }
- Comprobación de las suscripciones de google android desde el lado del servidor
- ¿Puedo incluir la biblioteca c ++ en la extensión nativa de Adobe Air para móviles IOS / Android?