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.
Related
I've looked at similar questions but they don't explain how to use a CursorAdapter.
What I'm trying to do is populate a Spinner with a column of Strings read from a selected database.
This is my current code:
//open database depending on selected item
myDatabase = openOrCreateDatabase("Courses"+spSem.getSelectedItem().toString(),MODE_PRIVATE,null);
//read fields to be populated and store them in an arraylist, I need to use arraylist since not every database has the same size
Cursor resultSet = myDatabase.rawQuery("Select * from Subject",null);
resultSet.moveToFirst();
List<String> course = new ArrayList<String>();
for (int i = 1; i < course.size(); i++) {
course.add(resultSet.getString(i));
}
This next part is where I'm stuck, I'm thinking I need to use CursorAdapter() to populate the spinner, but I have no idea how? Previously I had this:
String[] cArr = new String[course.size()];
course.toArray(cArr);
CursorAdapter a = new CursorAdapter(this,cArr,android.R.layout.simple_spinner_item);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCourse.setAdapter(a);
With this code I get the error "CursorAdapter is abstract and cannot be instantiated", and "cannot resolve method'setDropDownView'".
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 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.
I'm displaying data from SQLite using
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
employees,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
getListView().setAdapter(adapter);
empoyees - my Cursor.
Data is showing correctly, but how to get what row I used from SQLite duaring OnClick on my item from List?
For example I have 2 tables
1)Category
_id Integer
Name Text
2)Articles
_id Integer
Name text
CategoryId (foreing key)
So on first screen I'm displaying all Categorys, then on list item click I want to display Articles that are from specified category, how to do that?
Don't use your cursor in your adapter. Build up an array and send in the array instead. In that array, typically something like List<MyDataRecordHolder> (where MyDataRecordHolder is an object you've created and filled with data from the database), you will have to set a reference to a row id.
UPDATE:
Create your MyDataRecordHolder: (Pseudo code)
List<MyDataRecordHolder> list = new List<MyDataRecordHolder>();
in your loop where you fetch data:
MyDataRecordHolder record = new MyDataRecordHolder();
record.setId(cursor.get("rowid"))
//set any other pertinent data
list.add(record);
For your adapter:
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
list,
new String[] {"FirstName","LastName"},
new int[] {android.R.id.text1,android.R.id.text2},0);
Note, you are now supplying the List (list) to the adapter.
In your OnClick for your row, in the adapter, you will get the current MyDataRecordHolder:
final MyRecordHolder record = getItem(position);
String id = record.getId();
//make new db query based on your id