I have a Listview
that brings me a text field from a Web server and shows it in the respective 'listview now the issue is that I want to add an icon plus the text to the lisview, but we must consider that the icon will be added as long as it finds text.
private class AsyncRefrescar extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(VerPreguntas.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
url = new URL("http://bdauditorio.esy.es/Verpregunta/mostrarpre.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
if (response_code == HttpURLConnection.HTTP_OK) {
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return (result.toString());
} else {
return ("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
if(result.equals("unsuccessful")) {
final AlertDialog.Builder alertaDeError = new AlertDialog.Builder(VerPreguntas.this);
alertaDeError.setTitle("Error");
alertaDeError.setMessage("Ups, no se han podido cargar las preguntas. Intentelo de nuevo.");
alertaDeError.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertaDeError.create();
alertaDeError.show();
}else{
//Existen Datos
List<String> preguntas = new ArrayList<String>();
//Parsea la respuesta obtenida por el Asynctask
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(result);
} catch (JSONException e) {
e.printStackTrace();
}
for (int i=0; i<jsonArray.length(); i++) {
JSONObject preguntaDatos = null;
try {
preguntaDatos = jsonArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
String pregunta = null;
try {
pregunta = preguntaDatos.getString("pregunta");
} catch (JSONException e) {
e.printStackTrace();
}
preguntas.add(pregunta);
}
//crear el Adapter.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(VerPreguntas.this,
android.R.layout.simple_list_item_1, preguntas);
//Asignas el Adapter a tu ListView para mostrar los datos.
mostrarr.setAdapter(adapter);
}
}
}