android – 如何在同一个ListView中使用来自不同Url的提要

在我当前的项目中,我可以从单个Url中获取数据,然后在我的自定义列表视图中显示它们.
那么有没有一种方法可以从多个Url获取数据然后放入相同的ListView,即使每个Url的数据被调用不同?
这是我的代码,允许我从URL获取项目:

    public  class CLASS1 extends Fragment {

    private RSSFeed myRssFeed = null;

    public CLASS1()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1, null);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        try {
            URL rssUrl = new URL("URL");
            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)
        {
            ListView list = (ListView)view.findViewById(android.R.id.list);
            CustomList adapter = new CustomList(getActivity(),myRssFeed.getList());
            adapter.addAll();
            list.setAdapter(adapter);

        }
        else
            Toast.makeText(getActivity(), "Spiacente, connessione non disponibile!" +
                            " Prova più tardi.",
                    Toast.LENGTH_LONG).show();
        return view;
    }
}

解决方法:

我正在我的应用程序中这样做,

这就是我这样做的原因,我创建了AsyncTask来解析RSS,并使用循环传递不同的URL来解析为AysncTask.

这就是我的意思,

这是一个URL数组,

private String[] newsURLs = {
            "url1", "url2", "url3" };

这就是我执行它们的方式,

//GetNews is AsyncTask class
for (int i = 0; i < newsURLs.length; i++) {
                GetNews blog = new GetNews(i);
                blog.execute();
            }

在上面的代码中,iis是作为AsyncTask类中的构造函数传入的URL号.

这是我在AsyncTask类中的构造函数,

int number;
public GetNews(int urlNumber) {
            number = urlNumber;
        }

并以这种方式在AsyncTask的doInBackground()方法中加载URL,

                URL feedURL = new URL(newsURLs[number]);
                HttpURLConnection connection;
                connection = (HttpURLConnection) feedURL.openConnection();
                connection.setConnectTimeout(8000);
                connection.connect();

之后,我使用SAXParser根据我的需要解析xml.

请注意,如果要并行执行,请替换blog.execute();

blog.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

// this type of executor uses the following params:
            //
            // private static final int CORE_POOL_SIZE = 5;
            // private static final int MAXIMUM_POOL_SIZE = 128;
            // private static final int KEEP_ALIVE = 1;
            //
            // private static final ThreadFactory sThreadFactory = new ThreadFactory() {
            //     private final AtomicInteger mCount = new AtomicInteger(1);
            //
            //     public Thread newThread(Runnable r) {
            //         return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
            //     }
            // };
            //
            // private static final BlockingQueue<Runnable> sPoolWorkQueue =
            //        new LinkedBlockingQueue<Runnable>(10);

另请注意,

When first introduced, AsyncTasks were executed serially on a single background thread. Starting
with DONUT, this was changed to a pool of threads allowing multiple
tasks to operate in parallel.
After HONEYCOMB, it is planned to change this back to a single thread
to avoid common application errors caused by parallel execution. If
you truly want parallel execution, you can use the
executeOnExecutor(Executor, Params…) version of this method with
THREAD_POOL_EXECUTOR; however, see commentary there for warnings on
its use.

DONUT是Android 1.6,HONEYCOMB是Android 3.0.

您也可以根据自己的版本执行任务,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    blog.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    blog.execute();
}

这是我的部分代码来展示我在做什么,

    // This is my Main class

private String[] newsURLs = {
            "url1", "url2", "url3" };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myapp);


            for (int i = 0; i < newsURLs.length; i++) {
                GetNews blog = new GetNews(i);
                blog.execute();
            }
        }
    }

这是AsyncTask类(部分代码),

private class GetNews extends
        AsyncTask<Object, Void, Void> {

    protected int number;

    public GetNews(int urlNumber) {
        number = urlNumber;
    }

    @Override
    protected void doInBackground(
            Object... arg0) {

        try {
            // Check if the feed is Live
            URL feedURL = new URL(newsURLs[number]);
            HttpURLConnection connection;
            connection = (HttpURLConnection) feedURL.openConnection();
            connection.setConnectTimeout(8000);
            connection.connect();
上一篇:session 、 tensor、feed


下一篇:PHP MySQL的活动源