首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >来自资源的具有xml布局的getApplicationContext

来自资源的具有xml布局的getApplicationContext
EN

Stack Overflow用户
提问于 2012-08-16 02:33:05
回答 2查看 804关注 0票数 0

我被一段代码卡住了。我有一个PHP页面,它从数据库获取我的值,这没问题。然后在我的活动中,我返回它以在文本视图中显示,问题是。我在我的xml中获得了一个背景,但它没有显示,

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

这是我的活动

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>

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

有人能帮我一下吗?

致以亲切的问候,帕特里克

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-17 19:38:38

问题是您正在用setContentView(R.layout.leesverslag);设置一个初始的内容视图,然后继续创建一个新视图,用来覆盖第一个视图。

尝试替换以下内容:

代码语言:javascript
复制
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..."); 

有了这个:

代码语言:javascript
复制
txt = (TextView)findByViewId(R.id.*yourtextviewid*)
txt.setText("Connecting...");

这会将初始内容视图上的TextView分配给txt

票数 1
EN

Stack Overflow用户

发布于 2012-08-17 19:58:28

您不能将多个视图直接添加到滚动视图中,只能像在xml中那样引用LinaerLayout

代码语言:javascript
复制
<?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>

然后在on create方法中

代码语言:javascript
复制
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);

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11975010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档