本文介绍了如何传递值的AsyncTask的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用AsyncTask的,可能有点困惑,以及大约传递值。

This is the first time i am using AsyncTask and probably a little confused as well, about passing the values.

我有4个的ArrayList<串GT; s,它包含应该插入到数据库中的数据。我想在后台做这个插入。将有至少50行由4的ArrayList,我有考虑过值插入到数据库和1行插入的时间,

I have 4 ArrayList<String> s, which contain data that should be inserted into the database. I want this insertion to be done in background. There will be a minimum of 50 rows to be inserted into the database and 1 row is inserted at a time, by taking passing values from 4 arraylists that i have.

有人能指导我如何做到这一点?

Can someone guide me on how to do this?

现在我创建一个扩展的AsyncTask :::一个子类InsertData

Now i create a subclass InsertData that extends AsyncTask :::

private class InsertData extends AsyncTask<Params, Progress, Result>{

        @Override
        protected Result doInBackground(Params... params) {
            // TODO Auto-generated method stub
            return null;
        }
}

我有4的ArrayList,其中我需要检索每个ArrayList中1项,通过4个值到数据库中值。我如何通过这4个值的AsyncTask的,也是,我怎么重复此,直到有在的ArrayList项。

I have values in 4 arraylists, in which i need to retrieve 1 entry from each arraylist and pass the 4 values to the database. How do i pass these 4 values to the AsyncTask and also, how do i repeat this till there are entries in the arraylists.

下面是插入1行插入数据库:::

Here is the database method that inserts 1 row into the database :::

public void insert_row(int count, String image_url, String name, String number) {
        // TODO Auto-generated method stub

        db.execSQL("INSERT INTO contacts VALUES('"+ count +"', '"+ image_url +"', '"+ name +"', '"+ number +"')");
    }

如果有人可以给我如何,我可以实现这样的概述,这将是巨大的。

If someone can give me a overview on how i can implement this, It would be great.

推荐答案

请尝试以下方法,而且,不管怎么说,你可以为InsertData在InserData类领域建立构造您所需要​​的参数,EM存储和doInBackground使用()。

Try the following, and, anyway, You could create constructor for InsertData with parameters you need, store em in InserData class fields and use in doInBackground().

private class InsertData extends AsyncTask<ArrayList, Progress, Result>{

            @Override
            protected Result doInBackground(ArrayList... params) {
                // TODO Auto-generated method stub
                ArrayList list1 = params[0];
                ArrayList list2 = params[1];
                ...
                return null;
            }
    }

电话:

InsertData task= new InsertData();
task.execute(yourList1, yourList2, yourList3...);

这篇关于如何传递值的AsyncTask的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:49