2

My application can select and show the data into the Log.e but it cannot set it into the textViews. What´s the reason for this?

I want to show the data just after an OnClick event, setting visibility to visible.

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;

}

I had this block of code inside the try/catch jus after the Logs.e but the problem was the same:

count_vou.setText(get_yes);
count_nao.setText(get_no);
count_talvez.setText(get_yes);
madsongr
  • 653
  • 1
  • 11
  • 29

5 Answers5

1

setVisible() runs before the server call completes, and it's setting the values before you actually get the values.

So, instead of calling this code right after you trigger the async task, call in from within the async task, right after the Log statements. You can use the onPostExecute method of the async task.

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

}
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

Try to use this.count_vou.setText(get_yes); or activityname.this.count_vou.setText(get_yes)

It seems you are using in runOnUI thread and it doesn't getting access inside runOnUi method.

Abhinav Singh Maurya
  • 3,313
  • 8
  • 33
  • 51
Gopal Sharma
  • 300
  • 4
  • 14
0

setVisible() method call before your async task is completed. so you can not get database values in setVisible() method.

Implement onPostExecute method in your select asynctask then call setVisible() method inside it.

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

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
0

Solution 1. Call setVisible() function after select asynctask finish. Remove Runnable thread

new select().execute(); setVisible();

Solution 2. You can also call setVisible() in asynctask

@Override

protected void onPostExecute(Boolean result) {

    super.onPostExecute(result); 
    setVisible();

   }
0

Solution 1:- Call setVisible() Method after assigning all values in AsyncTask. Code as below. Remove setVisible() method call from onClickListener

Try this below code. Note :- You cannot change UI on background task. For that you have to add onPostExecute().

public class select extends 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(); }}