我一直致力于个人酒吧点击列表,该列表使用JSOUP为列出的啤酒解析网页,更重要的是,这里是最后一次更改日期。这里的问题是,当我加载活动时,解析的内容不会出现,直到我手动干预并将手指放在导致另一个活动的按钮上。 在我的“公共类MainScreen ...”中,我的相关代码如下所示:
public class MainScreen extends ExpandableListActivity
implements OnChildClickListener {
ArrayList<String> mData = new ArrayList<String>();
ListView mListView; ArrayAdapter<String> mAdapter;
ArrayList<String> ArrayOfBeerNames;
ArrayList<Beer> ArrayOfBeers;
ExpandableListView expandbleLis;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.taplist);
expandbleLis = getExpandableListView();
expandbleLis.setDividerHeight(2);
expandbleLis.setGroupIndicator(null);
expandbleLis.setClickable(true);
ArrayOfBeerNames = new ArrayList<String>();
ArrayOfBeers = new ArrayList<Beer>();
new AsyncDerp().execute();
}
private class AsyncDerp extends AsyncTask<Void, Void, Void>
{
private ProgressDialog LoginProgressDialog = new ProgressDialog(MainScreen.this);
protected void onPreExecute()
{
LoginProgressDialog.setMessage("Fetching the up-to-date list...");
LoginProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
processUpdate();
return null;
}
private void processUpdate() {
String URL = "http://www.dogfish.com/restaurant/menus/brews-whats-on-tap.htm";
try {
Document doc = Jsoup.connect(URL)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
.get();
Elements updates = doc.select("p:matches(.+?)+p");
for(Element update: updates) {
String TheUpdateTextWeGot = update.text();
if((TheUpdateTextWeGot.contains("updated")))
{
String updatedate = "Current as of " + TheUpdateTextWeGot.substring(9, 20) + ".";
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(updatedate);
}
}
if(mData.size() == 0) {
mData.add("Empty result");
}
} catch (Exception ex) {
ex.printStackTrace();
mData.clear();
mData.add("Exception: " + ex.toString());
}
}
protected void onPostExecute(Void unused)
{
LoginProgressDialog.dismiss();
}
}
我意识到这里发生了一些事情,因为很多SE帖子都提醒了我。它使用Async下载页面并解析它,但应用程序应该自动显示数据,而不必干预并强制它这样做。
我在哪里错了?
非常感谢。
答案 0 :(得分:0)
试一下
public class MainScreen extends ExpandableListActivity
implements OnChildClickListener {
ArrayList<String> mData = new ArrayList<String>();
ListView mListView; ArrayAdapter<String> mAdapter;
ArrayList<String> ArrayOfBeerNames;
ArrayList<Beer> ArrayOfBeers;
ExpandableListView expandbleList;
Document doc;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.taplist);
expandbleLis = getExpandableListView();
expandbleLis.setDividerHeight(2);
expandbleLis.setGroupIndicator(null);
expandbleLis.setClickable(true);
ArrayOfBeerNames = new ArrayList<String>();
ArrayOfBeers = new ArrayList<Beer>();
new progression().execute();
}
public class progression extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog = new ProgressDialog(
MainScreen.this);
@Override
protected void onPreExecute() {
this.dialog.setCancelable(true);
this.dialog
.setMessage("Fetching the up-to-date list..., Please Wait.......");
this.dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
this.dialog.setProgress(0);
this.dialog.setMax(1000);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
/*
* This is run on a background thread, so we can sleep here or
* do whatever we want without blocking UI thread. A more
* advanced use would download chunks of fixed size and call
* publishProgress();
*/
String URL = "http://www.dogfish.com/restaurant/menus/brews-whats-on-tap.htm";
try {
doc = Jsoup.connect(URL)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
.get();
} catch (Exception ex) {
ex.printStackTrace();
//mData.clear();
//mData.add("Exception: " + ex.toString());
}
} catch (Exception e) {
//Log.e("tag", e.getMessage());
/*
* The task failed
*/
}
/*
* The task succeeded
*/
return null;
}
protected void onProgressUpdate(Void... params) {
super.onProgressUpdate(params);
}
@Override
protected void onPostExecute(final Void unused) {
super.onPostExecute(unused);
Elements updates = doc.select("p:matches(.+?)+p");
for(Element update: updates) {
String TheUpdateTextWeGot = update.text();
if((TheUpdateTextWeGot.contains("updated")))
{
String updatedate = "Current as of " + TheUpdateTextWeGot.substring(9, 20) + ".";
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(updatedate);
}
}
if(mData.size() == 0) {
mData.add("Empty result");
}
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
/*
* Update here your view objects with content from download. It is
* save to dismiss dialogs, update views, etc., since we are working
* on UI thread.
*/
}
}