gpt4 book ai didi

Android setText 无法显示来自数据库的字符串

转载 作者:行者123 更新时间:2023-11-29 01:29:26 24 4
gpt4 key购买 nike

我的应用程序可以选择数据并将其显示到 Log.e 中,但不能将其设置到 TextView 中。这是什么原因?

我想在 OnClick 事件之后立即显示数据,将可见性设置为可见。

final TextView txv_yes = (TextView) findViewById(R.id.vou);
final TextView txv_no = (TextView) findViewById(R.id.nao_vou);
final TextView txv_maybe = (TextView) findViewById(R.id.talvez);
show_count = (RelativeLayout) findViewById(R.id.show_block);

count_vou = (TextView) findViewById(R.id.count_vou);
count_nao = (TextView) findViewById(R.id.count_nao_vou);
count_talvez = (TextView) findViewById(R.id.count_talvez);

txv_yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
yes = txv_yes.getText().toString();
new insertYes().execute();
new select().execute();

runOnUiThread(new Runnable() {
public void run() {
setVisible();
}
});

}
});

public class select extends AsyncTask<String, Boolean, Boolean>{

@Override
protected Boolean doInBackground(String... params) {

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("e_id", subString));

try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website.com/includes/select_counter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}

try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(inputStream,"UTF-8"));
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
inputStream.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}

try
{
JSONObject json_data = new JSONObject(result);
get_yes = (json_data.getString("Vou"));
get_no = (json_data.getString("Nao_Vou"));
get_maybe = (json_data.getString("Talvez"));
Log.e("pass 3", "Vou : "+ get_yes);
Log.e("pass 4", "Não Vou : "+ get_no);
Log.e("pass 5", "Talvez: "+ get_maybe);

}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}

return null;
}
}

public boolean setVisible() {
show_count.setVisibility(View.VISIBLE);
count_vou.setText(get_yes);
count_nao.setText(get_no);
count_talvez.setText(get_yes);

return true;

}

在 Logs.e 之后,我在 try/catch 中有这段代码,但问题是一样的:

count_vou.setText(get_yes);
count_nao.setText(get_no);
count_talvez.setText(get_yes);

最佳答案

setVisible() 在服务器调用完成之前运行,它在您实际获取值之前设置值。

因此,不要在触发异步任务后立即调用此代码,而是在异步任务中调用,在 Log 语句之后立即调用。您可以使用异步任务的 onPostExecute 方法。

runOnUiThread(new Runnable() {      public void run() {        setVisible();                }            });}

关于Android setText 无法显示来自数据库的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065359/

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