getApplicationContext,来自资源的xml布局

时间:2012-08-15 18:33:05

标签: android textview android-xml

我被困在一段代码上。 我有一个PHP页面,从数据库中获取我的值,这一切都没问题。 然后在我的Activity中我将它返回到textview中显示,问题是。 我在我的xml中输入了背景,但它没有显示,

我也在文本视图中看到了 http://www.wvvzondag2.nl/android/leesverslag.php {“introtext”:“”我的数据库中的变量值“}

这是我的活动

public class Leesverslag extends Activity{
/** Called when the activity is first created. */

TextView txt;

public static final String KEY_121 = "http://www.wvvzondag2.nl/android/leesverslag.php"; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.leesverslag);
    // Create a crude view - this should really be set via the layout resources  
    // but since its an example saves declaring them in the XML.
    ScrollView rootLayout = new ScrollView(getApplicationContext());
    //txt = (TextView)findViewById(R.id.verslag);
    txt = new TextView(getApplicationContext());  
    rootLayout.addView(txt);  
    setContentView(rootLayout);
    // Set the text and call the connect function.  
    txt.setText("Connecting..."); 
    //call the method to run the data retrieval and convert html tag to (plain) text
    txt.setText(Html.fromHtml(getServerData(KEY_121))); 
}

private String getServerData(String returnString) {

   InputStream is = null;

   String result = "";
    //the year data to send
   String titelhoofd = getIntent().getStringExtra("titelverslag");
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("titel",titelhoofd));

    //http post
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(KEY_121);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            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),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }
    //parse json data
    try{
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++){
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","id: "+json_data.getString("introtext")
                    );
                    //Get an output to the screen
                    returnString += jArray.getJSONObject(i); 
            }
    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return returnString; 
}    

}

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    android:keepScreenOn="true"
    android:id="@+id/scrollView1"
    android:scrollbars="vertical"
    android:background="@drawable/bg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/verslag"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TextView>

    </LinearLayout>


</ScrollView>

我想看到文字和背景,但我似乎无法修复它。

有人可以帮我吗?

亲切的问候, 帕特里克

0 个答案:

没有答案