从Custom Array Adapter中刷新列表

时间:2015-02-06 09:23:58

标签: java android

我已经看过大多数讨论此问题的问题,但似乎无法找到解决我问题的任何问题。我有一个自定义数组适配器将数据传递到列表,我删除一个项目后我需要刷新列表,我知道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");
     }

}


}

2 个答案:

答案 0 :(得分:0)

我认为您的问题在于您没有删除字符串数组中的项目。你应该改变

 CustomBasketAdapter.this.clear();
 CustomBasketAdapter.this.addAll();
 CustomBasketAdapter.this.notifyDataSetChanged();

通过

 CustomBasketAdapter.this.remove(item);
 CustomBasketAdapter.this.notifyDataSetChanged();

答案 1 :(得分:0)

没关系我找到了解决方案 我所做的就是称为构建列表的方法  来自自定义适配器

            if(context instanceof ViewBasket){
          ((ViewBasket)context).GetBasketProducts();
        }

因为删除该项目只会更改数据库上的数据,该数据通过了我再次请求信息所需的信息