0

I can not in any way to display the text of the TextView. I have tried in various ways and since it is a static method do I have to call this function:

sendQuery.text.setText(provola);

this is the secondary class:

public class sendQuery extends main {
    // ///////// Public method to send Query ///////////
    public static String send(String query, Activity sendQuery) {
        String result = "0";
        InputStream is = null;
        String weekDayVal = null;
        String provola = null;
        // the query to send
        ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();

        querySend.add(new BasicNameValuePair("querySend", query));

        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://locali.altervista.org/php/locali.php");
            httppost.setEntity(new UrlEncodedFormEntity(querySend));
            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);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
            try {
                TextView text = (TextView) sendQuery
                        .findViewById(R.id.textView10);
                JSONArray weekDetails = new JSONArray(result); // Your response
                                                                // string
                for (int index = 0; index < 1/* weekDetails.length() */; index++) {
                    JSONObject tempWeekDetail = weekDetails
                            .getJSONObject(index);
                    weekDayVal = tempWeekDetail.getString("Lunedi");// Value for
                                                                    // Monday
                    // added this Log which you can view from LogCat. also
                    // changed above variable name
                    Log.i("Resp Value", "Moday Value " + weekDayVal);
                    JSONObject provino = weekDetails.getJSONObject(index);
                    provola = provino.getString("Martedi");// Value for Monday
                    // added this Log which you can view from LogCat. also
                    // changed above variable name
                    Log.i("Resp Value", "Moday Value " + provola);
                    text.setText(provola);
                }
            } catch (Exception e) {

            }
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result: " + e.toString());
        }

        Log.i("SendQUERY", result);
        return result;
    }
}

instead this is the main activity:

 public class main extends Activity {
/** Called when the activity is first created. */
@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView resLayout = (TextView) findViewById(R.id.res);


    String res = sendQuery.send("SELECT * FROM contatti", null);

    resLayout.append(res);



}
}

the value in the variable "provola" is present but nothing is displayed. Thanks.

Giacomoni
  • 1,468
  • 13
  • 18
ddd
  • 3
  • 5
  • where is TextView `text` added to the activity? – Raghunandan Mar 14 '14 at 16:17
  • here text.setText(provola); – ddd Mar 14 '14 at 16:20
  • does main.xml have a textview with id `textView10`? and `send("SELECT * FROM contatti", null);` its null ad you have `send(String query, Activity sendQuery) {` – Raghunandan Mar 14 '14 at 16:21
  • 1
    A bigger problem seems to be that you are doing this network stuff on the main thread which is a no no. You should do that in a background thread then update the `UI` once it has finished. So you are probably getting an exception and it is returning `null`. – codeMagic Mar 14 '14 at 16:22
  • @ddd does your app crash??\ – Raghunandan Mar 14 '14 at 16:23
  • yes there is a textview with that id, where is the problem? – ddd Mar 14 '14 at 16:24
  • I would recommend moving your code to an `AsyncTask`. [See this answer about using an interface with an AsyncTask](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) And [this one](http://stackoverflow.com/questions/18898039/using-asynctask/18898105#18898105) will show you how to set up an `AsyncTask` if you are unfamiliar – codeMagic Mar 14 '14 at 16:26
  • The app does not crash but I can not display the message in the TextView – ddd Mar 14 '14 at 16:26
  • check if the textColor and your `TextView` background aren't the same, in your xml !!! – S.Thiongane Mar 14 '14 at 16:26
  • In `sendQuery()` you have an empty `catch` block...bad idea. You should at least put a log there. Then check if you are getting an `exception` which I think you will find is what is happening. – codeMagic Mar 14 '14 at 16:27
  • there is not a problem of color – ddd Mar 14 '14 at 16:35
  • @ddd we are all trying to help you so asking for help really isn't useful. Have you looked at my suggestions yet to see if those help? – codeMagic Mar 14 '14 at 16:37
  • Yes, why I have no error but I can not display the text in the TextView? – ddd Mar 14 '14 at 17:01

1 Answers1

0

resLayout is a variable of type Textview. You should do like this instead of append :-

resLayout.setText("Your String goes here in quotes or a variable without the quotes");

You say that you are getting the value "provoke" in variable "res"

String res = sendQuery.send("SELECT * FROM contatti", null);
resLayout.append(res); // Dont do this
resLayout.setText(res); // Do this
Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66