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);
}
}
Related
I have an arraylist that I am striving to rearrange when the view is loaded. Curious if there are built in methods or ways to sort a list of items that I retrieve let's say in alphabetical order. I currently retrieve the items in the list from the cursor from oldest in the database on top to newest on bottom. I also want to find ways to reverse that to get newest on top. Any advice on such things? Or is this too particular?
To sort your list,
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
and to reverse your list,
Collections.reverse(list);
You can simply reverse an ArrayList by
Collections.sort(arraylist, Collections.reverseOrder());
further you can sort w.r.t name, age, number etc by
Collections.sort(unsortedList,new Comparator<YourModel>() {
#Override
public int compare(YourModela, YourModelb) {
return a.getName().compareTo(b.getName());
}
});
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.
I want to display a single row from the database in a listview.
For example:
I have a database with three row and 4 columns.
I want to display the columns from row 2 as a listview (but i only want to display the columns that contains data).
I dont have any code, because i dont even know where to start.
I only used the simplecursoradapter in the past.
Sorry for my bad english. I searched a lot and didnt find any answer. Please dont be too rude if thats a stupid question.
Okay now collect your data in cursor just get all your data and store each row object in a ArrayList of the wrapperClass object like
Cursor c=null;
c=db.getAllRecords();
if(c.getCount()>0)
{
for(int j=0;j<c.getCount();j++)
{
c.moveToPosition(j);
employeewrapper cw=new employeewrapper();
cw.id=c.getLong(0);
cw.name=c.getString(1);
cw.phone=c.getString(2);
cw.web=c.getString(3);
cw.address=c.getString(4);
cw.des=c.getString(5);
cw.qual=c.getString(6);
cw.doj=c.getString(7);
cw.sal=c.getString(8);
if(cw.des.equalsIgnoreCase("Designer"))
{
designers.add(cw);
}
}
}
c.close();
db.close();
npw you can display any row you want create a new ArrayList of string to store names and pass that arraylist to the adapter in this way you can easily display your data and may be able to transfer it anywhere you want easily
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.