0

我在可扩展的自定义适配器中有一个带有复选框和子元素的自定义适配器,并且当父项 - 对于我的情况 Orders 被选中/选中时,Orders 被传递给没有 checkbox 的新自定义适配器。我能够传递父元素,即订单,但我无法传递子元素,即作为 ORDERS 子元素的项目。所以我需要将选择的订单的子元素传递给新的适配器视图。 在此处输入图像描述

在此处输入图像描述

我的代码:

      try {     
            System.out.println("READ/PARSING JSON");                            
            serverStatus = jobj.getString("SERVER_STATUS"); 
                System.out.println("serverStatusObj:  "+serverStatus);                          
                JSONArray serverResponseArray2=jobj.getJSONArray("SERVER_RESPONSE");    
                for (int m = 0; m < serverResponseArray2.length(); m++) {
                     String SERVER_RESPONSE = serverResponseArray2.getString(m);                 
                     JSONObject Open_Orders_obj = new JSONObject(SERVER_RESPONSE);
                     mMAX_ORDERS_TOBEPICKED = Open_Orders_obj.getInt("MAX_ORDERS_TOBEPICKED");  
                     JSONArray ja = Open_Orders_obj.getJSONArray("ORDER_ITEM_DETAILS");     
                     order_Item_Values.clear(); 
                     mOpenOrders = new ArrayList<OpenOrders>(ja.length());                  
                        for(int i=0; i<ja.length(); i++){
                           String ORDER_ITEM_DETAILS = ja.getString(i);                            
                           jobj1 = new JSONObject(ORDER_ITEM_DETAILS);
                           String ORDERNAME = jobj1.getString("ORDERNAME");     
                           String ORDERID = jobj1.getString("ORDERID");    
                           final OpenOrders parent = new OpenOrders();
                           parent.setOrderName(ORDERNAME+ " "+  i);
                           parent.setOrderID(ORDERID);
                           parent.setChecked((i % 2) == 0);                      
                          OpenOrders    openOrderObj= new OpenOrders(ORDERID,ORDERNAME);                                
                           JSONArray Order_Items = jobj1.getJSONArray("ITEMS"); 
                            itemList =new ArrayList<String>();                            
                            parent.setChildren(new ArrayList<Child>());
                            for(int k=0; k<Order_Items.length(); k++){
                                String ITEMS = Order_Items.getString(k);
                                System.out.println(ITEMS);
                                ItemObj = new JSONObject(ITEMS);
                                String ITEMNUMBER = ItemObj.getString("ITEMNUMBER");    
                                String ITEMNAME = ItemObj.getString("ITEMNAME");    
                                itemList.add(ITEMNAME);//This adds item  name's  to the  ArrayList named 'itemList'
                                openOrderObj.setItemID(ITEMNUMBER);
                                openOrderObj.setItemName(ITEMNAME);
                                System.out.println("item name"+ITEMNAME);                           
                            final Child child = new Child();
                            child.setName(ITEMNAME  + i + "/" + k);
                            parent.getChildren().add(child);                                  
                            }
                            mOpenOrders.add(parent);
                        }                 
                   }

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }  //***End code to read  json content from text file saved in device   
enter code here

我的开放订单课程:

    import java.io.Serializable;
    import java.util.ArrayList;

    import com.kits.ddf_order_model.Child;

        public class OpenOrders implements Serializable {
            /**
             * 
             */
            private static final long serialVersionUID = 1L;
            private String orderID;
            private String orderName;    

            private boolean selected;

            private boolean checked;
            private ArrayList<Child> children;

            private String itemID;
            private String  itemName;

            public OpenOrders(String orderID, String orderName) {
                super();
                this.orderID = orderID;
                this.orderName = orderName;
            }
            public OpenOrders() {
                // TODO Auto-generated constructor stub
            }
            public String getOrderID() {
                return orderID;
            }
            public void setOrderID(String orderID) {
                this.orderID = orderID;
            }
            public String getOrderName() {
                return orderName;
            }
            public void setOrderName(String orderName) {
                this.orderName = orderName;
            }
             @Override
             public String toString() {          
                 return  this.orderName;
             }



             public boolean isSelected() {
              return selected;
             }

              public void setSelected(boolean selected) {
              this.selected = selected;
             }

                public ArrayList<Child> getChildren()
                {
                    return children;
                }

                public void setChildren(ArrayList<Child> children)
                {
                    this.children = children;
                }
                public boolean isChecked() {
                    return checked;
                }
                public void setChecked(boolean checked) {
                    this.checked = checked;
                }
                public String getItemID() {
                    return itemID;
                }
                public void setItemID(String itemID) {
                    this.itemID = itemID;
                }
                public String getItemName() {
                    return itemName;
                }
                public void setItemName(String itemName) {
                    this.itemName = itemName;
                }
        }

