Android在GridView.setOnItemClickListener上使用AsyncTask

时间:2016-07-09 05:09:52

标签: android gridview android-asynctask

我尝试在GridView.setOnItemClickListener上使用AsyncTask,如下所示:

gv_name = (CustomGridView)view.findViewById(R.id.gridViewName);

    gv_name .setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            new AsyncTaskName().execute();

        }
    });

这是AsynncTask:

class AsyncTaskName extends AsyncTask<Void, Void, BinderDataName> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected BinderDataName doInBackground(Void... params) {
        try {
            URL url = new URL(xml_file);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setDoInput(true);
            http.connect();
            InputStream in = http.getInputStream();
            XmlPullParserName parser_Name = new XmlPullParserName();
            nameList = parser_Name.parse(in, emp_name);
            BinderDataName bd_Name = new BinderDataName(getActivity(), nameHashmap);
            return bd_Name;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(BinderDataName bd_Name) {
        if (bd_Name != null) {
            gv_detail.setAdapter(bd_Name);
        }

    }

}

AsyncTask无法在GridView.setOnItemClickListener中执行 如果我在GridView.setOnItemClickListener之外使用它,我可以使用它 如何解决?

0 个答案:

没有答案