文本在TextView中不可见

时间:2014-03-14 16:14:26

标签: android

我无论如何都不能显示TextView的文本。我已经尝试过各种各样的方法,因为它是一个静态方法,我必须调用这个函数:

sendQuery.text.setText(provola);

这是二级课程:

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;
    }
}

相反,这是主要活动:

 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);



}
}

变量中的值&#34; provola&#34;存在,但没有显示。感谢。

1 个答案:

答案 0 :(得分:0)

resLayout是Textview类型的变量。 你应该这样做,而不是append: -

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

你说你正在获得价值&#34;挑衅&#34;在变量&#34; res&#34;

String res = sendQuery.send("SELECT * FROM contatti", null);
resLayout.append(res); // Dont do this
resLayout.setText(res); // Do this