title: Android refreshing listview after clicking button
body: <p>I'm trying to refresh a ListView getting text from an <code>ArrayList&lt;String&gt;</code> after clicking a button. </p>
  
  <p>Before clicking, my <code>ListView</code> is filled with the first string found in the <code>ArrayList</code> (I did that using <code>subList(0,1)</code>). </p>
  
  <p>All I want is to find the next string in the <code>arrayList</code> and put it on the <code>ListView</code>. I tried the following but it returns error putting the adapter into the <code>onClick</code> method. </p>
  
  <pre><code>    package com.android.quizzle;
  
      import java.io.BufferedReader;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.util.*;
  
      import org.apache.http.HttpEntity;
      import org.apache.http.HttpResponse;
      import org.apache.http.NameValuePair;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.entity.UrlEncodedFormEntity;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.message.BasicNameValuePair;
      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;
  
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.ArrayAdapter;
      import android.widget.Button;
      import android.widget.ListView;
      import android.widget.RadioButton;
      import android.widget.RadioGroup;
      import android.widget.TextView;
      import android.widget.Toast;
  
      public class MainMenuActivity extends Activity {
  
  
  
  ListView ls1;
  
  String result = null;
  
  String result2 = null;
  
  InputStream is = null;
  
  StringBuilder sb=null;
  
  StringBuilder sb2=null;
  
  JSONArray jArray;
  
  String domande;
  
  String risposta;
  
  RadioGroup radioGroup;
  
  private RadioButton radio1;
  
  private RadioButton radio2;
  
  private RadioButton radio3;
  
  private RadioButton radio4;
  
  private Button nextQuest;
  
  TextView t1;
  
  ArrayList&lt;String&gt; strings2 = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; risposte = new ArrayList&lt;String&gt;();
  
  ArrayList&lt;String&gt; risp = new ArrayList&lt;String&gt;();
  
  
  
  @Override
  public void onCreate(Bundle icicle)
  
  {
      Bundle b = getIntent().getExtras();
      String user = b.getString("il dato inserito è: " );
      Toast.makeText(getBaseContext(), "Benvenuto: " + user ,
              Toast.LENGTH_LONG).show();
  
      //array list per memorizzare l'output json
  
  
           super.onCreate(icicle);
           setContentView(R.layout.managerdb);
           ls1 = (ListView) findViewById(R.id.list);
           radioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
           radio1 = (RadioButton)findViewById(R.id.radioButton1);
           radio2 = (RadioButton)findViewById(R.id.radioButton2);
           radio3 = (RadioButton)findViewById(R.id.radioButton3);
           radio4 = (RadioButton)findViewById(R.id.radioButton4);
          //prossima domanda
           nextQuest = (Button)findViewById(R.id.buttonNextQuest);
           t1 = (TextView)findViewById(R.id.TextView01);
  
          nextQuest.setOnClickListener(new OnClickListener() 
          {
              public void onClick(View v) 
              {
                  String risp = null;
                  ListView ls1;
                  if(v == nextQuest)
                  {
  
                  if(radio1.isChecked() == true)
                  {
                          risp=risposte.get(0);
                          Toast.makeText(getBaseContext(), risp ,
                                  Toast.LENGTH_LONG).show();
      //HERE I PUT MY ADAPTER BUT IT GIVES ME ERROR
  
      ls1.setAdapter(new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_list_item_1,
              strings2.subList(0, 1)));
      ls1.setTextFilterEnabled(true);
  
                  }
                  if(radio2.isChecked() == true)
                  {
                          risp=risposte.get(1);
                          Toast.makeText(getBaseContext(), risp ,
                                  Toast.LENGTH_LONG).show();
  
                  }
                  if(radio3.isChecked() == true)
                  {
                      risp=risposte.get(3);
                  }
                  if(radio4.isChecked() == true)
                  {
                      risp=risposte.get(4);
                  }
                  }
  
  
              }
  
  
  
  
           });
  
  
  
      // query post.
  
      ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
      nameValuePairs.add(new BasicNameValuePair("username", user));
  
  
      // http post
  
      try {
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(
                  "http://labinform.altervista.org/Android/quiz.php");
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          HttpResponse response = httpclient.execute(httppost);
          HttpEntity entity = response.getEntity();
          is = entity.getContent();
      } catch (Exception e) {
          Log.e("log_tag", "Error in http connection " + e.toString());
      }
  
      // convert response to string
  
      try {
  
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "iso-8859-1"), 8);
          sb = new StringBuilder();
          sb.append(reader.readLine());
          String line;
          while ((line = reader.readLine()) != null) {
              sb.append(line);
  
          }
          is.close();
          result = sb.toString();
  
  
  
          if(result.contains("0") || result.contains("1"))
         { 
             Toast.makeText(MainMenuActivity.this, "Seleziona una categoria di apprendimento" , Toast.LENGTH_LONG).show();               
         }
          else{
             Toast.makeText(MainMenuActivity.this, "Non hai ancora compilato un quiz" , Toast.LENGTH_LONG).show();  
                      retrieveArray();
          }}
      catch (Exception e) {
          Log.e("log_tag", "Error converting result " + e.toString());
      }
      }
  
  
      private void retrieveArray() {
  
          //arrayList per memorizzare il profilo
  
          //nuova connessione alla pagina contenente i quiz.
          ArrayList&lt;NameValuePair&gt; nameValuePairs2 = new ArrayList&lt;NameValuePair&gt;();
  
          try {
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(
                      "http://labinform.altervista.org/Android/tag.php");
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs2));
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
          } catch (Exception e) {
              Log.e("log_tag", "Error in http connection " + e.toString());
          }
          try {
  
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      is, "iso-8859-1"), 8);
              sb2 = new StringBuilder();
              sb2.append(reader.readLine());
              String line;
              while ((line = reader.readLine()) != null) {
                  sb2.append(line);               
              }
              is.close();
              result2 = sb2.toString();
  
  
          }
          catch (Exception e) {
              Log.e("log_tag", "Error in http connection " + e.toString());
          }
          //parsing data to Json
  
  
      try{
  
          JSONObject jObject = new JSONObject(result2);
  
          jArray = jObject.getJSONArray("domande");
          JSONArray jArray2 = jObject.getJSONArray("risposte");
  
  
          for(int i=0;i&lt;jArray.length();i++)
          {                        
              domande=jArray.getString(i);
              strings2.add(domande);     
          }
          for(int k=0;k&lt;jArray2.length();k++){
          risposta = jArray2.getString(k);
          risposte.add(risposta);
          }
  
  
      }catch(JSONException e1){
  
      }catch(Exception e1){
          Log.e("log_tag", "error " + e1.toString());
  
      } 
  
  
  
      ls1.setAdapter(new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_list_item_1,
              strings2.subList(0, 1)));
      ls1.setTextFilterEnabled(true);
  
  
      radio1.setText(risposte.get(0));
      radio2.setText(risposte.get(1));
      radio3.setText(risposte.get(2));
      radio4.setText(risposte.get(3));
  
  }
  }
  </code></pre>
  
  <p>Can anyone help me? </p>