I have my own adapter for ListView inherited from SimpleCursorAdapter . In each item, I have 2 TextView and CheckBox . Checkbox states are loaded from the database. It is necessary to implement the following: first, in the list only items with the unchecked checkboxes are displayed (in the 'complete' column in the database it is 0 ). And after them - items with checked checkboxes (in the column is 1 ).

What I've done

  1. At first I showed laziness and decided to make two lists. Everything is fine, only there were problems with the scrolling. And in general, it is a crutch. Somehow not good at it.

  2. I decided to filter the boolean array with checkbox states (first false , then true ). Happened. But between checkboxes and text in the item, and in general there was no correspondence with the record id. Since I changed the order of checkboxes, and did not touch the rest.

  3. I decided to rewrite the adapter altogether, the idea went well, but after compiling and running the application crashed with a billion errors that I could not figure out. After all these rakes, I could not stand it and decided to ask you, the people.

Adapter code

I don’t remember how I did everything, so I returned the code to its original position. Where the list is displayed as is .

 public class ListCursorAdapter extends SimpleCursorAdapter { private int layout; public ListCursorAdapter(Context _context, int _layout, Cursor _cursor, String[] _from, int[] _to) { super(_context, _layout, _cursor, _from, _to); layout = _layout; } @Override public void bindView(View view, Context _context, Cursor _cursor) { String list_name = _cursor.getString(_cursor.getColumnIndex(DatabaseHelper.LIST_NAME)); String list_data = _cursor.getString(_cursor.getColumnIndex(DatabaseHelper.LIST_TIME)); int chb_list = _cursor.getInt(_cursor.getColumnIndex(DatabaseHelper.LIST_COMPLETE)); TextView name_list_tv = (TextView)view.findViewById(R.id.lv_list_name); TextView data_list_tv = (TextView)view.findViewById(R.id.lv_list_data); CheckBox chb_lists = (CheckBox) view.findViewById(R.id.chb_list_complete); chb_lists.setClickable(false); chb_lists.setFocusable(false); name_list_tv.setText(list_name); data_list_tv.setText(list_data); chb_lists.setChecked(chb_list==1); } @Override public View newView(Context _context, Cursor _cursor, ViewGroup _parent) { LayoutInflater inflater = (LayoutInflater)_context.getSystemService(_context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layout, _parent, false); return view; } } 

What I need:

In ListView now the data from the database are displayed as is, in order. I need to change this order: first go items with data from the base rows with zeros in the 'complete' column. Then in the list should be displayed items with data from the rows of the database with units. In other words, first I need to show items with state checkboxes false , and then with true . And not like now - the data from the database is simply taken and in turn placed in the item. You need to turn down

How to solve this problem?

  • You can either initially make a request to the database specifying the data in the order of the order (in your case, by the boolean column), or abandon the cursor altogether, make your own data model (the class representing the record in the database) put information from the database into it and have a list of these classes sort by means of java. - Yuriy SPb
  • in the query method, through which a query is made to the database, there is an orderBy parameter (the seventh parameter), which sorts the cursor according to your wishes / And you need to be as I told you - to read books and not to invent unreal crutches, to which waste time. - pavlofff
  • @pavlofff, books? What kind? No money to buy them. And in the internet - the junkies' old junkies - Flippy
  • @Yuriy SPb, did not understand anything .. - Flippy
  • one
    I advised you to book already. After reading them you will become better than you will not read them. At least wild crutches will have to stop coming to your head. Find both of these books in the internet is not a problem at all. - pavlofff

1 answer 1

The adapter has nothing to do with solving this problem. The purpose of the adapter is to display the provided data, and not to sort, rearrange and replace something in it.

In order to get the result you want, you must issue a corresponding query to the database so that the data (cursor) transmitted to the adapter gets there in the correct form. To do this, in the query() method, with which you send a query to the database, there is an orderBy parameter - sort the result obtained in the selection. Since the query to the database itself is not in question, I will give an abstract example:

 Cursor cursor = BDHelper.query("mytable", null, null, null, null, null, "complete"); 

where complete is the name of the column in the database to sort by.

You can add the SQL service word DESC to the column name in ascending order, but this order is used by default and can be omitted:

 Cursor cursor = BDHelper.query("mytable", null, null, null, null, null, "complete DESC"); 

To sort in descending order, you should add the SQL service word - ASC

 Cursor cursor = BDHelper.query("mytable", null, null, null, null, null, "complete ASC"); 

read about it

PS: the query() method has several overloads and in the simplest version takes the following arguments:

 query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) 

that is, in order: table name, column names in the result set, selection condition, arguments for the selection condition, grouping, filter for grouped, sorting

  • thank!! Did not even look towards query () ... - Flippy