我的孩子班:

    public class Child
    {
private String name;

public String getName()
{
    return name;
}

public void setName(String name)
{
    this.name = name;
}
   }

因此,当单击按钮时,我要做的是:

                               button.setOnClickListener(new OnClickListener() {            
                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                      if(mAdapter != null) {              
                                          int isSelectedOrderNumber=0;//This   Variable will check with the parameter  passed from server
                                          mOpenOrdersSelected = new ArrayList<OpenOrders>();              
                                          StringBuffer sb = new StringBuffer();
                                          Iterator<OpenOrders> it = mOpenOrders.iterator();
                                          while(it.hasNext())
                                          {
                                              OpenOrders objOpenOrders = it.next();
                                              //Do something with objOpenOrders
                                              if (objOpenOrders.isChecked()) {
                                                  isSelectedOrderNumber++;
                                                 // mOpenOrdersSelected.add(new OpenOrders(objOpenOrders.getOrderID(),objOpenOrders.getOrderName()));
                                                  sb.append(objOpenOrders.getOrderID());
                                                  sb.append(",");
                                                  final OpenOrders parent = new OpenOrders(objOpenOrders.getOrderID(),objOpenOrders.getOrderName());
                                                  ArrayList<Child>  mOpenOrderItems=objOpenOrders.getChildren();
                                                  Iterator<Child> i = mOpenOrderItems.iterator();
                                                mOpenOrdersSelected.add(parent);
                                                }
                                              }

                                          //Below Condition Will Check the selected Items With parameter passed "mMAX_ORDERS_TOBEPICKED"
                                             if(isSelectedOrderNumber<1){
                                                ShowErrorDialog("Please Select atleast One order");
                                                return;
                                              }
                                            if(isSelectedOrderNumber>mMAX_ORDERS_TOBEPICKED){
                                                ShowErrorDialog(" Select Maximum of  "+mMAX_ORDERS_TOBEPICKED+ " Orders only to process");
                                                return;
                                              }
                                     //Below code is to Call again the adapter and Displays the Order's which are checked/selected.                                            
                                        expListView = (ExpandableListView) findViewById(R.id.expandable_order_item_list);                                       
                                        ExpandableOrderSelectedListAdapter mOrderSelectedAdapter = new ExpandableOrderSelectedListAdapter(SelectLocationActivity.this,mOpenOrdersSelected);
                                        expListView.setAdapter(mOrderSelectedAdapter);                         
                                        expListView.setOnItemClickListener(new OnItemClickListener() {
                                                @Override
                                                public void onItemClick(AdapterView<?> parent, View view,
                                                        int position, long id) {
                                                    //nothing to do , as  Null Pointer exception Occurred ,to avoid that I just used this "setOnItemClickListener"
                                                }
                                                });

                                         button.setVisibility(View.GONE);//Hide  the  Initial Button in the view

                                         fullfilment_btn.setVisibility(View.VISIBLE);//Displays the confirm Button
                                         fullfilment_btn.setOnClickListener(new OnClickListener() {             
                                            @Override
                                            public void onClick(View arg0) {
                                                // TODO Auto-generated method stub

                                              }
                                            });
                                      }
                                }
                            });

所以在这部分代码中,我在迭代子元素时遇到错误。

任何帮助都会很棒

4

1 回答 1

0

