【问题标题】:Android setText unable to show string coming from the databaseAndroid setText 无法显示来自数据库的字符串
【发布时间】: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


【解决方案1】:

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

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

runOnUiThread(新可运行(){ 公共无效运行(){ 设置可见(); } }); }

【讨论】:

【解决方案2】:

在您的异步任务完成之前调用 setVisible() 方法。因此您无法在 setVisible() 方法中获取数据库值。

在你的 select asynctask 中实现 onPostExecute 方法,然后在其中调用 setVisible() 方法。

@Override
   protected void onPostExecute(String result) {
      setVisible();
   }

希望对你有帮助!

【讨论】:

    【解决方案3】:

    尝试使用this.count_vou.setText(get_yes);activityname.this.count_vou.setText(get_yes)

    您似乎在 runOnUI 线程中使用它,但它无法在 runOnUi 方法中访问。

    【讨论】:

      【解决方案4】:

      解决方案 1。选择异步任务完成后调用 setVisible() 函数。移除 Runnable 线程

      新的选择()。执行(); 设置可见();

      解决方案 2。你也可以在 asynctask 中调用 setVisible()

      @Override

      protected void onPostExecute(Boolean result) {
      
          super.onPostExecute(result); 
          setVisible();
      
         }
      

      【讨论】:

        【解决方案5】:

        解决方案 1:- 在 AsyncTask 中分配所有值后调用 setVisible() 方法。 代码如下。从 onClickListener 中移除 setVisible() 方法调用

        试试下面的代码。 注意:- 您不能更改后台任务的 UI。为此,您必须添加 onPostExecute()。

        公共类选择扩展 AsyncTask{

        @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;
        }
        @Override
        
        
        protected void onPostExecute(String result) {
          setVisible(); }}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-09-20
          • 1970-01-01
          • 2016-10-05
          • 1970-01-01
          • 2014-05-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多