如何在android

时间:2016-01-22 06:23:33

标签: android android-layout http android-studio android-asynctask

我正在创建一个列表视图,其中包含文本和按钮,列表视图数据是从异步任务填充的,在这里我将在对话框中显示该列表视图,如enter image description here。我的问题是当用户选择列表视图中存在的“微调器”值时,在对话框弹出时,我在微调器的onItemclick内写的异步任务正在连续显示,如下所示

enter image description here。请帮我解决此问题并抱歉提出这样的问题。这是我执行点击取消按钮的适配器代码

leaves_type.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

        //  leaves_type_string = leaves_type.getSelectedItem().toString();
            new update_leave_entry_breakup_values().execute("UpdateValues",
                    "1082", id_string, leaves_type.getSelectedItem().toString(), username, "",
                    "", "", "", "", "", "");

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

这是我的异步任务:

  class update_leave_entry_breakup_values extends
            AsyncTask<String, integer, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

            progressdialog_view.setMessage("Updating leaves...");
            progressdialog_view.show();

        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            String TempMethod = params[0];
            String Flag = params[1];
            String value1 = params[2];
            String value2 = params[3];
            String value3 = params[4];
            String value4 = params[5];
            String value5 = params[6];
            String value6 = params[7];
            String value7 = params[8];
            String value8 = params[9];
            String value9 = params[10];
            String value10 = params[11];

            try {
                SoapObject request = new SoapObject(NAME_SPACE, TempMethod);
                request.addProperty("Flag", Flag);
                request.addProperty("value1", value1);
                request.addProperty("value2", value2);
                request.addProperty("value3", value3);
                request.addProperty("value4", value4);
                request.addProperty("value5", value5);
                request.addProperty("value6", value6);
                request.addProperty("value7", value7);
                request.addProperty("value8", value8);
                request.addProperty("value9", value9);
                request.addProperty("value10", value10);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                HttpTransportSE Android_HttpTransport = new HttpTransportSE(URL);
                Android_HttpTransport.debug = true;
                Android_HttpTransport.call(NAME_SPACE + TempMethod, envelope);
                String responseXml = envelope.getResponse().toString();
                return responseXml;
            } catch (Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
             super.onPostExecute(result);

            progressdialog_view.dismiss();

            if (result == null) {
                Toast.makeText(context, "Error while reading data",
                        Toast.LENGTH_SHORT).show();
            } else if (result.equals("0")) {
                Toast.makeText(context, "Error while Updating data",
                        Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "Update Success",
                        Toast.LENGTH_SHORT).show();

            }
        }
    }

1 个答案:

答案 0 :(得分:2)

从点击事件

中删除notifyDataSetChanged();
final Button cancel = (Button) row.findViewById(R.id.btn_cancel);
        cancel.setTag(position);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                int tag_position = (Integer) v.getTag();
                objects.remove(tag_position);
                notifyDataSetChanged(); // remove this
                new cancel_leave_entry_breakup_values().execute("UpdateValues", "1083",
                        leave_app.getID(), leave_app.getUsername().toString(),
                        "", "", "", "", "", "", "", "");

            }
        });

根据我的说法,你打电话给你的适配器两次。你需要打电话一次。

如果要刷新listview的数据,请使用notifyDataSetChanged();,如OnRestart()方法。