我通过一个 for 循环解决了这个问题,它将孩子添加到新的“mOpenOrderSelected”变量中:

  if (objOpenOrders.isChecked()) {
                                              isSelectedOrderNumber++;
                                              sb.append(objOpenOrders.getOrderID());
                                              sb.append(",");
                                              final OpenOrders parent = new OpenOrders(objOpenOrders.getOrderID(),objOpenOrders.getOrderName());
                                              ArrayList<Child>  mOpenOrderItems=objOpenOrders.getChildren();                                                     
                                            parent.setChildren(new ArrayList<Child>());
                                            for(int k=0; k<mOpenOrderItems.size(); k++){                                                      
                                               final Child child = (Child) mOpenOrderItems.get(k);
                                               Log.d("ChildItemsname", "ChildItemsname:"+child.getName());
                                               parent.getChildren().add(child);  
                                              }
                                            mOpenOrdersSelected.add(parent);
                                            }

所以新的按钮点击代码将是这样的:

         button.setOnClickListener(new OnClickListener() {          
                                @Override
                                public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                      if(mAdapter != null) {              
                                          int isSelectedOrderNumber=0;//This   Variable will check with the parameter  passed from server
                                          mOpenOrdersSelected = new ArrayList<OpenOrders>();              
                                          StringBuffer sb = new StringBuffer();
                                          Iterator<OpenOrders> it = mOpenOrders.iterator();
                                          while(it.hasNext())
                                          {
                                              OpenOrders objOpenOrders = it.next();
                                              //Do something with objOpenOrders
                                              if (objOpenOrders.isChecked()) {
                                                  isSelectedOrderNumber++;
                                                  sb.append(objOpenOrders.getOrderID());
                                                  sb.append(",");
                                                  final OpenOrders parent = new OpenOrders(objOpenOrders.getOrderID(),objOpenOrders.getOrderName());
                                                  ArrayList<Child>  mOpenOrderItems=objOpenOrders.getChildren();                                                     
                                                parent.setChildren(new ArrayList<Child>());
                                                for(int k=0; k<mOpenOrderItems.size(); k++){                                                      
                                                   final Child child = (Child) mOpenOrderItems.get(k);
                                                   Log.d("ChildItemsname", "ChildItemsname:"+child.getName());
                                                   parent.getChildren().add(child);  
                                                  }
                                                mOpenOrdersSelected.add(parent);
                                                }
                                              }

                                          //Below Condition Will Check the selected Items With parameter passed "mMAX_ORDERS_TOBEPICKED"
                                             if(isSelectedOrderNumber<1){
                                                ShowErrorDialog("Please Select atleast One order");
                                                return;
                                              }
                                            if(isSelectedOrderNumber>mMAX_ORDERS_TOBEPICKED){
                                                ShowErrorDialog(" Select Maximum of  "+mMAX_ORDERS_TOBEPICKED+ " Orders only to process");
                                                return;
                                              }
                                     //Below code is to Call again the adapter and Displays the Order's which are checked/selected.                                            
                                        expListView = (ExpandableListView) findViewById(R.id.expandable_order_item_list);                                       
                                        ExpandableOrderSelectedListAdapter mOrderSelectedAdapter = new ExpandableOrderSelectedListAdapter(SelectLocationActivity.this,mOpenOrdersSelected);
                                        expListView.setAdapter(mOrderSelectedAdapter);                         
                                        expListView.setOnItemClickListener(new OnItemClickListener() {
                                                @Override
                                                public void onItemClick(AdapterView<?> parent, View view,
                                                        int position, long id) {
                                                    //nothing to do , as  Null Pointer exception Occurred ,to avoid that I just used this "setOnItemClickListener"
                                                }
                                                });

                                         button.setVisibility(View.GONE);//Hide  the  Initial Button in the view
                                         fullfilment_btn.setVisibility(View.VISIBLE);//Displays the confirm Button
                                         fullfilment_btn.setOnClickListener(new OnClickListener() {             
                                            @Override
                                            public void onClick(View arg0) {
                                                // TODO Auto-generated method stub

                                              }
                                            });
                                      }
                                }
                            });
于 2013-09-17T10:17:19.547 回答