I was wondering if I have this situation:
In our SQLite databse, there are some tables (let's say four). each table consists of two columns: Title and Content.
We retrieved the titles from all the four tables, and display them in one ListView
The question is: How can we handle the onItemClickListener so that we can retrieve the Content of the selected item given that the items are from different tables?
I think I'll come across problem like that and I just want to know if it can be handled or not.
Thanks.
I'd say use an ArrayAdapter and wrap the data in a custom class that contains the table it came from (and ID etc if you need it), then you can get the ID and table by just knowing the index in the list.
The class could look something like this:
public static class ListItem {
public String title, table;
// Maybe include these as well?
public String content;
public int id;
#Override
public String toString() {
return title; // Or something else maybe?
}
}
Then simply build your data from the cursors into a ListItem[] and create the adapter like this:
new ArrayAdapter<ListItem>(data);
One problem with this solution is that you need to load everything in memory, if there is a lot of data you can create a custom Cursor which contains all four cursors and use a CursorAdapter instead.
The solution Nicklas A. proposed will do the trick. But if you have identical items why do you keep them in 4 different tables? If you items are identical it's betten to keep them in signle table and just to add some additional column to identify them if needed. If your items are not identical create 4 model classes for each of them and handle onItemClicked in each model.
Related
I have a database table A, that contains two columns: the primary key _idA and a column called datalist.
I also have a database table B, containing multiple columns, one of them being its primary key _idB, the others contain misc data.
I want to load the datalist from table A by a specific _idA. This datalist is a string, containing _idBs separated by a comma, i.e. "31,62,612,6123,682". After that, I need to load all rows from table B, that have an _idB that is contained in the datalist of table A.
Since I am on Android, working with a Dataloader, that is connected to a Listview, if possible I would really like to do all this in one query, since I am not completely sure it is possible to do two separate queries in one Android DataLoader.
Example:
-> My program needs all items with an id that is stored in the datalist of _idA = 5
-> Get datalist from table A, where _idA = 5
-> Get all rows from table B, where _idB is in datalist from table A, _idA = 5
I am still pretty new to SQLite and still learning, so thanks a lot for your help!
Assuming that by DataLoader you are referring to the CursorLoader, things are simple:
You should either:
Extent a cursor loader and in onLoadInBackground() method do the first table query, get the id and use it in the second table query. Make sure to return the Cursor and swap it in the ListView adapter.
In the first cursor loader onLoadFinished() callback, get the id and use it to start a new loader for the second table cursor.
Let me know if you need more help.
I am writing an application for managing students time table and I'm having problem with presentation. I want to have all items sorted by time they start and if there is next day there should be shown list separator with date.
#Override
public void bindView(View view, Context context, Cursor cursor) {
String day = cursor.getString(cursor.getColumnIndex("DAY"));
if(day.equals(lastDay)){
separator.setVisibility(View.GONE);
}else{
separator.setText(day + " " + dayOfWeek);
lastDay = day;
separator.setBackgroundColor(Color.parseColor("#0099CC"));
}
}
Unluckilly, I am facing 2 problems. 1st items are not sorted at all, maybe but 10 first, and 2nd it seems to me I have no control when each cell is drawn thats why algorithm with compering last day doesnt work at all and I have separator between every single entry, beside few first cells... Any ideas how can I solve this? (Btw. data is fetched from sqlite db)
Regards,
Robert
There is no sorting whatsoever in the code in your answer, so I cannot help you with the 1st issue.
BTW, if possible, you should sort your data on the SQLite level - it will be faster than any Java code. If it's not possible (for example your sorting criteria cannot be expressed as a sqlite query), you should sort the data before passing it to the adapter. In this case you should probably create your own BaseAdapter subclass.
The second issue is the expected behavior of CursorAdapter. The bindView are called on demand, so they will be called for the subsequent items when you're scrolling down, but when you start scrolling up, it will be called for the items appearing on the top of the screen. Read more about views recycling in Android adapters.
If you want to section your data I recommend creating an adapter with two view types - one for the section header, the other one for the actual item. Calculate the positions of added section items in swapCursor() call and call different code in newView/getView.
1st, you will need to sort your data by the day column. You need to do this when query the database and get the cursor. The sort string will look something like this:
"DAY ASC"
2nd, this is the really tricky thing. If you did this yourself you would have to remap the cursor positions and set different view types on getView like chalup said. I've made a library that does all of this for you though called SectionCursorAdapter. To implement your this for yourself extend SectionCursorAdapter and do the following.
#Override
protected Object getSectionFromCursor(Cursor cursor) {
int columnIndex = cursor.getColumnIndex("DAY");
String day = cursor.getString(columnIndex);
return day;
}
That will give you a new section for each day. Mattering on what you return you could make it by week or month as well.
i made a table layout with 4 columns and rows were added dynamically.
i want to sort columns in the table layout according to there respective data. first two column contains images and independent to each other and other two columns contain string data.
Can anyone help me on this????
Thanks in advance guys....
I would suggest you to use a listView for this purpose. Sort your data and update your list with notifiyDataSetChanged method.This way it will be easier and more efficient.
Edit: I assume you know how to sort arraylist.
Edit2: If you have to use a table then try to sort data(arraylist), after your sort, loop on your table's rows and update their properties/textViews with your sorted arrayList. You can use getChildAt(int index) method of tableView on your loop.
You can implement custom Comparator in your adapter. So after you get items you will sort them Collections.sort(items, new MyComparator());
public class MyComparator implements Comparator<Integer> {
#Override
public int compare(Integer lhs, Integer rhs) {
return lhs.compareTo(rhs);
}
}
Good day to all.
Is there some way to order the TableLayout content by a specific column?
I need to order the content according to user selection from a spinner.
Example if the user chooses to order the table by "name" the column name is arranged in ascending order.
The rows contain TextViews in which said content is saved, and the content is populated via a database.
Is there an easier way to order the content other than having to re-open a connection to the database?
Thanks.
The only way i can think of is to create a java object for your table row entries, putting all your objects into a list and comparing them using something like this:
Collections.sort(nodeList, new Comparator<DataNode>(){
public int compare(DataNode o1, DataNode o2){
if(o1.degree == o2.degree)
return 0;
return o1.degree < o2.degree ? -1 : 1;
}
});
It would be easier to re-open a connection to the database. You have to redraw your TableLayout in all cases.
What I want to do:
Display a list of items
The actual data is coming from an external source and can change
Each item can have several "columns". E.g. "type", "content", "date"
The list should e.g. be sortable by "type" and "date".
I thought I might be able to get the desired functionality using a ListView (but maybe also this is a very bad choice?). I have read some stuff about them, but still have some questions. I know that the ListView Displays data which is managed by an Adapter. If I have an ArrayAdapter, does the Listview always display the items in the order of the underlying Array in the Adapter? So to implement sorting, I would have to somehow change the Array in the Adapter? I have read, that you can use SimpleAdapter to have several properties in a row, but the data SimpleAdapter uses seems to be static. How can I achieve that?
Thanks in advance!
Yes, ListView will represent depending on how your ArrayList or Vector([]) is sorted. Now, if you want to have several properties for each row, I would recommend that you create an Object of your choice, for instance: class Person which takes name, age, address etc etc.
Then create a list which takes Person objects. Then in your getView of your ArrayAdapter that you will override, you can call Person person = getItem(position); and now you have a hold of that object in that specific row (position), and can do whatever you want with that Person object.
Create your own adapter, and in getView() return view that matches your position. For instance if you got data like this:
id name
0 ccc
1 bbb
2 aaa
then when you sort by id and listview wants row at position 1, you return 1 bbb. But if you sort by name ascending, and list wants row #2, then you return 0 ccc. Of course you need to maintain the "mapping", but that's rather trivial.