0

我有一个gridview。如果单击该网格中的一个项目,它将调用一个名为get_details. 我的问题是,第一次单击网格中的任何项目时,它会跳过,new get_details().execute();但在我下一次单击后它已经开始工作了..你能帮帮我吗..

这是我的代码

gridView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {


            final globalVars globalVars = (globalVars) getApplicationContext();


            String sel_id = ca_list.get(arg2).stud_id;
            globalVars.setClickedGrid(sel_id);
            Log.i("SELECT", sel_id);

            new get_details().execute();

            String p_id = globalVars.getPopupID().toString();
            String p_name = globalVars.getPopupName().toString();
            String p_absent = globalVars.getPopupAbsent().toString();
            String p_late = globalVars.getPopupLate().toString();
            Log.i("SHOW CALL" , p_name);

            int y = arg1.getTop();
            int x = arg1.getLeft();

                 showPopup(ViewAttendanceSP.this, x, y, p_id, p_name, p_absent, p_late);

        }
       });

get_details 类

private class  get_details extends AsyncTask<String, Integer, String> 
{
    @Override
    protected String doInBackground(String... arg0) 
    {
        InputStream is = null;
        String result = "";


        String bidSt = dispid.getText().toString();
        globalVars globalVars = (globalVars)getApplicationContext();
        String idSt = globalVars.getClickedGrid().toString();

        try 
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.59.1/sp/viewsumsp.php");

            List<NameValuePair> parameter = new ArrayList<NameValuePair>();
            parameter.add(new BasicNameValuePair("student_id", idSt));
            parameter.add(new BasicNameValuePair("beadle_id", bidSt));


            httppost.setEntity(new UrlEncodedFormEntity(parameter));

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) 
        {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        try 
        {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) 
            {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) 
        {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) 
    {
        super.onPostExecute(result);

        ArrayList<HashMap<String, String>> to_take = new ArrayList<HashMap<String, String>>();

        try 
        {
            JSONObject jArray = new JSONObject(result);

            JSONArray stud_info = jArray.getJSONArray("stud_info");
            ca_list1 = new ArrayList<ViewAttendanceSummaryObject>();

            for (int i = 0; i < stud_info.length(); i++) 
            {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject e = stud_info.getJSONObject(i);

                map.put("id", String.valueOf(i));
                map.put("stud_id", e.getString("student_id"));
                map.put("f_name", e.getString("first_name"));
                map.put("l_name", e.getString("last_name"));
                map.put("absence", e.getString("absence"));
                map.put("lateness", e.getString("lateness"));

                to_take.add(map);
                Log.i("INNER" , e.getString("first_name"));

                final globalVars globalVars = (globalVars) getApplicationContext();

                String id = e.getString("student_id");
                String name = e.getString("last_name") + ", " + e.getString("first_name");
                String abs = e.getString("absence");
                String lat = e.getString("lateness");

                globalVars.setPopupID(id);
                globalVars.setPopupName(name);
                globalVars.setPopupAbsent(abs);
                globalVars.setPopupLate(lat);

                ca_list1.add(new ViewAttendanceSummaryObject(e.getString("student_id"), e.getString("first_name"), e.getString("last_name"), e.getString("absence"), e.getString("lateness")));
            }



        } catch (JSONException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

ps 我刚刚发现,它不会跳过 new get_details().execute(); 而是在 new get_details().execute(); 之后停止 被执行..我该如何解决这个问题?

4

2 回答 2

0

用于onPreExecute访问内部AsyncTask的 UI 元素:

private class  get_details extends AsyncTask<String, Integer, String> 
{
     String bidSt="";
       @Override
    protected void onPreExecute() {
        super.onPreExecute();

        //access all Ui elements here
        bidSt = dispid.getText().toString();

    }
    @Override
    protected String doInBackground(String... arg0) 
    {
        InputStream is = null;
        String result = "";
         //your code here

因为在 AsyncTask 中无法从 doInBackground 访问 Ui 元素。

于 2012-12-04T16:50:35.657 回答
0

如果我正确理解您的问题,您将看到AsyncTask. 它将在后台运行,然后将其结果发布到主 UI 线程,因此它可能不会显示为立即运行。

根据AsyncTask文档,它应该是一个相当短的操作,最多几秒钟。您的手术需要多长时间?如果您的后台任务应该处理您的每次点击GridView,那么它应该足够短,通常可以在用户再次点击之前完成。否则,您可能会创建一个后台任务队列,这些任务可能需要很长时间才能将结果发布给用户。

于 2012-12-04T16:56:15.360 回答