0

onPostExecute()在我的应用程序中从 Web 服务获取值时,我无法调用Android。我在下面添加了代码。返回后线程退出ArrayList。你能检查我的代码中是否有任何问题吗?

public class searchuser extends AsyncTask<Void, Void,ArrayList<DeleteuserDetails>>{

        protected ArrayList<DeleteuserDetails> doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            path.setNamespace();
            path.setMethod_name("searchuser");
            path.setSoap_action();
            path.setUrl();
            selecteduserType=String.valueOf(utype.getSelectedItem());
            userid=edtTxtUserId.getText().toString();
            if(!String.valueOf(utype.getSelectedItem()).equals("select")){

            Webservice service=new Webservice(path.getNamespace(), path.getMethod_name(), path.getUrl(), path.getSoap_action());
            try{
            SoapObject searchUser=service.getRequest();
             PropertyInfo search_userid=new PropertyInfo();
             search_userid.setName("userID");
             search_userid.setValue(userid);
             searchUser.addProperty(search_userid);
            /* PropertyInfo search_utype=new PropertyInfo(); 
             search_utype.setName("utype");
             search_utype.setValue(selecteduserType);
             searchUser.addProperty(search_utype);*/
             SoapSerializationEnvelope envelope = service.getEnvelope(searchUser);
                SoapObject response = service.getResponse(envelope);
                int count = response.getPropertyCount();
                for (int i = 0; i < count; i++) {

                    SoapObject result = (SoapObject) response.getProperty(i);
                    DeleteuserDetails deleteuserDetails=new DeleteuserDetails();
                    if(result.getPropertyAsString(4)!=null){
                    deleteuserDetails.setUserId(result.getPropertyAsString(4));
                    }
                    else{

                    }
                    if(result.getPropertyAsString(2)!=null){
                    deleteuserDetails.setUserName(result.getPropertyAsString(2));
                    }
                    else{

                    }
                    if(result.getPropertyAsString(3)!=null){
                        deleteuserDetails.setUserRole(result.getPropertyAsString(3));   
                    }
                    if(result.getPropertyAsString(1)!=null){
                        deleteuserDetails.setCreatedDate(result.getPropertyAsString(1));    
                    }
                    else{

                    }
                    try {
                        deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
                    }
                    catch(NullPointerException npe) {
                        deleteuserDetails.setCreatedBy("null");
                    }
                    userdetail.add(deleteuserDetails);
                }

        }
            catch(Exception e){
                e.printStackTrace();
            }
            }
            else{
                alertbox.generateAlert(Deleteuser.this, "Please select usertype", "Default value Selected", R.drawable.noicon);

            }
            return userdetail;
        }
        protected void onPostExecute( ArrayList<DeleteuserDetails>deleteuserDetails) {
            deleteuserList = new TextView[deleteuserDetails.size()];

            for(int i=0;i<deleteuserDetails.size();i++){

                    TableRow deleteUserRow=new TableRow(Deleteuser.this);
                    deleteUserRow.setId(i);
                    deleteUserRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                    selectcheck = new CheckBox(Deleteuser.this);
                    selectcheck.setId(i);
                    selectcheck.setTag(i);

                    deleteUserRow.addView(selectcheck);

                    deleteuserList[i]=new TextView(Deleteuser.this);
                //  deleteuserList[i].setId(i);
                    deleteuserList[i].setTag(i);
                    deleteuserList[i].setText(deleteuserDetails.get(i).getUserId());
                    deleteUserRow.addView(deleteuserList[i]);

                    uName=new TextView(Deleteuser.this);
                    uName.setId(i);
                    uName.setText(deleteuserDetails.get(i).getUserName());
                    deleteUserRow.addView(uName);

                    Userrole=new TextView(Deleteuser.this);
                    Userrole.setId(i);
                    Userrole.setPadding(20, 0, 0, 0);
                    Userrole.setText(deleteuserDetails.get(i).getUserRole());
                    deleteUserRow.addView(Userrole);

                    CreatedDate=new TextView(Deleteuser.this);
                    CreatedDate.setId(i);
                    CreatedDate.setPadding(20, 0, 0, 0);
                    CreatedDate.setText(deleteuserDetails.get(i).getCreatedDate());
                    deleteUserRow.addView(CreatedDate);

                    CreatedBy=new TextView(Deleteuser.this);
                    CreatedBy.setId(i);
                    CreatedBy.setPadding(20, 0, 0, 0);
                    CreatedBy.setText(deleteuserDetails.get(i).getCreatedBy());
                    deleteUserRow.addView(CreatedBy);
                    userdetailTable.addView(deleteUserRow,
                            new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));



                    selectcheck.setOnClickListener(listener);
                    Log.i("Exception",""+i);
                }
            TableRow addcomponentrow=new TableRow(Deleteuser.this);
            addcomponentrow.setId(200);
            addcomponentrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            Button Deletecomponentbtn=new Button(Deleteuser.this);
            Deletecomponentbtn.setText("Delete");
            Deletecomponentbtn.setId(200);
            Deletecomponentbtn.setPadding(10, 0, 20, 2);
            Deletecomponentbtn.setBackgroundColor(color.red);
            addcomponentrow.addView(Deletecomponentbtn);

            userdetailTable.addView(addcomponentrow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            Deletecomponentbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // TODO Auto-generated method stub
                    new DeleteUserid().execute(checkedValue);
                }
            });


        }
        final OnClickListener listener=new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                int index = (Integer) v.getTag();
                checkedValue=(String) deleteuserList[index].getText();

            }
        };

    }
4

2 回答 2

2

改变

protected void onPostExecute( ArrayList<DeleteuserDetails>deleteuserDetails) {
                                                     // ^^^

@Override
protected void onPostExecute( ArrayList<DeleteuserDetails> deleteuserDetails) {
 // your code here...
于 2012-12-26T08:23:01.267 回答
0

初始化DeleteuserDetails如下:

  DeleteuserDetails deleteuserDetails = new DeleteuserDetails();

声明字符串变量并赋值

  String userId = "";
  String userName = "";
  String userRole = "";
  String createdDate = "";
  String createdBy = "";

在模型中,生成构造函数如下:

  DeleteuserDetails(String userId, String userName, String userRole, String createdDate, String createdBy) {
   this.userId = userId;
   this.userName = userName;
   this.userRole = userRole;
   this.createdDate = createdDate;
   this.createdBy = createdBy;
 }

现在,您在doInBackground(..)

   if (result.getPropertyAsString(4) != null) 
             userId = result.getPropertyAsString(4);
   if (result.getPropertyAsString(2) != null) 
             userName = result.getPropertyAsString(2);
   if (result.getPropertyAsString(3) != null) 
             userRole = result.getPropertyAsString(3);
   if (result.getPropertyAsString(1) != null) 
             createdDate = result.getPropertyAsString(1);
    if (result.getPropertyAsString(0) != null) 
        createdBy = result.getPropertyAsString(0);

   deleteuserDetails = new  DeleteuserDetails(userId, userName, userRole, createdDate,  createdBy);

     userdetail.add(deleteuserDetails);

现在runOnUiThread()在 onPostExecute() 方法中添加方法并按照以下步骤操作:

        runOnUiThread(new Runnable() {

            @Override
            public void run() {


            deleteuserList = new TextView[deleteuserDetails.size()];

            for(int i=0;i<deleteuserDetails.size();i++){

                    TableRow deleteUserRow=new TableRow(Deleteuser.this);
                    deleteUserRow.setId(i);
                    deleteUserRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                    selectcheck = new CheckBox(Deleteuser.this);
                    selectcheck.setId(i);
                    selectcheck.setTag(i);

                    deleteUserRow.addView(selectcheck);

                    deleteuserList[i]=new TextView(Deleteuser.this);
                //  deleteuserList[i].setId(i);
                    deleteuserList[i].setTag(i);
                    deleteuserList[i].setText(deleteuserDetails.get(i).getUserId());
                    deleteUserRow.addView(deleteuserList[i]);

                    uName=new TextView(Deleteuser.this);
                    uName.setId(i);
                    uName.setText(deleteuserDetails.get(i).getUserName());
                    deleteUserRow.addView(uName);

                    Userrole=new TextView(Deleteuser.this);
                    Userrole.setId(i);
                    Userrole.setPadding(20, 0, 0, 0);
                    Userrole.setText(deleteuserDetails.get(i).getUserRole());
                    deleteUserRow.addView(Userrole);

                    CreatedDate=new TextView(Deleteuser.this);
                    CreatedDate.setId(i);
                    CreatedDate.setPadding(20, 0, 0, 0);
                    CreatedDate.setText(deleteuserDetails.get(i).getCreatedDate());
                    deleteUserRow.addView(CreatedDate);

                    CreatedBy=new TextView(Deleteuser.this);
                    CreatedBy.setId(i);
                    CreatedBy.setPadding(20, 0, 0, 0);
                    CreatedBy.setText(deleteuserDetails.get(i).getCreatedBy());
                    deleteUserRow.addView(CreatedBy);
                    userdetailTable.addView(deleteUserRow,
                            new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
                                    LayoutParams.WRAP_CONTENT));



                    selectcheck.setOnClickListener(listener);
                    Log.i("Exception",""+i);
                }
            TableRow addcomponentrow=new TableRow(Deleteuser.this);
            addcomponentrow.setId(200);
            addcomponentrow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

            Button Deletecomponentbtn=new Button(Deleteuser.this);
            Deletecomponentbtn.setText("Delete");
            Deletecomponentbtn.setId(200);
            Deletecomponentbtn.setPadding(10, 0, 20, 2);
            Deletecomponentbtn.setBackgroundColor(color.red);
            addcomponentrow.addView(Deletecomponentbtn);

            userdetailTable.addView(addcomponentrow,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            Deletecomponentbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // TODO Auto-generated method stub
                    new DeleteUserid().execute(checkedValue);
                }
            });


        }
        final OnClickListener listener=new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                int index = (Integer) v.getTag();
                checkedValue=(String) deleteuserList[index].getText();

            }
        };

            }
        });
    }
于 2012-12-26T08:23:01.473 回答