点击按钮后,我正在尝试刷新ListView从ArrayList<String>
获取文本。
在点击之前,我的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 :(得分: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
。