how to retrieve the orderID from custom list view in Android
Can any one help me in retrieving the order id from custom list view with
check box. I need to get the order id, which are checked, when the show
button is clicked, i need to get the order id which is added to the
mopenOrders array list . the checked/selected order id's from the custom
list view i must retrieve, below is the image and my code
My main activity:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
ListView mListView;
Button btnShowCheckedItems;
ArrayList<Product> mProducts;
ArrayList<OpenOrders> mOpenOrders;
MultiSelectionAdapter<OpenOrders> mAdapter;
public String serverStatus =null;
private InputStream is;
private AssetManager assetManager;
ArrayList<String> order_Item_Values =new
ArrayList<String>();
int mMAX_ORDERS_TOBEPICKED; //Parameter passed from
server for the selection of max order
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assetManager=getAssets();
bindComponents();
init();
addListeners();
}
private void bindComponents() {
// TODO Auto-generated method stub
mListView = (ListView) findViewById(android.R.id.list);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
btnShowCheckedItems = (Button)
findViewById(R.id.btnShowCheckedItems);
}
private void init() {
// TODO Auto-generated method stub
try {
is = assetManager.open("open_order_item_details.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jString = getStringFromInputStream(is);
String fileContent=jString;
JSONObject jobj = null;
JSONObject jobj1 = null;
JSONObject ItemObj=null;
try {
jobj = new JSONObject(fileContent);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
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>();
for(int i=0; i<ja.length(); i++){
String ORDER_ITEM_DETAILS = ja.getString(i);
// System.out.println(ORDER_ITEM_DETAILS);
jobj1 = new JSONObject(ORDER_ITEM_DETAILS);
String ORDERNAME = jobj1.getString("ORDERNAME");
String ORDERID = jobj1.getString("ORDERID");
mOpenOrders.add(new OpenOrders(ORDERID,ORDERNAME));
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mAdapter = new MultiSelectionAdapter<OpenOrders>(this, mOpenOrders);
mListView.setAdapter(mAdapter);
}
private void addListeners() {
// TODO Auto-generated method stub
btnShowCheckedItems.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mAdapter != null) {
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..
Log.d(MainActivity.class.getSimpleName(), "cheked Items: " +
o.toString());
Toast.makeText(getApplicationContext(), "chked Items:: "
+o.toString(), Toast.LENGTH_LONG).show();
}
}
ArrayList<OpenOrders> mArrayProducts = mAdapter.getCheckedItems();
Log.d(MainActivity.class.getSimpleName(), "Selected Items: " +
mArrayProducts.toString());
}
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
}
MultiSelectionAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class MultiSelectionAdapter<T> extends BaseAdapter{
Context mContext;
LayoutInflater mInflater;
ArrayList<T> mList;
SparseBooleanArray mSparseBooleanArray;
public MultiSelectionAdapter(Context context, ArrayList<T> list) {
// TODO Auto-generated constructor stub
this.mContext = context;
mInflater = LayoutInflater.from(mContext);
mSparseBooleanArray = new SparseBooleanArray();
mList = new ArrayList<T>();
this.mList = list;
}
public ArrayList<T> getCheckedItems() {
ArrayList<T> mTempArry = new ArrayList<T>();
for(int i=0;i<mList.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(mList.get(i));
}
}
return mTempArry;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup
parent) {
// TODO Auto-generated method stub
if(convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
tvTitle.setText(mList.get(position).toString());
CheckBox mCheckBox = (CheckBox)
convertView.findViewById(R.id.chkEnable);
mCheckBox.setTag(position);
///mCheckBox.setTag(mList.get(position).toString());
mCheckBox.setChecked(mSparseBooleanArray.get(position));
mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
return convertView;
}
OnCheckedChangeListener mCheckedChangeListener = new
OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
// TODO Auto-generated method stub
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
}
OpenOrders.java
public class OpenOrders {
private String orderID;
private String orderName;
private String itemID;
private String itemName;
public OpenOrders(String orderID, String orderName) {
super();
this.orderID = orderID;
this.orderName = orderName;
}
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 void OpenOrderItems(String itemID, String itemName) {
this.itemID = itemID;
this.itemName = itemName;
}
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;
}
}
My json: { "SERVER_STATUS": "1", "SERVER_RESPONSE": [ {
"MAX_ORDERS_TOBEPICKED":3, "ORDER_ITEM_DETAILS": [ { "ORDERNAME":
"ORDER1", "ORDERID": "1", "ITEMS": [ { "ITEMNUMBER": 1, "ITEMNAME":
"APPLE-LAPTOP", "CATEGORY":"ELECTRONICS", "ORDERQUANTITY": "5",
"PICKEDQUANTITY": "", "PRICE": "4500 DHMS", "SHOPNAME": "Indico Icon Kits
403", "ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" }, {
"ITEMNUMBER": 2, "ITEMNAME": "DEL-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731094558Victorinox%20Aug%204.JPG",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731094558Victorinox%20Aug%204.JPG"
}, { "ITEMNUMBER": 3, "ITEMNAME": "FUJI-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122832COLLECTOR-ENSEMBLE-RVB%204.JPG",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122832COLLECTOR-ENSEMBLE-RVB%204.JPG"
} ] }, { "ORDERNAME": "ORDER2", "ORDERID": "2", "ITEMS": [ { "ITEMNUMBER":
4, "ITEMNAME": "APPLE-LAPTOP", "CATEGORY":"ELECTRONICS", "ORDERQUANTITY":
"5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS", "SHOPNAME": "Indico Icon
Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg"
}, { "ITEMNUMBER": 5, "ITEMNAME": "DEL-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731125233Boss%204.JPG",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731125233Boss%204.JPG"
}, { "ITEMNUMBER": 6, "ITEMNAME": "FUJI-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg"
} ] }, { "ORDERNAME": "ORDER3", "ORDERID": "3", "ITEMS": [ { "ITEMNUMBER":
7, "ITEMNAME": "APPLE-LAPTOP", "CATEGORY":"ELECTRONICS", "ORDERQUANTITY":
"5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS", "SHOPNAME": "Indico Icon
Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg"
}, { "ITEMNUMBER": 8, "ITEMNAME": "DEL-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg"
}, { "ITEMNUMBER": 9, "ITEMNAME": "FUJI-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" } ] }, {
"ORDERNAME": "ORDER4", "ORDERID": "4", "ITEMS": [ { "ITEMNUMBER": 10,
"ITEMNAME": "AsusE-LAPTOP", "CATEGORY":"ELECTRONICS", "ORDERQUANTITY":
"5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS", "SHOPNAME": "Indico Icon
Kits 403", "ITEMIMAGEURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://www.dubaidutyfree.com/Content/upload/products/20130731122312DG_INTENSE_EDP_100ML_conv%204.jpg"
}, { "ITEMNUMBER": 11, "ITEMNAME": "hp-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" }, {
"ITEMNUMBER": 12, "ITEMNAME": "FUJI-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" } ] }, {
"ORDERNAME": "ORDER5", "ORDERID": "5", "ITEMS": [ { "ITEMNUMBER": 13,
"ITEMNAME": "Asus-k-LAPTOP", "CATEGORY":"ELECTRONICS", "ORDERQUANTITY":
"5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS", "SHOPNAME": "Indico Icon
Kits 403", "ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" }, {
"ITEMNUMBER": 14, "ITEMNAME": "wio-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403", "ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" }, {
"ITEMNUMBER": 15, "ITEMNAME": "Accer-LAPTOP", "CATEGORY":"ELECTRONICS",
"ORDERQUANTITY": "5", "PICKEDQUANTITY": "", "PRICE": "4500 DHMS",
"ITEMIMAGEURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg",
"ITEMIMAGEFULLVIEWURL":
"http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg" } ] } ] } ],
"ERROR_STATUS": "0", "ERROR_RESPONSE": [ { "ERROR_MESSAGE": "" } ] }
No comments:
Post a Comment