2012-08-15 69 views
0

我困在一段代碼中。 我有一個PHP頁面從數據庫中獲取我的值,這一切都可以。 然後在我的活動中,我將它返回以textview顯示,問題在於。 我在XML有一個背景,但它沒有顯示,getApplicationContext與來自資源的xml佈局

而且我在TextView中 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> 

我想看到的只是文字和我的背景,但我似乎無法得到修復。

有人可以幫助我嗎?

親切的問候, 帕特里克

回答

1

的問題是,你是設置初始內容視圖與setContentView(R.layout.leesverslag);之後你繼續創造您使用覆蓋第一個以新的視角。

嘗試更換此:

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

與此:

txt = (TextView)findByViewId(R.id.*yourtextviewid*) 
txt.setText("Connecting..."); 

此分配的TextView這是您的初始內容視圖txt

+0

感謝您的幫助,現在我得到了我的ow n XML佈局,而不是默認值。 – 2012-08-19 19:59:18

1

不能添加多個視圖滾動視圖直接 只取像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:id="@+id/Linear" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</LinearLayout> 

然後在上創建方法您LinaerLayout參考

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.leesverslag); 
LinearLayout mLinearlayout= (LinearLayout) findViewById(R.id.Linear); 
    TextView txt = new TextView(this); 
    txt.setText("Connecting..."); 
    mLinearlayout.addView(txt); 

    Runnable postRunnable = new Runnable() { 

     @Override 
     public void run() { 
      txt.setText(Html.fromHtml(getServerData(KEY_121))); 

     } 
    }; 
    txt.post(postRunnable,500); 

}

+0

感謝您的幫助,現在我得到了自己的XML佈局,而不是默認的。 – 2012-08-19 19:59:43

+0

你必須回答它,以便其他尋找問題必須得到解決方案或使用上述的一個批准它,以便其他用戶可以使用它......... – 2012-08-20 05:26:52