gpt4 book ai didi

Android点击按钮后刷新 ListView

转载 作者:行者123 更新时间:2023-11-30 04:36:15 25 4
gpt4 key购买 nike

我正在尝试刷新从 ArrayList<String> 获取文本的 ListView单击按钮后。

点击之前,我的 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));

}
}

谁能帮帮我?

最佳答案

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

传递应用程序上下文,而不是 this,即 OnClickListener()。

这可以通过在定义监听器之前存储应用程序上下文来实现:

Context context = this;

并传递 context 变量而不是 this

关于Android点击按钮后刷新 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6793660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com