如何在android中使用线程处理两个任务?

时间:2015-07-23 07:09:48

标签: android multithreading android-asynctask async-await

每当我尝试在列表视图中显示我的服务器中的图像列表时,我就会卡在模拟器中,点击按钮以排序图像。当我尝试点击时,我会逐一列出图像在排序按钮,那时我的模拟器卡住了一段时间。 我想同时处理这个问题。 我在点击功能上有两个按钮,一个用于执行后台进程的Asynctask,另一个用于普通按钮点击监听器,用于对输入组件进行排序以便从UI组件中读取。

 WebServiceTask wst = new WebServiceTask(WebServiceTask.GET_TASK, this, "Loading.....");
            wst.execute(new String[]{sampleURL});
 try {


//Extract the data…

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
            nameValuePairs.add(new BasicNameValuePair("city", city_name));
            nameValuePairs.add(new BasicNameValuePair("room_type", room_type));

            nameValuePairs.add(new BasicNameValuePair("bed", no_bed));

            nameValuePairs.add(new BasicNameValuePair("bedrooms", no_bedrooms));

            nameValuePairs.add(new BasicNameValuePair("guest_allow", guest_allowed));


            // Add your data

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);


            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                json = EntityUtils.toString(entity);

                try {


//                    JSONArray jso = new JSONArray(json);
                    JSONObject object = new JSONObject(json);
                    JSONArray jso = object.getJSONArray("search_result");
                    Toast.makeText(this, "jso length" + jso.length(), Toast.LENGTH_LONG).show();
                    if (jso.length() != 0) {

                        for (int i = 0; i < jso.length(); i++) {
                            if (jso.getJSONObject(i).getString("city").equalsIgnoreCase(city_name)) {
                                Person resultRow = new Person();
                                resultRow.id = jso.getJSONObject(i).getString("user_id");
                                resultRow.ProjectName = jso.getJSONObject(i).getString("user_name");
                                resultRow.Country = jso.getJSONObject(i).getString("bathrooms");

                                resultRow.city = jso.getJSONObject(i).getString("city");
                                resultRow.descrip = jso.getJSONObject(i).getString("price");
                                resultRow.bed = jso.getJSONObject(i).getString("bed");
                                resultRow.bedrooms = jso.getJSONObject(i).getString("bedrooms");
                                resultRow.guest_allow = jso.getJSONObject(i).getString("guest_allow");
                                resultRow.roomtype = jso.getJSONObject(i).getString("room_type");
                                resultRow.property_type = jso.getJSONObject(i).getString("property_type");

                                resultRow.logo = jso.getJSONObject(i).getString("property_image");

                                resultRow.user_ppty_id = jso.getJSONObject(i).getString("user_property_id");
                                resultRow.check_in_time = jso.getJSONObject(i).getString("check_in_time");
                                resultRow.check_out_time = jso.getJSONObject(i).getString("check_out_time");
                                resultRow.host_image = jso.getJSONObject(i).getString("host_image");


                                arrayOfwebData.add(resultRow);
                                Toast.makeText(this, "counting" + i, Toast.LENGTH_LONG).show();

                            }

                        }

                    } else {
                        Toast.makeText(this, "NOT found", Toast.LENGTH_LONG).show();
                    }


                } catch (Exception e) {
                    Toast.makeText(this, "NOT found", Toast.LENGTH_LONG).show();
                    Log.e(TAG, e.getLocalizedMessage(), e);
                }
                ListView myListView = (ListView) findViewById(R.id.listview);

                Toast.makeText(this, "updated" + up, Toast.LENGTH_LONG).show();

                aa = new FancyAdapter();
                myListView.setAdapter(aa);
                up++;

            } else {
                Toast.makeText(this, "NOT found", Toast.LENGTH_LONG).show();
            }


            //   ViewHolder aa1= new ViewHolder();

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

并点击按钮

 btn_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String val = (String) no_guest.getText();

                int values = Integer.parseInt(val);
                no_guest.setText("" + (values + 1));
                guest_allowed = (String) no_guest.getText();
            }
        });

0 个答案:

没有答案