2016-04-21 48 views
0

这是我的代码库适配器来设置列表项目。我在notifyDataSetChanged()中的代码不起作用。删除BTN单击要删除我如何可以改变工作列表视图的项目这段代码,请帮助我不能在BaseAdapter类中使用notifyDataSetChanged()

public class CustomListviewMyCartitem extends BaseAdapter { 

    ArrayList<Mycartitem> myList = new ArrayList<Mycartitem>(); 
    LayoutInflater inflater; 
    Context context; 
    int loader = R.drawable.loader; 
    static int minteger = 0; 
    public String Response_code; 
    Mycartitem currentListData; 
    String cid, qcount; 
    UserSessionManager session; 
    String rem, b, userid, btntag; 

    public CustomListviewMyCartitem(Context context, ArrayList<Mycartitem> list) { 

     this.myList = list; 
     this.context = context; 
     inflater = LayoutInflater.from(context); 

     session = new UserSessionManager(context); 
     HashMap<String, String> user = session.getUserDetails(); 
     userid = user.get(UserSessionManager.KEY_ID); 

     Log.e("", "@myList" + myList); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return myList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return myList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public int getViewTypeCount() { 

     return getCount(); 
    } 

    @Override 
    public int getItemViewType(int position) { 

     return position; 
    } 

    /* 
    * public void updateReceiptsList() { myList.clear(); this.myList = list; 
    * this.notifyDataSetChanged(); } 
    */ 
    public void updateResults(ArrayList<Mycartitem> results) { 
     this.myList = results; 
     // Triggers the list update 
     this.notifyDataSetChanged(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     final MyViewHolder mViewHolder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.customcartlist, parent, false); 
      mViewHolder = new MyViewHolder(convertView); 
      convertView.setTag(mViewHolder); 
     } else { 
      mViewHolder = (MyViewHolder) convertView.getTag(); 
     } 

     currentListData = myList.get(position); 

     mViewHolder.txtproductnamecart.setText(currentListData.getCategoryName()); 
     mViewHolder.txtprizecart.setText("$" + currentListData.getCharge()); 
     mViewHolder.txttotalitemcart.setText(currentListData.getQuantity()); 

     mViewHolder.delete.setTag(currentListData.getCartItemId()); 

     mViewHolder.delete.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       btntag = mViewHolder.delete.getTag() + ""; 
       new DeleteItem().execute(); 

      } 
     }); 

     return convertView; 
    } 

    private class MyViewHolder { 
     TextView txtproductnamecart, txtprizecart, txttotalitemcart; 
     Button delete; 

     public MyViewHolder(View item) { 
      txtproductnamecart = (TextView) item.findViewById(R.id.txtproductnamecart); 
      txtprizecart = (TextView) item.findViewById(R.id.txtprizecart); 
      txttotalitemcart = (TextView) item.findViewById(R.id.txttotalitemcart); 
      delete = (Button) item.findViewById(R.id.btndelete); 
     } 
    } 

    private class DeleteItem extends AsyncTask<String, Void, Void> { 

     protected void onPreExecute() { 
      // Utils.Pdialog(Cart.this); 
     } 

     // Call after onPreExecute method 
     protected Void doInBackground(String... urls) { 
      request(); 
      return null; 
     } 

     protected void onPostExecute(Void unused) { 
      Utils.Pdialog_dismiss(); 
      try { 

       JSONObject jsonObject; 
       jsonObject = new JSONObject(Response_code); 
       String msg = jsonObject.getString("status"); 

       if (msg.equals("200")) { 

        JSONObject jsonArray = jsonObject.getJSONObject("payload"); 

        updateResults(myList); 

       } 

      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (Exception e) { 
       Toast.makeText(context, "No item Found", Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 
      } 

     } 
    } 

    private void request() { 
     try { 

      List<NameValuePair> values = new ArrayList<NameValuePair>(2); 

      values.add(new BasicNameValuePair("userId", userid)); 
      values.add(new BasicNameValuePair("cartItemId", btntag)); 
      values.add(new BasicNameValuePair("action", "singleItem")); 
      values.add(new BasicNameValuePair("cartId", Utils.CARTID)); 

      Log.e("", "@Parameteruserid" + userid); 
      Log.e("", "@ParametercartItemId" + currentListData.getCartItemId()); 
      Log.e("", "@Parameteaction" + "singleItem"); 
      Log.e("", "@ParametercartId" + Utils.CARTID); 

      HttpParams httpParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParams, 10000); 
      HttpConnectionParams.setSoTimeout(httpParams, 10000); 
      HttpClient client = new DefaultHttpClient(httpParams); 

      String url = Utils.link + "index.php/api/deletecartitem/"; 

      HttpPost httppost = new HttpPost(url); 

      httppost.setEntity(new UrlEncodedFormEntity(values)); 

      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      Response_code = client.execute(httppost, responseHandler); 
      Log.e("Cart item delete Response :---", "CratItemDelete" + Response_code); 

     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 
+0

许多答案张贴:)反正他们中的一个应该正常工作,如果没有工作:)你可以请确认从服务器异步的任务应答实际上是否删除了对象:P因为我在代码中找不到任何问题:D –

回答

0

相反的方法:

public void updateResults(ArrayList<Mycartitem> results) { 
    this.myList = results; 
    // Triggers the list update 
    this.notifyDataSetChanged(); 
} 

使用以下方法:

public void updateResults(ArrayList<Mycartitem> results) { 
    this.myList.clear(); 
    this.myList.addAll(results); 
    // Triggers the list update 
    this.notifyDataSetChanged(); 
} 
+0

您的建议是工作,但问题是我列表中的三个项目单击一个项目删除按钮清除所有列表,不显示剩余两个项目.. –

+0

对不起,我没有你理解了你在上面评论中提到的与3项有关的问题。你能否详细解释一下。 –

+0

3个项目中的列表视图我现在可以删除2个项目中的1个项目列表视图,问题是在那里我可以删除1个项目,其余2个项目不会在列表视图中显示通过点击删除按钮。 –

0

你没有更新myList变量。清除列表中的所有项目并用新项目更新,然后调用notifyDatasetchange。

0

变化updateResults方法

public void updateResults(ArrayList<Mycartitem> results) { 
    this.myList = results; 
    // Triggers the list update 
    this.notifyDataSetChanged(); 
    } 

代码由

public void updateResults(ArrayList<Mycartitem> results) 
{ 
myList.clear(); 
myList.addAll(results); 
this.notifyDataSetChanged(); 
} 
相关问题