【发布时间】:2015-08-18 06:45:31
【问题描述】:
我的应用程序可以选择数据并将其显示到 Log.e 中,但不能将其设置到 textViews 中。这是什么原因?
我想在 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与您的AsyncTask-s 不同步 -
尝试将
show_count.setVisibility(View.VISIBLE);与其他setText()语句一起放入 try/catch 中。 -
尝试在 asynctask 的 Post Execute 方法上调用 setVisible 方法
标签: android