简体   繁体   English

在自定义listview android的子视图中对值进行排序

[英]Sort values in child views of custom listview android

I designing a custom ListView ,which has more child view 我设计了一个自定义ListView ,它具有更多的子视图

I have ideas about sorting the ListView data in "Asc" or "Desc" order ,that retrieves data directly from database , but in my case I used CustomSimpleCursorAdapter , I requires to sort data in TextView depending upon the values that is: 我对以“ Asc”或“ Desc”顺序对ListView数据进行排序有想法,可以直接从数据库中检索数据,但在我的情况下,我使用CustomSimpleCursorAdapter,因此我需要根据以下值对TextView数据进行排序:

  1. today 今天
  2. tomorrow 明天
  3. more than 2 days ie; 超过2天,即; 354 354

CustomSimpleCursorAdapter .java CustomSimpleCursorAdapter .java

//Days remaining for BirthDay  

        String year=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_YEAR));
        String month=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_MONTH));  
        String date=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_DATE));
        String remainingDays=BirthdayCalculation.getDaysRemainingForNextBirthDay(year, month, date);

         Calendar today=Calendar.getInstance();
         int CMonth=(today.get(Calendar.MONDAY)+1);
         int CDate=(today.get(Calendar.DAY_OF_MONTH));


         //checking whether the BD is on TODAY

         if (remainingDays.equals("1") && (CDate==Integer.parseInt(date) && (CMonth)==Integer.parseInt(month))) {                        
             viewHolder.txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
             viewHolder.txtDaysRemainigValue.setTypeface(fontRoboto_Regular);
             viewHolder.txtDaysRemainigValue.setTextColor(Color.parseColor("#00CC33"));
             remainingDays="today";              
        }

         //checking whether the BD is on TOMORROW
         else if (remainingDays.equals("1")) {           
             viewHolder.txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
             viewHolder.txtDaysRemainigValue.setTextColor(Color.parseColor("#FF0099"));
             remainingDays="tomorrow";
             viewHolder.txtDaysRemainigValue.setTypeface(fontRoboto_Regular);
        }


       //checking how many days remaining BD
         else{
          remainingDays=BirthdayCalculation.getDaysRemainingForNextBirthDay(year, month, date);
          viewHolder.txtDaysRemainigValue.setTextSize(TypedValue.COMPLEX_UNIT_SP, 27);
          viewHolder.txtDaysRemainigValue.setTextColor(Color.parseColor("#990000"));

         }

Here's Screen Shot link 这是屏幕截图链接

If you get the data in database, you can use ASC or DESC and put ArrayList. 如果在数据库中获取数据,则可以使用ASC或DESC并将ArrayList放入。
another way is you can use Collections.sort(). 另一种方法是可以使用Collections.sort()。 but you must data class implements comparable and overriding compare method. 但是您必须数据类实现可比且覆盖的比较方法。

When you query your database, you should use an "order by" clause. 查询数据库时,应使用“ order by”子句。 For example, this method takes the order by clause as the last argument. 例如, 此方法将order by子句作为最后一个参数。 I don't know how you store your dates and times in your database, but if it's something SQLite can recognize and provide sorting on, then it should work. 我不知道如何将日期和时间存储在数据库中,但是如果SQLite可以识别并提供排序功能,那么它应该可以工作。 The following will query for all columns on a table named "table" with no where clause, no "group by" clause, no "having" clause, and order by the time column descending (use ASC for ascending if you want that instead): 下面的代码将查询名为“ table”的表上的所有列,其中不带where子句,不具有“ group by”子句,不具有“ having”子句,并且按时间列降序排列(如果需要,请使用ASC进行升序) :

database.query("table", null, null, null, null, null, "time DESC");

EDIT 编辑

If you can't store the exact data you want (in this case days remaining until an event), I can only see two options: 如果您无法存储所需的确切数据(在这种情况下,直到发生事件还剩下几天),我只能看到两个选项:

1). 1)。 After getting the cursor, you iterate over the results and compose a new sorted list. 获取光标后,您遍历结果并组成一个新的排序列表。 You could make some kind of model object in java, read the values into it from the cursor, and sort them with a comparator function. 您可以在Java中创建某种模型对象,从游标中读取值,然后使用比较器函数对它们进行排序。 At that point you probably would not use a CursorAdapter any more. 那时,您可能不再使用CursorAdapter It's quite easy to build your own ListAdapter - I recommend you watch The World of Listview 构建自己的ListAdapter非常容易-我建议您观看《 Listview的世界》

2). 2)。 Since the query methods take strings, you can actually compose more complicated queries so that SQLite provides you the data you DO want (and still sort it for you as well). 由于查询方法采用字符串,因此您实际上可以组成更复杂的查询,以便SQLite为您提供您想要的数据(并且仍然为您排序)。 If your times are stored as longs, you could do something like this: 如果您将时间存储为多长时间,则可以执行以下操作:

long currentTime = System.currentTimeMillis();
String timeAsString = Long.toString(currentTime);
String remainingTimeColumn = "(time_column - " + timeAsString + ") AS remaining_time";

// compose query
String table = "table";
String[] columns = new String[] {"column1", "column2", ..., "columnN", remainingTimeColumn};
String order = "remaining_time ASC";

// query
Cursor cursor = database.query(table, columns, null, null, null, null, order);

// later, get remaining time from cursor row
long remainingTime = cursor.getLong(cursor.getColumnIndex("remaining_time"));

In this case "time_column" is the name of the column that stores the event time. 在这种情况下, "time_column"是存储事件时间的列的名称。 What this is doing is creating an additional column with the name "remaining_time" , which is calculated from one of the actual column values. 这是在创建一个名为"remaining_time"的附加列,该列是根据实际列值之一计算得出的。 The results are then sorted by that synthesized column. 然后将结果按该合成列排序。 The cursor you get back will contain this column and these values, you just need to have the proper column name to access them. 您返回的游标将包含此列和这些值,您只需要具有正确的列名即可访问它们。

Since I don't know the details of your data format, I can't say this is exactly how your query should look, but the idea should be clear. 由于我不知道您的数据格式的详细信息,因此无法确切地说这就是查询的外观,但是思路应该很清楚。 You can work out the finer details from here... 您可以从这里计算出更详细的信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM