1

我是Android新手,如果我做错了事情,我深表歉意。欢迎您提出建议。我尝试将 SOAP webserice 与我的 Android 连接,并将接收到的数据显示给 recyclerView。我有一个 editText 来键入用于向 SOAP 发送请求的密钥。发送请求和接收数据步骤运行良好,但是,当我尝试使用notifyDataSetChanged()向 recyclerView 显示数据时,直到我再次触摸 editText 才更新回收站视图。

这里我发送请求和接收数据,我也尝试通过 notifyDataSetChanged 更新适配器:

btnInvoke.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Message msg = Message.obtain(connectSOAP.getHandler(),new SOAPCONNECT());
                msg.what = SOAPCONNECT;
                msg.sendToTarget();
            }
        });

//here is the SOAP implementation
private class SOAPCONNECT implements Runnable{
        String str;
        private static final String SOAP_ACTION = "http://tempuri.org/DanhSachSanPham";
        private String METHOD_NAME = "DanhSachSanPham";
        private static final String NAMESPACE = "http://tempuri.org/";
        private static final String URL = "https://www.minhyeuphuong.com/TestSOAP.asmx?WSDL";
        @Override
        public void run() {
            SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
            request.addProperty("maGoiHang",edtMaGoiHang.getText().toString());
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE ht = new HttpTransportSE(URL);
            try{
                ht.call(SOAP_ACTION,envelope);
                str = envelope.bodyIn.toString();
                SoapObject result = (SoapObject) envelope.bodyIn;
                if(str!=null){
                    SoapObject Sanpham = (SoapObject) result.getProperty(0);
                    int tongSP = Sanpham.getPropertyCount();
                    for (int i = 0;i<tongSP;i++){
                        SoapObject Sanpham1 = (SoapObject) Sanpham.getProperty(i);
                        arrSanPham.add(new 
                        SanPham(Integer.parseInt(Sanpham1.getProperty("MaSanPham").toString()),
                                Integer.parseInt(Sanpham1.getProperty("SoLuong").toString()),
                                Sanpham1.getProperty("TenSanPham").toString()));
                    }

                }else
                    Log.d("WebRespone", "ERROR");
                listSPAdapter.notifyDataSetChanged();
            }catch(Exception e){
                Log.d("Error","Error while recieving data");
            }
        }
    }

这是我的适配器初始化和适配器类。

//adapter initialization
        arrSanPham          = new ArrayList<SanPham>();
        listSPAdapter       = new ListSPAdapter(arrSanPham);
        layoutManager = new LinearLayoutManager(this);
        rcvSanPham.setLayoutManager(layoutManager);
        rcvSanPham.setAdapter(listSPAdapter);
//adapter class
public class ListSPAdapter extends RecyclerView.Adapter {
   /*Khoi tao adapter de control thanh phan cua recycle view*/
    private ArrayList<SanPham> arrSanPham;

    public ListSPAdapter(ArrayList<SanPham> arrSanPham) {
        this.arrSanPham = arrSanPham;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listsanpham,parent,false);
        return new ListSanPhamHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ListSanPhamHolder viewHolder = (ListSanPhamHolder) holder;
        viewHolder.txtTenSP.setText(arrSanPham.get(position).getTenSanPham());
        viewHolder.txtMaSP.setText(""+arrSanPham.get(position).getMaSanPham());
        viewHolder.txtSoLuong.setText(""+arrSanPham.get(position).getSoLuong());
    }

    @Override
    public int getItemCount() {
        return arrSanPham.size();
    }

    /*Holder để hold cac thanh phan cua layout*/
    public static class ListSanPhamHolder extends RecyclerView.ViewHolder{
        TextView txtMaSP,txtTenSP,txtSoLuong;
        public ListSanPhamHolder(@NonNull View itemView) {
            super(itemView);
            txtMaSP         = itemView.findViewById(R.id.txtMaSP_listsanpham);
            txtTenSP        = itemView.findViewById(R.id.txtTenSP_listsanpham);
            txtSoLuong      = itemView.findViewById(R.id.txtSoLuong_listsanpham);
        }
    }
}

4

0 回答 0