Java 从自定义阵列适配器中刷新列表

Java 从自定义阵列适配器中刷新列表,java,android,Java,Android,我已经查看了讨论这个问题的大多数问题,但似乎找不到任何解决问题的方法。我有一个自定义的数组适配器将数据传递给列表,我需要在删除一个项目后刷新该列表,我知道notifyDataSetChanged()应该是我的解决方案,但还没有使它起作用 对不起,我的代码太乱了 但我还在学习:) 任何帮助都将不胜感激,thanx:) 我的适配器: public class CustomBasketAdapter extends ArrayAdapter<String> { private Stri

我已经查看了讨论这个问题的大多数问题,但似乎找不到任何解决问题的方法。我有一个自定义的数组适配器将数据传递给列表,我需要在删除一个项目后刷新该列表,我知道notifyDataSetChanged()应该是我的解决方案,但还没有使它起作用 对不起,我的代码太乱了 但我还在学习:) 任何帮助都将不胜感激,thanx:)

我的适配器:

public class CustomBasketAdapter extends ArrayAdapter<String> 
{

private String TAG ="Vik";
public String stringEmail= "";
public String stringStore= "";
private String product = "";
private String quantity = "";
private String store = "";

//String[] stringEmail={"stefan.grobler@gmail.com"};

 private final Activity context;
 private final String[] storename;
 private final String[] itemprice;
 private final String[] itemname;
 private final String[] productquantity;
 private final String[] basketlinetotal;



 //public CustomListAdapter(Activity context, String[] itemname,String[]     itemprice, Integer[] imgid) {
 public CustomBasketAdapter(Activity context, String[] storename,String[] itemprice, String[] productquantity, String[] itemname, String[] basketlinetotal ) {
 super(context, R.layout.viewbasket_layout, storename);




 // TODO Auto-generated constructor stub
 this.context=context;
 this.storename=storename;
 this.itemprice=itemprice;
 this.itemname=itemname;
 this.productquantity=productquantity;
 this.basketlinetotal=basketlinetotal;

 Intent intent = ((Activity) context).getIntent();
    if (null != intent)
{
    stringEmail = intent.getStringExtra("EmailAddress");
    stringStore = intent.getStringExtra("StoreDescription");
}

Toast toast = Toast.makeText(getContext(),stringEmail, Toast.LENGTH_LONG);
              toast.setGravity(Gravity.CENTER, 0, 10);
              //toast.show();

 }

 public View getView(int position,View view,ViewGroup parent) {
 LayoutInflater inflater=context.getLayoutInflater();
 View rowView=inflater.inflate(R.layout.viewbasket_layout, null,true);

 final TextView txtstore = (TextView) rowView.findViewById(R.id.txtstorename);
 final TextView txtproduct = (TextView) rowView.findViewById(R.id.txtproduct);
 final TextView txtquantity = (TextView) rowView.findViewById(R.id.txtquantity);
 TextView txtprice = (TextView) rowView.findViewById(R.id.txtprice);
 TextView txttotal = (TextView) rowView.findViewById(R.id.txttotal);
 Button btnclear = (Button) rowView.findViewById(R.id.btnclearitem);

 txtstore.setText(storename[position]);
 txtproduct.setText(itemname[position]);
 txtquantity.setText(productquantity[position]);
 txtprice.setText(itemprice[position]);
 txttotal.setText(basketlinetotal[position]);


 btnclear.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {

        product = txtproduct.getText().toString();
        quantity = txtquantity.getText().toString();
        store = txtstore.getText().toString();

        DeleteFromBasket();

        Toast toast = Toast.makeText(getContext(),"Item Cleared", Toast.LENGTH_LONG);
          toast.setGravity(Gravity.CENTER, 0, 10);
          toast.show();

          notifyDataSetChanged();



    }
     /*your onclick code*/
 });



 return rowView;




 };



