I want to make the simplest query but can't find anywhere online how to do it in active android.
I have an Items table that I simply want to get all items for but my query is only returning me one.
items1.name = "item1";
items2.name = "item2";
items1.save();
items2.save();
List<Item> queryResults = new Select().from(Item.class).execute();
aToDoAdapter = new ItemAdapter(this, (ArrayList<Item>) queryResults);
My result list is always whatever the last item I saved was. How can I get all of the items in the table?
I had the same problem and I solved it like this:
return new Select()
.all()
.from(Item.class)
.execute();
This will return all the items from the Items class.
Related
I want to get some data from all users in Users table. I've found that I have to use Data paging. I've written the same code as described in Feature 47->https://backendless.com/feature-47-loading-data-objects-from-server-with-sorting/ (because I also
have to sort) , but then I've figured out that this code takes data only from first page. Then , I decided that I have to go to the next page and read it , until its size is not equal to zero. Below,you can see my wrong solution:
QueryOptions queryOptions = new QueryOptions();
List<String> list = new ArrayList<String>() ;
list.add("point DESC") ;
queryOptions.setSortBy(list);
BackendlessDataQuery backendlessDataQuery = new BackendlessDataQuery();
backendlessDataQuery.setQueryOptions(queryOptions);
Backendless.Data.of(BackendlessUser.class).find(backendlessDataQuery, new AsyncCallback<BackendlessCollection<BackendlessUser>>() {
#Override
public void handleResponse(BackendlessCollection<BackendlessUser> one) {
while(one.getCurrentPage().size()>0) {
Iterator<BackendlessUser> it = one.getCurrentPage().iterator();
while (it.hasNext()) {
//something here,not so important
}
one.nextPage(this);// here I want to get next page,
//but seems like it does not work, cause my loop became infinite
}
}
I think that I have to use nextPage method with AsyncCallback instead of one.nextPage(this) , but if I do so , the method couldn't keep up with the loop. So, how can I solve my problem?
I actually can't find the problem with your solution, but I solved this problem using:
int tot = response.getTotalObjects()
to get the total number of objects at the first response. Then use a loop until your list of objects has size = tot. In each loop you make a query setting the offset equals to the current size of the list.
I have a listview that show some items, each item contains attribute called category Id, this is attribute is related to one of my database taples
I need for each item to open database, make a query to get category object where id = item category id and then show data in the listview
it is very heavy to open database to read record by record
can anyone advice how to solve this problem without opening database many times?
Thanks
you can red the data base once. Get all data at the same time. then read the desired data from the returned array.
//returns data from DB
String[] array = getInfoDataBase();
//array example
array = ["id1", "name1", "data2","id2", "name2", "data2", ....]
for (int i = 0; i < array.length; i = i+3) {
if (array[i] == //desired id) {
//get your data from the array like name1, data1
break; // to stop the fro loop
}
}
You can use JOINS for this purpose.
SELECT * FROM item INNER JOIN category ON item.categoryId = category.id WHERE item.id in (id1, id2, id3)
You will have to replace (id1, id2, id3) with the item Ids in your table.
I'm trying to get data from databse in a listview as shown below.
long count = UserTableSugar.count(UserTableSugar.class);
if(count>0)
{
UserTableSugar.listAll(UserTableSugar.class);
List<UserTableSugar> userTable = UserTableSugar.listAll(UserTableSugar.class);
CustomAdapterListview madapter = new CustomAdapterListview(getApplicationContext(),userTable);
listView.setAdapter(madapter);
}
but,the data won't show up. On debugging, the value of count is 2 (there are two records in table). But the size of list userTable is shown 0.
SOLVED : Adding empty constructor of the model class did the trick.
With SugarORM, all model classes need an empty constructor or they cannot be used.
UserTableSugar() {} will do the job!
I have a listview in which i am adding the items whose values are retrieved from the table in my database. Now, in the current scenario, if i am deleting an item, i am passing the complete string and breaking it in parts to provide information to my delete query.
Instead of all these dumb work, i can simply pass the id of that particular item, but the id is not in the string array which is binded to the listview. How can i delete items from the database by simply using the item's id...?
Note: id is the column in table, so i have each item's id from database. But i am not displaying it in the listview.
String[] records = new String[report.size()];
for(int i = 0; i<report.size(); i++)
{
if(report.get(i).getDate()!="")
{
String eachRecord = (i+1) +". "+report.get(i).getDate() + " -- " + report.get(i).getType();
records[i] = eachRecord;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.singlerow, records);
listView.setAdapter(adapter);
You should map the data you are getting from the Database to a java object.
Read the data from the db store in list or array whatever you like.
Create your custom adapter by extending the BaseAdaper REF and pass your list of object to it.
In Adapter's getView build your view.
Now when you delete your list item you can easily find that object and ask for the id of it. You can use this id to delete the item from db. Now you can refresh the list with the remaining data.
How can I get an array with only the column "Name" of my Category model?
I can get all my categories using
List<Category> categoryList = new Select()
.from(Category.class)
.execute();
and then create another array with the name of the Category, but I guess there is a way to get it directly and I cannot find how.
A little late in answering this, but the issue is because SQLiteUtils.rawQuery() makes the assumption that the Id column is always going to be in the cursor result. Set your columns String[] to {Id, your_column} and you'll be fine.
List<Category> categoryList = new Select(new String[]{"Id,your_column"}).from(Category.class).execute();