0

我被困在一段代码上。我有一个 PHP 页面,它可以从数据库中获取我的值,这一切都很好。然后在我的活动中,我将它返回以显示在文本视图中,问题是。我在我的 xml 中有一个背景,但它没有显示,

我还在我的文本视图中看到 http://www.wvvzondag2.nl/android/leesverslag.php {"introtext":""my variable values from the database"}

这是我的活动

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>

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

有人可以帮我吗?

亲切的问候,帕特里克

4

2 回答 2

1

您不能直接向滚动视图添加多个视图,只需像 xml 中的方式一样引用您的 LinaerLayout

<?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>

然后在你的创建方法中

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

}

于 2012-08-17T11:58:27.947 回答
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.

于 2012-08-17T11:38:37.530 回答