public void ChannelToService()
{

 String SOAP_ACTION = "http://tempuri.org/ITHubServ/DeleteFromBasket";
 String METHOD_NAME = "DeleteFromBasket";
 String NAMESPACE = "http://tempuri.org/";
 String URL = "http://www.buh34nart.co.za/THubServ.svc?wsdl";
 String OrderLine = "";

 try{

     SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

    String basketline = stringEmail + "|" + store + "|" + product.replace("|", "") + "|" + quantity;

     //OrderLine = OrderLine.substring(0,OrderLine.length() -1);
     Request.addProperty("Line", basketline);
     //Request.addProperty("UserPassword", txtPass.getText().toString());

     SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
     soapEnvelope.dotNet = true;
     soapEnvelope.setOutputSoapObject(Request);

     HttpTransportSE transport= new HttpTransportSE(URL);

     Object Response = null;

        transport.call(SOAP_ACTION, soapEnvelope);

        Response = soapEnvelope.getResponse();

    //  data = Response.toString().split("[\\x7C]");

      //Log.i(TAG, "Result Order: " + Response);
        //if(Response.toString().equals("1"))
        //{
        //    runOnUiThread(new Runnable() {
        //             public void run() {

              //Toast toast = Toast.makeText(LoginPage.this,"Incorrect Username/Password" +
              //"              Login Failed", Toast.LENGTH_LONG);
              //toast.setGravity(Gravity.CENTER, 0, 10);
              //toast.show();
              //Not Registered or psw wrong or email wrong (forgot password)
         //            }
         //  });
        //}
        //else
        //{
        //  if(Response.toString().equals("9"))
        //  {
        //      startActivity(new Intent("com.ctc.android.widget.TechnicalError"));
        //      // Probleem met system Technical 
        //  }
        //  else
        //  {
                //btnloginclick();
        //  }

    //  }           
 }

 catch(Exception Ex) {}
}



  public void DeleteFromBasket()
 {
   AsyncCallWS task = new AsyncCallWS();
   task.execute();

 }



 private class AsyncCallWS extends AsyncTask<Void, Void, Void>  {


 ProgressDialog progDailog = new ProgressDialog(getContext());

 @Override
 protected Void doInBackground(Void... params) {
     Log.i(TAG, "doInBackground");
     ChannelToService();
     return null;
 }

 @Override
 protected void onPostExecute(Void result) {
     Log.i(TAG, "onPostExecute");
     CustomBasketAdapter.this.clear();
     CustomBasketAdapter.this.addAll();
     CustomBasketAdapter.this.notifyDataSetChanged();

     progDailog.dismiss();





 }


@Override
 protected void onPreExecute() {
     Log.i(TAG, "onPreExecute");
     super.onPreExecute();
     progDailog.setMessage("Loading...");
     progDailog.setIndeterminate(false);
     progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     progDailog.setCancelable(true);
     progDailog.show();
 }

 @Override
 protected void onProgressUpdate(Void... values) {
     Log.i(TAG, "onProgressUpdate");
     }

}


}
公共类CustomBasketAdapter扩展了ArrayAdapter
{
私有字符串TAG=“Vik”;
公共字符串stringEmail=“”;
公共字符串stringStore=“”;
私有字符串product=“”;
私有字符串数量=”;
私有字符串存储=”;
//String[]stringEmail={”stefan。grobler@gmail.com"};
私人最终活动背景;
私有最终字符串[]storename;
私有最终字符串[]itemprice;
私有最终字符串[]itemname;
私有最终字符串[]productquantity;
私有最终字符串[]basketlinetotal;
//公共CustomListAdapter(活动上下文,字符串[]itemname,字符串[]itemprice,整数[]imgid){
公共CustomBasketAdapter(活动上下文,字符串[]storename,字符串[]itemprice,字符串[]productquantity,字符串[]itemname,字符串[]basketlinetotal){
super(上下文、R.layout.viewbasket\u布局、店名);
//TODO自动生成的构造函数存根
this.context=context;
this.storename=storename;
this.itemprice=itemprice;
this.itemname=itemname;
这个.productquantity=productquantity;
this.basketlinetotal=basketlinetotal;
Intent=((活动)上下文).getIntent();
if(null!=intent)
{
stringEmail=intent.getStringExtra(“电子邮件地址”);
stringStore=intent.getStringExtra(“StoreDescription”);
}
Toast Toast=Toast.makeText(getContext(),stringEmail,Toast.LENGTH\u LONG);
toast.setGravity(Gravity.CENTER,0,10);
//toast.show();
}
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.viewbasket\u布局,null,true);
final TextView txtstore=(TextView)rowView.findViewById(R.id.txtstorename);
final TextView txtproduct=(TextView)rowView.findViewById(R.id.txtproduct);
最终TextView txtquantity=(TextView)rowView.findViewById(R.id.txtquantity);
TextView txtprice=(TextView)rowView.findViewById(R.id.txtprice);
TextView txttotal=(TextView)rowView.findViewById(R.id.txttotal);
按钮btnclear=(按钮)行视图.findViewById(R.id.btnclearitem);
setText(storename[位置]);
txtproduct.setText(项目名称[位置]);
txtquantity.setText(产品数量[位置]);
setText(itemprice[位置]);
setText(basketlinetotal[位置]);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
product=txtproduct.getText().toString();
数量=txtquantity.getText().toString();
store=txtstore.getText().toString();
DeleteFromBasket();
Toast Toast=Toast.makeText(getContext(),“项已清除”,Toast.LENGTH\u LONG);
toast.setGravity(Gravity.CENTER,0,10);
toast.show();
notifyDataSetChanged();
}
/*你的onclick代码*/
});
返回行视图;
};
公共无效频道服务()
{
字符串SOAP_ACTION=”http://tempuri.org/ITHubServ/DeleteFromBasket";
字符串方法\u NAME=“DeleteFromBasket”;
字符串命名空间=”http://tempuri.org/";
字符串URL=”http://www.buh34nart.co.za/THubServ.svc?wsdl";
字符串OrderLine=“”;
试一试{
SoapObject请求=新的SoapObject(名称空间、方法名称);
String basketline=stringEmail+“|”+商店+“|”+产品。替换(“|”)+“|”+数量;
//OrderLine=OrderLine.substring(0,OrderLine.length()-1);
请求。添加属性(“线”,篮线);
//Request.addProperty(“UserPassword”,txtPass.getText().toString());
SoapSerializationEnvelope soapEnvelope=新的SoapSerializationEnvelope(soapEnvelope.VER11);
soapEnvelope.dotNet=true;
setOutputSoapObject(请求);
HttpTransportSE传输=新的HttpTransportSE(URL);
对象响应=null;
调用(SOAP\u操作,soapEnvelope);
Response=soapEnvelope.getResponse();
//data=Response.toString().split(“[\\x7C]”);
//Log.i(标签,“结果顺序:”+响应);
//if(Response.toString()等于(“1”)
//{
//runOnUiThread(新的Runnable(){
//公开募捐{
//Toast Toast=Toast.makeText(LoginPage.this,“用户名/密码不正确”+
//“登录失败”,Toast.LENGTH\u LONG);
//toast.setGravity(Gravity.CENTER,0,10);
//toast.show();
//未注册或psw错误或电子邮件错误(忘记密码)
//            }
//  });
//}
//否则
//{
//if(Response.toString()等于(“9”))
//  {
//startActivity(新意图(“com.ctc.android.widget.TechnicalError”);
////Probleem met系统技术
//  }
//否则
//  {
//btnloginclick();
//  }
//  }           
}
捕获(例外情况除外){}
}
公共void DeleteFromBasket()
{
AsyncCallWS任务=新建AsyncCallWS();
task.execute();
}
私有类AsyncCallWS扩展了AsyncTask{
ProgressDialog progDailog=新建ProgressDialog(getContext());
@凌驾
受保护的Void doInBackground(Void…参数){
Log.i(标签“doInBackground”);
ChannelToService();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
Log.i(标记“onPostExecute”);
CustomBasketAdapter.this.clear();
CustomBasketAdapter.this.addAll();
CustomBasketAdapter.this.notifyDataSetChanged();
progDailog.disclose();
}
@凌驾
受保护的void onPreExecute(){
Log.i(标记“onPreExecute”);
super.onPreExecute();
progDailog.setMessage(
 CustomBasketAdapter.this.clear();
 CustomBasketAdapter.this.addAll();
 CustomBasketAdapter.this.notifyDataSetChanged();
 CustomBasketAdapter.this.remove(item);
 CustomBasketAdapter.this.notifyDataSetChanged();
            if(context instanceof ViewBasket){
          ((ViewBasket)context).GetBasketProducts();
        }