我有第二个查询在线数据库的活动,我想设置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);
}
}
到目前为止还没有人能够帮助我,我无法弄清楚我错在哪里。感谢
修改
问题在于第二个活动,这里TextView text =(TextView)sendQuery.findViewById(R.id.textView10); text.setText(provola);
答案 0 :(得分:1)
将resLayout.append(res);
更改为resLayout.setText(res);
答案 1 :(得分:0)
setText() is a method that you have to call when you want to set some text in textview.
so you have to use
resLayout.setText(res);
if you are getting any exception,error or nothing is set on your view.then you have to check your string value for null or empty.
if(!res.equalignorecase("null" && !res.equalignorecase(""))){
resLayout.setText(res);
}else{
resLayout.setText("set some dummy text");
}
答案 2 :(得分:0)
更改
resLayout.append(res);
到
resLayout.setText(res);