English 中文(简体)
在AsyncTask 中通过字符串参数吗?
原标题:Android: Passing String parameters in AsyncTask?

I m 执行一个 Android 应用程序, 以装入 RSS 种子 。 直到我决定为显示两个按钮和试图将字符串值传递到 AsyncTask 时, 添加一个新的屏幕 。 想法是这样 : 屏幕将显示两个用户可选择的按钮。 一旦用户点击其中的一个按钮, 它将引用 AsyncTask 和字符串值( url) 来 readRSS( url), 然后将 rss feed 显示为 displayRSS( url) 。 readRSS( url) 将相应装入 RSS 种子 。

我的代码如下:

import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class RSSProActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rss);
        startLoadRSS();

    }

    private void startLoadRSS(){
        new RSS_Load().execute();
    }

    //
    private void preReadRSS()
    {
        Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
    }

        private StringBuffer readRSS(String url)
        {
            //depends on the inputs, will set the String value accordingly. It should be "url" but using the default value at the moment.

            String urlStr = "http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml";
            AndroidSAXFeedParser saxFeedParser = new AndroidSAXFeedParser(urlStr);
            List<RSSItem> parse = saxFeedParser.parse();
            StringBuffer sb = new StringBuffer();
            sb.append(parse.size()+" items found");
            for(RSSItem item:parse){
                sb.append(item.getTitle()+"posted at "+item.getDate());
                sb.append(item.getDescription());
            }
            return sb;
        }

        private void displayRSS()
        {
            StringBuffer sb = readRSS(url);
            TextView textView = new TextView(this);
            textView.setText(sb.toString());
            setContentView(textView);
            Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
        }


        //AsyncTask
        public class RSS_Load extends AsyncTask<Void, Void, Void>{
           @Override
           protected void onPreExecute() {
              super.onPreExecute();
              preReadRSS();
           }

           @Override
           protected Void doInBackground(Void... arg0) {
             readRSS();
                       // should be readRSS(url) here
               return null;
           }

           @Override
           protected void onProgressUpdate(Void... values) {
              //super.onProgressUpdate(values);
          //    updateProgressBar(values[0]);

           }

           @Override
           protected void onPostExecute(Void result) {
              super.onPostExecute(result);
           //   dismissProgressBar();
                // should be displayRSS(url) here
            displayRSS();

           }               

 }
}

我试图更改 RSS_ Load 中的值扩展 AsyncTashsk, 但给了我编译错误 。 所以我更改了受保护的 Void doInBackforlor (String... url), 然后将 url 转换为 readRSS( url), 但它也行不通 。 我该怎么做?

提前感谢!

最佳回答

意思是它实际上穿过了这种类型的阵列。所以如果你把它修改为:

protected Void doInBackground(String... arg0)

你需要这样做:

@Override
protected Void doInBackground(String... arg0)
{
    readRSS(arg0[0]);
    // should be readRSS(url) here
    return null;
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签