2011-07-22 62 views
1

我試圖刷新ListView從單擊按鈕後從ArrayList<String>獲取文本。Android單擊按鈕後刷新列表視圖

在點擊之前,我的ListView填充了ArrayList(我使用subList(0,1))的第一個字符串。

我只想找到arrayList中的下一個字符串,並將其放在ListView上。我嘗試了以下方法,但它返回錯誤將適配器放入onClick方法中。

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<String> strings2 = new ArrayList<String>(); 
ArrayList<String> risposte = new ArrayList<String>(); 

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



@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<String>(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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    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<NameValuePair> nameValuePairs2 = new ArrayList<NameValuePair>(); 

     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<jArray.length();i++) 
     {       
      domande=jArray.getString(i); 
      strings2.add(domande);  
     } 
     for(int k=0;k<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<String>(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)); 

} 
} 

任何人都可以幫助我嗎?

+0

它更好地把問題出現的部分代碼與logcat說錯誤 – Rasel

+0

問題不在於logcat。您可以在上面留言發佈的代碼見://這裏我把我的適配器,但是它給了我錯誤 ls1.setAdapter(新ArrayAdapter (這一點, android.R.layout.simple_list_item_1, strings2.subList(0 ,1))); ls1.setTextFilterEnabled(true); 這是Eclipse的一個問題,特別是onClick()方法。看來我不能在裏面聲明適配器。我不知道爲什麼。 – Andre

回答

1
ls1.setAdapter(new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, 
     strings2.subList(0, 1))); 

代替this,其是OnClickListener(),通過該應用程序上下文。

Context context = this; 

並傳遞context變量,而不是this

這可以通過將應用程序上下文定義監聽器之前完成。

+0

謝謝soooo!我真的很感激。我還有另一個問題:做到這一點,我可以刷新我的單選按鈕?如果有可能,我該怎麼做?感謝loottt! – Andre

+0

如果您有問題,請格式化一個新問題。另外,儘量讓你的問題容易閱讀和直接。在確定自己無法弄清楚自己的問題之前,儘可能多地進行調查。 –