Android - 显示在异步任务中检索的字符串
我想要制作一个应用程序,该应用程序将从网站获取 HTML 并以线性布局显示应用程序内的某些字符串。 (我不想使用webView)。我检索的数据每天都在变化;但格式通常是相同的;仅页面上的项目数量不同。
我能够使用 jSoup (在异步任务中)来检索和解析我需要的字符串。我还可以使该字符串在 toast 消息中弹出;这样我就可以确认我得到了我想要的字符串。
我的问题是如何在应用程序中实际显示字符串?
我想显示两个不同的项目。名称,然后是其下面的描述。
例子:
(名称) 法式炸虾
(说明) 十二只虾,裹上日式面包糠,炸至金黄色。配鸡尾酒酱仅需 12.49 美元
下面是当前显示我想要的字符串的代码。
protected void onPostExecute(Document doc) {
Toast.makeText(RoundtheClockActivity.this, doc.select("p").get(0).toString(), Toast.LENGTH_SHORT).show();
}
现在我想要将视图更改为另一页。我想 String1 就是我想要显示的内容...代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/soupSpecial1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01"
android:text="@string/string1" />
</LinearLayout>
要添加,最初我有一个视图;但在我完成异步任务之后;我想改变一个新的观点。这将显示字符串。
public void onClick(View v) {
try {
new DownloadTask().execute("url");
} catch (Exception e) {
//logic for exception here
}
I want to make an application that will grab the HTML from a website and display certain strings all inside the app in a linear layout. (I do not want to use webView). The data that I am retrieving changes every day; however the format is generally the same; different in only the amount of items on the page.
I am able to use jSoup (in an Async Task) to retrieve and parse the strings that I need. I also can make the string pop up in a toast message; so I can confirm that I am getting the string that I want.
My question is how I can actually display the strings in the app?
There are two different items I want to display. A name, and then a description below it.
Example:
(Name) FRENCH FRIED SHRIMP
(Description) Twelve shrimp lightly breaded with Japanese style bread crumbs and deep fried to golden brown. Served with cocktail sauce ONLY $12.49
Below is the Code that is currently displaying a string that I want.
protected void onPostExecute(Document doc) {
Toast.makeText(RoundtheClockActivity.this, doc.select("p").get(0).toString(), Toast.LENGTH_SHORT).show();
}
Right now I am going to want to change views to another page.. And I suppose String1 is what I want to display... Code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/soupSpecial1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.01"
android:text="@string/string1" />
</LinearLayout>
To add, originally I have one view; but after I do the async task; I want to change to a new view. That will display the String.
public void onClick(View v) {
try {
new DownloadTask().execute("url");
} catch (Exception e) {
//logic for exception here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在应用程序中初始化 TextView,如下所示:
并在异步任务的 onPostExecute 中,执行以下操作:
initialize your TextView in your app as below:
and in your Async Task's onPostExecute, do the following: