在textView中设置文本

时间:2014-03-11 18:18:43

标签: android textview

我有第二个查询在线数据库的活动,我想设置TextView的文本,但我不能。

这是第二项活动的代码:

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{
            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);
            }
            TextView text = (TextView) sendQuery.findViewById(R.id.textView10);
            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);



}
}

3 个答案:

答案 0 :(得分:1)

您正在catch块中设置TextView - 只有try块抛出异常时才会执行此catch块。由于try块在您的情况下工作正常,因此永远不会执行此catch块。在相应的try块中设置TextView,一切都会正常工作。

编辑:使您的代码如下:

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)
        {
        }

答案 1 :(得分:0)

我认为问题可能是你在设置textview的值而不等待查询返回响应。因此您可能无法设置正确的值。

答案 2 :(得分:0)

首先,如上所述,您在catch块中设置TextView的文本,这有很多原因。 其次,定义textview并将其添加到带有GONE或INVISIBLE可见性的catch块之外的fatherLayout上,并在catch块中设置它的文本并将可见性更改为VISIBLE。

此外,所有这些内容必须在您的请求完成后完成。

相关问题