2016-04-21 50 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(); 
} 
相關問題