Android:视图之间的 onClick 并利用 Asynctask

发布于 2024-11-15 17:18:25 字数 3094 浏览 3 评论 0原文

我花了近 40 个小时完成的项目即将完成,但我还有最后一项任务要完成。我之前遇到的问题是,当用户单击拉取新闻 RSS 源时,应用程序有时会挂起,因为它试图同时加载活动和源。这个问题已通过 asynctask 解决。

除此之外,我还有四个按钮可以在不同的提要之间切换,并且本质上只是修改视图。这消除了重新加载屏幕或新活动的情况。好吧,当我最初启动此活动时,异步任务的工作方式与我想要的完全一样,但它并不像复制粘贴到底部的每个不同视图那么简单。如何在每个不同的按钮/视图下实现 asynctask。您将在底部看到 onClick。

这是异步任务部分。

public class RssLoadingTask extends AsyncTask<Void, Void, Void> {

         @Override
         protected void onPostExecute(Void result) {
          // TODO Auto-generated method stub
          displayRss();
         }

         @Override
         protected void onPreExecute() {
          // TODO Auto-generated method stub
          preReadRss();
         }

         @Override
         protected void onProgressUpdate(Void... values) {
          // TODO Auto-generated method stub
          //super.onProgressUpdate(values);
         }

         @Override
         protected Void doInBackground(Void... arg0) {
          // TODO Auto-generated method stub
          readRss();
          return null;
         }

        }

下面是 onClick 部分。显然,通过上面的内容,我在初始活动中的正确位置都有了每个 readRss、preReadRSS 等,但是为什么 Java 不允许我将这些相同的 asynctask 任务放在 onClick 部分中?我只是对修饰符了解不够,还不知道如何放置它们。仍然学到很多东西。

public void onClick(View v) {

if (v == hawknation) {


    try {

          URL rssUrl = new URL("http://www.rss.com/feed");
          SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
          SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
          XMLReader myXMLReader = mySAXParser.getXMLReader();
          RSSHandler myRSSHandler = new RSSHandler();
          myXMLReader.setContentHandler(myRSSHandler);
          InputSource myInputSource = new InputSource(rssUrl.openStream());
          myXMLReader.parse(myInputSource);

          myRssFeed = myRSSHandler.getFeed();

         } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (ParserConfigurationException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (SAXException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }}


   {

         if (myRssFeed!=null)
         {
          TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
          TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
          TextView feedPubdate = (TextView)findViewById(R.id.feedpubDate);
          TextView feedLink = (TextView)findViewById(R.id.feedlink);
          feedTitle.setText(myRssFeed.getTitle());
          feedDescribtion.setText(myRssFeed.getDescription());
          feedPubdate.setText(myRssFeed.getPubdate());
          feedLink.setText(myRssFeed.getLink());

          MyCustomAdapter adapter =
           new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
          setListAdapter(adapter);

         }       
     }

My project that I've spent almost 40hrs on is almost complete, but I have one last task to accomplish. My previous problem what that when users clicked to pull up a news RSS feed that app would sometimes hang because it both tried to load the activity and the feed at the same time. This was resolved with asynctask.

Now along with this, I have four buttons that switch between different feeds and essentially just modifying the view. This eliminates reloading the screen or new activity. Well, when I initially fire up this activity, the asynctask works exactly like I want, but it's not as simple as copying pasting to each of the different views at the bottom. How do I implement asynctask under each of those different buttons/views. You'll see the onClick at the bottom.

Here is the asynctask section.

public class RssLoadingTask extends AsyncTask<Void, Void, Void> {

         @Override
         protected void onPostExecute(Void result) {
          // TODO Auto-generated method stub
          displayRss();
         }

         @Override
         protected void onPreExecute() {
          // TODO Auto-generated method stub
          preReadRss();
         }

         @Override
         protected void onProgressUpdate(Void... values) {
          // TODO Auto-generated method stub
          //super.onProgressUpdate(values);
         }

         @Override
         protected Void doInBackground(Void... arg0) {
          // TODO Auto-generated method stub
          readRss();
          return null;
         }

        }

And below here is the onClick section. Obviously with the above i have each readRss, preReadRSS, etc...in the right places in the initial activity, but why won't Java allow me to place these same asynctask tasks in the onClick section? I just don't know enought about modifiers yet to know how to place them. Still learning a lot.

public void onClick(View v) {

if (v == hawknation) {


    try {

          URL rssUrl = new URL("http://www.rss.com/feed");
          SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
          SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
          XMLReader myXMLReader = mySAXParser.getXMLReader();
          RSSHandler myRSSHandler = new RSSHandler();
          myXMLReader.setContentHandler(myRSSHandler);
          InputSource myInputSource = new InputSource(rssUrl.openStream());
          myXMLReader.parse(myInputSource);

          myRssFeed = myRSSHandler.getFeed();

         } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (ParserConfigurationException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (SAXException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }}


   {

         if (myRssFeed!=null)
         {
          TextView feedTitle = (TextView)findViewById(R.id.feedtitle);
          TextView feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
          TextView feedPubdate = (TextView)findViewById(R.id.feedpubDate);
          TextView feedLink = (TextView)findViewById(R.id.feedlink);
          feedTitle.setText(myRssFeed.getTitle());
          feedDescribtion.setText(myRssFeed.getDescription());
          feedPubdate.setText(myRssFeed.getPubdate());
          feedLink.setText(myRssFeed.getLink());

          MyCustomAdapter adapter =
           new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
          setListAdapter(adapter);

         }       
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-11-22 17:18:25

我最终没有使用我的观点。我只是通过在每个按钮上打开不同的活动来在视图之间切换。

I ended up not using my view in question. I just switch between views by opening a different activity on each button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文