Jerry Abraham
Jerry Abraham

Reputation: 1039

How to get the checked item id in custom list view with multiple selections in android

How to get the checked item id (custom id, not the position or name of the selected item -in my case order id i need to retrieve) in a custom list view with multiple selections in android. I have Order name and Order id from json and its populated in custom list view ,In the custom list view i have text view and check box but how to get the Orderid's of the selected/checked Orders.

I have a button when i click the button i need to retrieve the id not the name or position , in my case i need order id to be retrievedenter image description here

Upvotes: 1

Views: 2423

Answers (3)

Jerry Abraham
Jerry Abraham

Reputation: 1039

I Solved and got the OrderId by using below code , this worked for me and i could retrieve the custom ORDERID which i passed to the list

                      int isSelectedOrderNumber=0;
          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.isSelected()) {
                  isSelectedOrderNumber++;
                  mOpenOrdersSelected.add(new OpenOrders(objOpenOrders.getOrderID(),objOpenOrders.getOrderName()));
                  sb.append(objOpenOrders.getOrderID());
                  sb.append(",");
                }
             }

          //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;
            }
           Log.d(MainActivity.class.getSimpleName(), "cheked Order Items: " +sb);  
           Toast.makeText(getApplicationContext(), "cheked Order Items id:" +sb, Toast.LENGTH_LONG).show();

Upvotes: 0

flx
flx

Reputation: 14226

You just need to call ListView.getCheckedItemIds(). It will return a long[] with all checked ids. There is also ListView.getCheckedItemPositions() which will give you all checked positions.

Make sure you set ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) in onCreate() or whereever you set up your views (or in layout xml).

To get the checked values you just need to do this:

SparseBooleanArray checked = mListView.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
    if (checked.valueAt(i)) {
        int pos = checked.keyAt(i);
        Object o = mListView.getAdapter().getItem(pos);
        // do something with your item. print it, cast it, add it to a list, whatever..
}

Upvotes: 0

Jayabal
Jayabal

Reputation: 3619

Jerry, set your order object as tag to the view which has selection event [CheckBox, TextView, Row view], when user select item you can get selected order object from tag and you can get any member of that object(order). for ex.

Order Object

Order {
int id;
String name;
boolean isSelected;

//add getters and setters
}

void getview(...) {
     View v = //inflate view
     CheckBox cb = (CheckBox) v.findViewById(..);
     cb.setTag(yourlist.get(position));
     cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
             ((Order) buttonView.getTag()).setSelected(isChecked);
        }
     });

}

Upvotes: 0

Related Questions