KSOAP 2 Complex Class - android

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;
}
}

Related

Android Volley Array in JSON

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!

How to get foreign DatabaseField automatically with query?

I'm using ORMLite (v4.48) with my Android app. I have the table "Contact" which can contain multiple "Email" (ForeignCollectionField) and one "Personal" (DatabaseField) object. When I get the Contact object from the database I would like to automatically get (or lazy load) the Personal object which has the same Contact ID.
It already automatically gets the Email objects which I can access. But for some reason the Personal object is always "null" even though there is an entry in the Personal table.
Here are my classes:
#DatabaseTable(tableName = "Contact", daoClass = ContactDao.class)
public class Contact {
#DatabaseField(generatedId = true, columnName = PersistentObject.ID)
int id;
#DatabaseField(index = true)
String contactName;
#ForeignCollectionField(eager = false)
ForeignCollection<Email> emails;
#DatabaseField(foreign = true)
public Personal personal;
public ForeignCollection<Email> getEmails() {
return emails;
}
public void setEmails(ForeignCollection<Email> emails) {
this.emails = emails;
}
public Personal getPersonal() {
return personal;
}
public void setPersonal(Personal personal) {
this.personal = personal;
}
...
}
And
#DatabaseTable(tableName = "Email", daoClass = EmailDao.class)
public class Email {
#DatabaseField(generatedId = true, columnName = PersistentObject.ID)
int id;
#DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = PersistentObject.CONTACT_ID_FIELD_NAME) // contact_id
Contact contact;
#DatabaseField
String emailType;
#DatabaseField(canBeNull = false)
String email;
public Email() {
}
public Email(int id, Contact Contact, String emailType, String email) {
this.id = id;
this.contact = contact;
this.emailType = emailType;
this.email = email;
}
public int getId() {
return id;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public String getEmailType() {
return emailType;
}
public void setEmailType(String emailType) {
this.emailType = emailType;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
...
}
and
#DatabaseTable(tableName = "Personal", daoClass = PersonalDao.class)
public class Personal {
#DatabaseField(generatedId = true, columnName = PersistentObject.ID)
int id;
#DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = PersistentObject.CONTACT_ID_FIELD_NAME)
Contact contact;
#DatabaseField
int age;
#DatabaseField
int weight; // in grams
#DatabaseField
int height; // in cm
public Personal() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
I'm getting the data from the database like this:
QueryBuilder<Contact, Integer> queryBuilder = mContactDao.queryBuilder();
queryBuilder.orderBy("lastViewed", false);
queryBuilder.limit(limit);
PreparedQuery<Contact> preparedQuery = queryBuilder.prepare();
List<Contact> contactList = mContactDao.query(preparedQuery);
that all works well so far.
Then further down the code I can access the Email objects like this:
ForeignCollection<Email> emails = contact.getEmails();
Iterator<Email> iter = emails.iterator();
while (iter.hasNext()) {
Email iAddress = iter.next();
Log.d(TAG, "EMAIL: " + iAddress.getEmail());
Log.d(TAG, "EMAIL TYPE: " + iAddress.getEmailType());
}
Which also works perfectly. Only if I want to access the Personal object I always get NULL.
Personal personal = contact.getPersonal(); // is always NULL
I can't figure out why that is. Do I manually need to add a JOIN in the query builder? I thought it would also lazily load the data once I access it with getPersonal() like it does with getEmails()?
You did not show how entity instances are created, but i assume Personal is created after Contact has been inserted. If that is a case, then after inserting Personal you should do contact.setPersonal(personal), and contactDao.update(contact) - that way personal_id will be stored in contact row

Getting array of data in android from mysql json using volley

I am populating listview with separate item file with json string here...
item file is ticket_items
listview file is ticktsfr
Tickets.php:
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$user_id = $_GET['user_id'];
require_once('db_configuration.php');
$sql = "SELECT * FROM (SELECT a.event_name, b.ticket_name,b.ticket_quantity,
b.ticket_description, b.ticket_channel, b.ticket_start_date,
b.ticket_end_date,b.user_id FROM event a, ticket b WHERE a.event_id =
b.event_id and a.user_id = b.user_id) AS T WHERE user_id='"$user_id"'";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res))
{
array_push($result,array
(
"event_name"=>$row[0],
"ticket_name"=>$row[1],
"ticket_quantity"=>$row[2],
"ticket_description"=>$row[3],
"ticket_channel"=>$row[4],
"ticket_start_date"=>$row[5],
"ticket_end_date"=>$row[6],
"user_id"=>$row[7]
)
);
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
Tickets.java:(Activity)
public class Tickets extends Fragment {
//boolean variable to check user is logged in or not
//initially it is false
boolean loggedIn = false;
private ListView listView;
private static final String JSON_URL =
"http://myip/eevento/tickets.php?user_id=";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.ticketsfr, null);
listView = (ListView) view.findViewById(R.id.list3);
sendRequest();
return view;
}
private void sendRequest(){
String url = JSON_URL+Loginhandler.USER_ID;
StringRequest stringRequest = new StringRequest(url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error){
Toast.makeText(getActivity(),error.getMessage(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private void showJSON(String json) {
Ticketshandler2 pj = new Ticketshandler2(json);
pj.parseJSON();
Ticketshandler cl = new Ticketshandler(getActivity(),
Ticketshandler2.event_name, Ticketshandler2.ticket_name,
Ticketshandler2.ticket_quantity, Ticketshandler2.ticket_description,
Ticketshandler2.ticket_channel, Ticketshandler2.ticket_start_date,
Ticketshandler2.ticket_end_date, Ticketshandler2.user_id);
listView.setAdapter(cl);
}
}
Ticketshandler.java:(Class)
public class Ticketshandler extends ArrayAdapter<String> {
TextView namme;
ProgressDialog loading;
private String[] event_name;
private String[] ticket_name;
private String[] ticket_quantity;
private String[] ticket_description;
private String[] ticket_channel;
private String[] ticket_start_date;
private String[] ticket_end_date;
private String[] user_id;
private Activity context;
public Ticketshandler(Activity context, String[] event_name, String[]
ticket_name,String[] ticket_quantity,String[] ticket_description,String[]
ticket_channel,String[] ticket_start_date,String[] ticket_end_date,String[]
user_id) {
super(context, R.layout.tickets_item, event_name);
this.context = context;
this.event_name = event_name;
this.ticket_name = ticket_name;
this.ticket_quantity = ticket_quantity;
this.ticket_description = ticket_description;
this.ticket_channel = ticket_channel;
this.ticket_start_date = ticket_start_date;
this.ticket_end_date = ticket_end_date;
this.user_id = user_id;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.tickets_item, null, true);
TextView event_topic_description = (TextView)
listViewItem.findViewById(R.id.titem);
String output = "" + event_name[position] + "\n" + ticket_name[position]
+ "\n" + ticket_quantity[position] + "\n" + ticket_description[position] +
"\n"
+ ticket_channel[position] + "\n" + ticket_start_date[position] + "\n" +
ticket_end_date[position];
// String output = "" +event_name[position];
event_topic_description.setText(output);
System.out.println(output);
return listViewItem;
}
}
Ticketshandler2.java:(Class)
public class Ticketshandler2 {
public static String[] event_name;
public static String[] ticket_name;
public static String[] ticket_quantity;
public static String[] ticket_description;
public static String[] ticket_channel;
public static String[] ticket_start_date;
public static String[] ticket_end_date;
public static String[] user_id;
public static final String JSON_ARRAY = "result";
public static final String EVENT_NAME = "event_name";
public static final String TICKET_NAME = "ticket_name";
public static final String TICKET_QUANTITY = "ticket_quantity";
public static final String TICKET_DESCRIPTION = "ticket_description";
public static final String TICKET_CHANNEL = "ticket_channel";
public static final String TICKET_START_DATE = "ticket_start_date";
public static final String TICKET_END_DATE = "ticket_end_date";
public static final String USER_ID = "user_id";
private JSONArray users = null;
private String json;
public Ticketshandler2(String json){
this.json = json;
}
protected void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);
users = jsonObject.getJSONArray(JSON_ARRAY);
System.out.println(users);
event_name = new String[users.length()];
ticket_name = new String[users.length()];
ticket_quantity = new String[users.length()];
ticket_description = new String[users.length()];
ticket_channel = new String[users.length()];
ticket_start_date = new String[users.length()];
ticket_end_date = new String[users.length()];
user_id = new String[users.length()];
for(int i=0;i<users.length();i++){
JSONObject jo = users.getJSONObject(i);
event_name[i] = jo.getString(EVENT_NAME);
ticket_name[i] = jo.getString(TICKET_NAME);
ticket_quantity[i] = jo.getString(TICKET_QUANTITY);
ticket_description[i] = jo.getString(TICKET_DESCRIPTION);
ticket_channel[i] = jo.getString(TICKET_CHANNEL);
ticket_start_date[i] = jo.getString(TICKET_START_DATE);
ticket_end_date[i] = jo.getString(TICKET_END_DATE);
user_id[i] = jo.getString(USER_ID);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Log:
"12-09 12:00:17.750 31591-31591/com.example.dell.evento W/System.err:
org.json.JSONException: Value <br of type java.lang.String cannot be
converted to JSONObject"
I think my query has some issues, but it is working perfectly in wamp mysql separately..I dont understand what is the issue,, help...
Your response code might be not escape from htmlentities.
you need to first escape html string to proper character code.because java default not took any character code.

How to create DataTableConfig for a class with a ForeignCollection?

I have two legacy classes that I want to persist via ormlite 4.48.
Those classes can't be modified, so I can't use the nice annotations.
Fortunately, DatabaseTableConfig came to the rescue.
Here my classes:
public class RedditLink extends RedditType {
// Thing
private String id;
private String name;
private String kind;
private String data;
// Votable
private int ups;
private int downs;
// Uncomment if this ever matters private boolean likes;
// Created
private long created_utc;
// Link
private String author;
private String domain;
private int num_comments;
private boolean over18;
private boolean is_self;
private String permalink;
private String subreddit;
private String subredditId;
private String title;
private String url;
private String selftext;
private String thumbnail;
private RedditLinkList list;
... Getters and setters
}
and
public class RedditLinkList {
private int id;
public static final RedditLinkList EMPTY = new RedditLinkList();
public Collection<RedditLink> links = new ArrayList<RedditLink>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Collection<RedditLink> getLinks() {
return links;
}
public void setLinks(Collection<RedditLink> links) {
this.links = links;
}
}
I get the following error
java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class rainstudios.kelo.data.model.RedditLinkList
at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:215)
at com.j256.ormlite.table.DatabaseTableConfig.fromClass(DatabaseTableConfig.java:146)
at com.j256.ormlite.table.TableInfo.<init>(TableInfo.java:53)
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:151)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:107)
at com.j256.ormlite.dao.BaseDaoImpl$4.<init>(BaseDaoImpl.java:907)
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:907)
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:70)
at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:297)
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:201)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.DaoManager.doCreateDao(DaoManager.java:359)
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:129)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:229)
at com.j256.ormlite.table.TableUtils.createTableIfNotExists(TableUtils.java:84)
at com.octo.android.robospice.persistence.ormlite.InDatabaseObjectPersisterFactory.createTableIfNotExists(InDatabaseObjectPersisterFactory.java:106)
at com.octo.android.robospice.persistence.ormlite.InDatabaseObjectPersisterFactory.initializeTablesIfNeeded(InDatabaseObjectPersisterFactory.java:120)
at com.octo.android.robospice.persistence.ormlite.InDatabaseObjectPersisterFactory.createObjectPersister(InDatabaseObjectPersisterFactory.java:69)
at com.octo.android.robospice.persistence.CacheManager.getObjectPersister(CacheManager.java:183)
at com.octo.android.robospice.persistence.CacheManager.loadDataFromCache(CacheManager.java:68)
at com.octo.android.robospice.request.DefaultRequestRunner.loadDataFromCache(DefaultRequestRunner.java:239)
at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:88)
at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:201)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Here is the code I use to generate my DatabaseFieldConfig :
//
// Link
//
List<DatabaseFieldConfig> fieldConfigs = new ArrayList<DatabaseFieldConfig>();
DatabaseFieldConfig field1 = new DatabaseFieldConfig("id");
field1.setId(true);
fieldConfigs.add(field1);
DatabaseFieldConfig field2 = new DatabaseFieldConfig("name");
field2.setCanBeNull(false);
fieldConfigs.add(field2);
DatabaseFieldConfig field3 = new DatabaseFieldConfig("url");
fieldConfigs.add(field3);
DatabaseFieldConfig field4 = new DatabaseFieldConfig("title");
fieldConfigs.add(field4);
DatabaseFieldConfig field6 = new DatabaseFieldConfig("author");
fieldConfigs.add(field6);
DatabaseFieldConfig field5 = new DatabaseFieldConfig("subreddit");
fieldConfigs.add(field5);
// #DatabaseField(foreign = true, foreignAutoRefresh = true,
// canBeNull =
// true, columnName = "list_id")
DatabaseFieldConfig field7 = new DatabaseFieldConfig("list");
field7.setForeign(true);
field7.setForeignAutoRefresh(true);
field7.setCanBeNull(true);
field7.setColumnName("list_id");
fieldConfigs.add(field7);
DatabaseTableConfig<RedditLink> link = new DatabaseTableConfig<RedditLink>(
RedditLink.class, fieldConfigs);
//
// List
//
List<DatabaseFieldConfig> fieldListConfigs = new ArrayList<DatabaseFieldConfig>();
DatabaseFieldConfig fieldList1;
fieldList1 = new DatabaseFieldConfig("id");
fieldList1.setColumnName("list_id");
fieldList1.setGeneratedId(true);
fieldListConfigs.add(fieldList1);
DatabaseFieldConfig fieldList2 = new DatabaseFieldConfig("links");
fieldList2.setForeignCollection(true);
fieldList2.setForeignCollectionEager(false);
fieldListConfigs.add(fieldList2);
DatabaseTableConfig<RedditLinkList> list = new DatabaseTableConfig<RedditLinkList>(
RedditLinkList.class, fieldListConfigs);
// Cross references
field7.setForeignTableConfig(list);
fieldList2.setForeignTableConfig(link);
What am I doing wrong?
Note: my app uses ormlite_config.txt cache

