Android notifyDataSetChanged在按edittext之前不工作

Android notifyDataSetChanged在按edittext之前不工作,android,android-recyclerview,recyclerlistview,Android,Android Recyclerview,Recyclerlistview,我是Android新手,如果我做错了事情,我道歉。欢迎你的建议。 我尝试将SOAP-webserice与我的Android连接起来,并将接收到的数据显示给recyclerView。我有一个editText,用于键入向SOAP发送请求的键。发送请求和接收数据步骤工作正常,但是,当我尝试使用notifyDataSetChanged()向recyclerView显示数据时,直到再次触摸editText,recycler视图才更新 在这里,我发送请求并接收数据,我还尝试通过notifyDataSetCh

我是Android新手,如果我做错了事情,我道歉。欢迎你的建议。 我尝试将SOAP-webserice与我的Android连接起来,并将接收到的数据显示给recyclerView。我有一个editText,用于键入向SOAP发送请求的键。发送请求和接收数据步骤工作正常,但是,当我尝试使用notifyDataSetChanged()向recyclerView显示数据时,直到再次触摸editText,recycler视图才更新

在这里,我发送请求并接收数据,我还尝试通过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");
            }
        }
    }

btnInvoke.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Message msg=Message.get(connectSOAP.getHandler(),new SOAPCONNECT());
msg.what=SOAPCONNECT;
msg.sendToTarget();
}
});
//下面是SOAP实现
私有类SOAPCONNECT实现可运行{
字符串str;
私有静态最终字符串SOAP_ACTION=”http://tempuri.org/DanhSachSanPham";
私有字符串方法\u NAME=“DanhSachSanPham”;
私有静态最终字符串命名空间=”http://tempuri.org/";
私有静态最终字符串URL=”https://www.minhyeuphuong.com/TestSOAP.asmx?WSDL";
@凌驾
公开募捐{
SoapObject请求=新的SoapObject(名称空间、方法名称);
addProperty(“maGoiHang”,edtMaGoiHang.getText().toString());
SoapSerializationEnvelope=新的SoapSerializationEnvelope(
第11版);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
HttpTransportSE ht=新的HttpTransportSE(URL);
试一试{
ht.呼叫(SOAP_动作、信封);
str=envelope.bodyIn.toString();
SoapObject结果=(SoapObject)envelope.bodyIn;
如果(str!=null){
SoapObject Sanpham=(SoapObject)result.getProperty(0);
int tongSP=Sanpham.getPropertyCount();

对于(int i=0;i请发布适配器初始化和适配器类。我添加了适配器类。请发布适配器初始化和适配器类。我添加了适配器类
//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);
        }
    }
}