I have this kind of JSON response
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
And I am able to parse country, country_id, and currency without a problem, problem starts with the product list when I am trying to parse it! below the code
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<Tarif> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<Tarif>>(){}.getType());
new DtoneTarifs(country, country_id, currency, tarifs);
}
}
And here is my Tarif and Other Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<Tarif> tarifList;
public Tarifs (String country, int country_id, String currency, List<Tarif> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
I want to fill the product_list in Tarif class where only one parameter accept and show them in recycler_view
{"error":false,"country":"United Kingdom","country_id":"903",
"currency":"GBP","product_list":["5","10","15","20","25","30","40","50"]}
You can see that product_list is JSON Array of string values. But you are converting it into list of Tarif type. It should be converted into list of string type.
Either set values of Tarif as custom objects to JSON Array or change your list type to string.
It should be like this:
try {
boolean error = response.getBoolean("error");
if (!error){
String country = response.getString("country");
int country_id = response.getInt("country_id");
String currency = response.getString("currency");
List<String> tarifs = new
Gson().fromJson(response.getJSONArray("product_list").toString(), new
TypeToken<List<String>>(){}.getType());
Tarifs result = new Tarifs(country, country_id, currency, tarifs);
}
}
Tarifs Class
public class Tarifs {
public String country;
public int country_id;
public String currency;
public List<String> tarifList;
public Tarifs (String country, int country_id, String currency, List<String> tarif){
this.country = country;
this.country_id = country_id;
this.currency = currency;
this.tarifList = tarif;
}
}
Here you go!
Related
I am getting array list from Global class and getting value with index , but i am getting error on sub-string is that (Sub-string cannot be resolved).
Glide.with(ChooseCategoryProductsActivity.this)
.load("file:///" + GlobalClass.IMAGES_PATH + "/" + GlobalClass.categoriesAr.get(GlobalClass.currentIndex)
.substring(categoriesAr.get(GlobalClass.currentIndex)
.lastIndexOf("/") + 1))
.placeholder(R.drawable.stub)
.into(categoryImage);
Category class :
public class Category {
public String catId = "";
public String catName = "";
public String catImage = "";
public String catDesc = "";
public String displayOrder = "";
public String createdDate = "";
public ArrayList<Product> productsAr = new ArrayList<Product>();
public Category(String catId, String catName, String catImage, String catDesc, String displayOrder, String createdDate) {
this.catId = catId;
this.catName = catName;
this.catImage = catImage;
this.catDesc = catDesc;
this.displayOrder = displayOrder;
this.createdDate = createdDate;
}
}
As you Declared
ArrayList<Category> categoriesAr = new ArrayList<Category>();
subString() is only applied on String not on custom object
There is no such method called subString() with glide as far as i know and hence the compile time error Sub string cannot be resolved. subString works with String
So finally i did just like following, but i don't know whether is it right or not :
GlobalClass.categoriesAr.get(GlobalClass.currentIndex).toString().substring(categoriesAr.get(GlobalClass.currentIndex)
ArrayList arReceipient = new ArrayList(); is declared globally. The arraylist is populated as follows
arReceipient.add(new MyItem(data.get(i).getId(),data.get(i).getNickName()));
Resulting in
sId 1000002327 sName htc1
sId 1000002208 sName htcandroid
sId 1000002208 sName htcandroid
sId 1000002242 sName htcandroid1
sId 1000000721 sName bachan
sId 1000000721 sName bachan
sId 1000000810 sName bachan2
How can i remove duplicates entries such that result is
sId 1000002327 sName htc1
sId 1000002208 sName htcandroid
sId 1000002242 sName htcandroid1
sId 1000000721 sName bachan
sId 1000000810 sName bachan2
Here is MyItem class
public class MyItem {
public String sId;
public String sName;
public MyItem(String sid, String sname){
this.sId=sid;
this.sName=sname;
}
}
instead of list use set.
LinkedHashSet<MyItem> arReceipient =new LinkedHashSet<MyItem>();
and add equals method in MyItem
public class MyItem {
public String sId;
public String sName;
public MyItem(String sid, String sname){
this.sId=sid;
this.sName=sname;
}
public boolean equals(Object o){
if(!(o instanceof MyItem)) return false;
return sId==((MyItem)o).sId;
}
}
ArrayList list = new ArrayList();
HashSet hashmap = new HashSet();
hashmap.addAll(list);
list.clear();
list.addAll(hashmap);
I have a listview that shows this paramaters. But 2 of the parameters returns the same value? How can I differentiate this two? Its email and voucher.
VoucherObj obj = new VoucherObj();
obj.customerID=item[0];
obj.type=item[1];
obj.name=item[2];
obj.searchStr=item[3]; <---- SAME PARAMETER
obj.searchStr=item[4]; <---- SAME PARAMETER
obj.branch=item[5];
obj.issued=item[6];
obj.expiration=item[7];
obj.status=item[8];
obj.vouchername=item[9];
obj.employeeid=item[10];
items.add(obj);
Voucher.class
public class VoucherObj {
public String customerID="";
public String type="";
public String name="";
public String email="";
public String voucher="";
public String branch="";
public String issued = "";
public String expiration="";
public String status = "";
public String vouchername = "";
public String employeeid = "";
public String searchStr;
}
If you'll have a static amount of items, you can declare the searchStr field of your VoucherObj as a static array with a fixed size. Assuming you're storing Strings, this would be:
String[] searchStr = new String[10];
If the number of items of that field is unknown, just use a more advanced data structure, for instance:
ArrayList<String> searchStr = new ArrayList<String>();
Afterwards you can use this method to add a value:
obj.searchStr.add("value1");
obj.searchStr.add("value2");
...
public class VoucherObj(){
int customerId;
..............
..............
ArrayList<String> searchStr;
..............
/*other parameters here*/
}
and in your main class use
obj.searchStr.add(item[3]);
obj.searchStr.add(item[4]);
I need to send my OrderItemList Class with OrderItem Class Array inside, I try this one but gives me error. Cannot serialize: foo.foo.OrderItemList#461e0bf8
Thanks for your time in advance.
SoapObject request = new SoapObject(NAMESPACE, WebService);
OrderItemList orderItemList = null;
PropertyInfo pinfo = new PropertyInfo();
pinfo.name = "orderItems";
pinfo.namespace = NAMESPACE ;
pinfo.type = OrderItemList.class;
ArrayList<OrderItem> orderItemListT = new ArrayList<OrderItem>();
orderItemListT.add(new OrderItem(9,9,"",9,9,9,9));
orderItemList = new OrderItemList(orderItemListT);
request.addProperty(pinfo,orderItemList);
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.addMapping(NAMESPACE, "orderItems", orderItemList.getClass());
envelope.addMapping(NAMESPACE, "OrderItem", OrderItem.class);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
androidHttpTransport.call("http://tempuri.org/" + WebService , envelope);
OrderItem >
public class OrderItem {
public int ID;
public int OrderId;
public String FinalCode;
public int Quantity;
public double Price;
public double Discount;
public int Status;
public OrderItem(int id, int orderId, String finalCode ,int quantity, double price, double discount, int status) {
ID = id;
OrderId = orderId;
FinalCode = finalCode;
Quantity = quantity;
Price = price;
Discount = discount;
Status = status;
}
}
OrderItemList >
public class OrderItemList {
public ArrayList<OrderItem> OrderItemList;
public OrderItemList(ArrayList<OrderItem> orderItemList) {
OrderItemList = orderItemList;
}
}
Implement Serializable in your class
import java.io.Serializable;
public class OrderItem implements Serializable {
public int ID;
public int OrderId;
public String FinalCode;
public int Quantity;
public double Price;
public double Discount;
public int Status;
public OrderItem(int id, int orderId, String finalCode ,int quantity, double price, double discount, int status) {
ID = id;
OrderId = orderId;
FinalCode = finalCode;
Quantity = quantity;
Price = price;
Discount = discount;
Status = status;
}
}
Can you please help me for sorting a list alphabetically
My code
emailList.add(contact.getUserName());
String[] emails = new String[emailList.size()];
emailList.toArray(emails);
namesList.add(name);
Collections.sort(emailList, new Comparator() {
public int compare(Object o1, Object o2) {
String name1 = (String) o1;
String name2 = (String) o2;
return name1.compareToIgnoreCase(name2);
}
});
System.out.println("namesList.toString() = " + namesList.toString());
You don't need to create a new comparator. Just call Collections.sort(emailList);.
UPDATE:
Collections.sort(emailList, new Comparator<String>()
{
#Override
public int compare(String text1, String text2)
{
return text1.compareToIgnoreCase(text2);
}
});