Ormlite - deleting nested objects from ForeignCollection but not from database

I have ContactItem which contains ForeignCollection of GroupItem:
public class ContactItem implements Parcelable{
#DatabaseField(generatedId = true)
private Integer id;
#DatabaseField(dataType = DataType.STRING, columnName = Constants.CONTACT_NAME)
private String name;
#DatabaseField(dataType = DataType.STRING, columnName = Constants.CONTACT_NUMBER)
private String number;
#DatabaseField(dataType = DataType.INTEGER, columnName = Constants.CONTACT_ICON)
private int icon;
#DatabaseField(dataType = DataType.INTEGER, columnName = Constants.CONTACT_DAYS)
private int days;
#DatabaseField(foreign=true,canBeNull = true,foreignAutoRefresh = true,columnName = Constants.CONTACT_GROUP)
private GroupItem group;
#ForeignCollectionField(columnName = Constants.CONTACT_GROUP_LIST)
private ForeignCollection<GroupItem> groups;
}
and GroupItem, which contains ForeignCollection of ContactItem:
public class GroupItem implements Parcelable{
#DatabaseField(generatedId = true)
private Integer id;
#DatabaseField(dataType = DataType.STRING, columnName = Constants.GROUP_NAME)
private String name;
#DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_ICON)
private int icon;
#DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_COUNT)
private int count;
#DatabaseField(foreign=true,canBeNull = true,foreignAutoRefresh = true, columnName = Constants.GROUP_CONTACT)
private ContactItem contact;
#ForeignCollectionField(eager = true, columnName=Constants.GROUP_CONTACTS_LIST)
private ForeignCollection<ContactItem> contacts;
#DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_DAYS)
private int days;
}
and i need to delete ContactItem from GroupItem's ForeignCollection of ContactItems. I do it like that:
public void removeContactFromGroup(GroupItem group, ContactItem contact)
{
DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
Dao<GroupItem,Integer> daoGroup = null;
Dao<ContactItem,Integer> daoContact = null;
GroupItem newGroup = null;
try {
daoGroup = db.getGroupDao();
daoContact = db.getContactDao();
UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
newGroup = daoGroup.queryForId(group.getId());
if ( newGroup.getContactCollection().contains(contact))
{
}
}
catch(Exception e)
{
Log.i(" saveGroupContacts database problem","It is cause problem");
e.printStackTrace();
}
}
but it delets ContactItem from whole database. But I need to remove it only from ForeignCollection. How can I implement this?
I solved my issue by maybe not elegant, but workable solution : I simply set my nested object in those, which contains it to null and update object-container with dao :
public void removeContactFromGroup(GroupItem group, ContactItem contact)
{
DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
Dao<GroupItem,Integer> daoGroup = null;
Dao<ContactItem,Integer> daoContact = null;
GroupItem newGroup = null;
ContactItem contactFromDb = null;
try {
daoGroup = db.getGroupDao();
daoContact = db.getContactDao();
UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
newGroup = daoGroup.queryForId(group.getId());
contactFromDb = daoContact.queryForId(contact.getId());
contactFromDb.setContactGroup(null);
daoContact.update(contactFromDb);
}
catch(Exception e)
{
Log.i(" saveGroupContacts database problem","It is cause problem");
e.printStackTrace();
}
}
Worked for me.

Categories

Resources