2012-08-15 98 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