静态类中的异步任务

时间:2012-02-26 13:42:44

标签: android class static android-asynctask android-fragments

我在我的应用程序中使用了actionbar sherlock / fragments

我的问题是我需要调用异步任务来在片段中运行,这是一个静态类

编译器抱怨“没有封闭的KeywordSearch类型的实例可以访问”

我想过将异步类移出并创建它自己的类,但在我的KeywordSearch类中声明的异常任务中使用的变量太多了

如何在不崩溃的情况下执行异步任务?

我的列表视图类(片段):

public static class ListFragment extends Fragment {
    private ListView list_list;
    private LazyAdapter adp_list;

    private KeywordSearch.SearchTask run;
    //run.execute();

    //new SearchTask().execute();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
    }
    @Override   
    public void onDestroy()
    {
        if(!RTN_REQ.equals("SSDR") && data_got){
        adp_list.imageLoader.stopThread();
            }
        list_list.setAdapter(null);
        if(data_got)
        ImageLoader.clearCache(true,true);       
        super.onDestroy();      
    }
    @Override
    public void onPause()
    {
        /*
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            list_list.setNumColumns(2);
        }else if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            list_list.setNumColumns(3);
        }
        */
        super.onPause();
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.list, container, false);
        adp_list = new LazyAdapter(getActivity(),list_data, 0);
        run = new SearchTask();
        list_list = (ListView) v.findViewById (R.id.list);
        if(list_data != null){

            list_list.setAdapter(adp_list);
            list_list.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {               
                    String[] FINAL = list_data[arg2].split("##");
                    final Intent d = new Intent();
                    d.putExtra("DATA",FINAL[0]);
                    d.setClass(getActivity().getApplicationContext(), Details.class);   
                    startActivity(d);
                    return false;
                    }
            });           
            list_list.setOnItemClickListener(new OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3)
                {
                    if(!eno_act){
                        String[] FINAL = list_data[arg2].split("##");
                        final Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(FINAL[1]));                 
                        startActivity(i);
                    }else{
                        String[] FINAL = list_data[arg2].split("##");
                        level = 1;
                        level_filter = FINAL[2];
                        run.execute();  //this is where it crashes!!




                    }

                }
            });     
        }
        return v;
    }
}

和我的SearchTask类:

 class SearchTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        //....to long to show..
    }

    @Override
    protected String doInBackground(Void... unsued) {
        //....to long to show..
    }

    @Override
    protected void onProgressUpdate(Void... unsued) {
        //....to long to show..
    }

    @Override
    protected void onPostExecute(String sResponse) {
        //....to long to show..
    }
}

1 个答案:

答案 0 :(得分:3)

在异步任务类

的构造函数中传递KeyWordSearch类的那些变量

例如

int foo;   

public SearchTask(int foo)  //your variables here
{
    this.foo = foo;
}

用于传递数据示例

//在异步任务中,您可以在doInBackground或任何其他方法中执行此操作。   让foo是你的字符串数组

 Bundle b = new Bundle();
 Intent i = new Intent(context, YourActivity.class)
 b.putExtraStringArray("myStringArray", foo);
 i.putExtras(b);

//在您的活动中接收此数组

 String[] myArray =  this.getIntent().getStringArrayExtra("myStringArray"); //this should be done after executing asyncTask class.