使用数据库中的时间作为字符串变量按升序对 ListView 上的数据进行排序?

Posted

技术标签:

【中文标题】使用数据库中的时间作为字符串变量按升序对 ListView 上的数据进行排序?【英文标题】:Sort data on ListView using time from database as a String variable in ascending order? 【发布时间】:2015-12-30 01:46:56 【问题描述】:

我有一个 ListView、光标适配器和一个 sqlite 数据库。我从数据库接收数据,它显示在 ListView 上。我想按升序对代码中值“beginTime”的数据进行排序。 'beginTime' 作为字符串从数据库中检索出来。我该怎么做?

周一课

public class Monday extends Fragment 
FloatingActionButton fab;
TimePickerDialog.OnTimeSetListener onTimeSetListener;
SimpleCursorAdapter adapter;
Database db;
String timeEnd;
String timeBegin;
String lEnd;
String lBegin;
EditText lName;
String day;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 

    final View v = inflater.inflate(R.layout.activity_monday, container, false);
   db = new Database(getActivity());
    String DAY = "Monday";
     Cursor cursor= db.getDayLessons(DAY);


    ListView listView = (ListView) v.findViewById(android.R.id.list);

    CustomAdapter adapter = new CustomAdapter(getActivity(),cursor);

    listView.setAdapter(adapter);
    fab = (FloatingActionButton) v.findViewById(R.id.fab);

    fab.attachToListView(listView);
    listView.setOnScrollListener(new AbsListView.OnScrollListener() 
                                     @Override
                                     public void onScrollStateChanged(AbsListView absListView, int i) 

                                     

                                     @Override
                                     public void onScroll(AbsListView absListView, int i, int i1, int i2) 

                                     
                                 

    );

   /* listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) 

        
    );*/
    fab.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Intent x = new Intent(getActivity(), LessonDetails.class);
            x.putExtra("day","Monday");
            x.putExtra("Tab","0");
            x.putExtra("weekDay","2");
            startActivity(x);
            getActivity();
        
    );

    return v;

      
   

自定义适配器类

public class CustomAdapter extends CursorAdapter 

public CustomAdapter(Context context, Cursor c) 
    super(context, c);




@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) 

    return LayoutInflater.from(context).inflate(R.layout.lesson_list, parent, false);


@Override
public void bindView(View view, final Context context, final Cursor cursor) 

    final TextView tvName = (TextView) view.findViewById(R.id.tvName);
    TextView tvTime = (TextView) view.findViewById(R.id.tvTime);
    TextView tvVenue = (TextView) view.findViewById(R.id.tvVenue);

    ImageButton delete = (ImageButton) view.findViewById(R.id.buttonDelete);
    ImageButton edit = (ImageButton) view.findViewById(R.id.buttonEdit);
    final String day = cursor.getString(cursor.getColumnIndexOrThrow(Database.DAY_COLUMN));

    final int _id = cursor.getInt(cursor.getColumnIndexOrThrow(Database.ID_COLUMN));
    final String id = String.valueOf(_id);
    final int intent = cursor.getInt(cursor.getColumnIndexOrThrow(Database.INTENT_COLUMN));
    final String name = cursor.getString(cursor.getColumnIndexOrThrow(Database.NAME_COLUMN));
    String time = cursor.getString(cursor.getColumnIndexOrThrow(Database.BEGIN_TIME_COLUMN)) + "-" + cursor.getString(cursor.getColumnIndexOrThrow(Database.END_TIME_COLUMN)) + "h";
    String venue = cursor.getString(cursor.getColumnIndexOrThrow(Database.VENUE_COLUMN));


    tvName.setText(name);
    tvTime.setText(time);
    tvVenue.setText(venue);


    delete.setTag(cursor.getPosition());
    delete.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Database db = new Database(context);
            db.deleteLesson(_id);
            cancelPre(context, intent);
            cancelActual(context, intent);
            cursor.requery();
            notifyDataSetChanged();


        
    );
    edit.setTag(cursor.getPosition());
    edit.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Intent p = new Intent(context, EditLessons.class);
            p.putExtra("Row_selected", id);
            context.startActivity(p);


        
    );


public static Comparator<Integer> ascending = new Comparator<Integer>() 
    @Override
    public int compare(Integer integer, Integer t1) 
        return t1.compareTo(integer);
    


;

public void cancelActual(Context context, int rq) 

    Intent intent = new Intent(context, Lessons.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, rq, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);




public void cancelPre(Context context, int rq) 
    Intent intent = new Intent(context, Lessons.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, rq, intent, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
  

Sqlite 数据库类

public class Database extends SQLiteOpenHelper 
public static final String DATABASE_NAME = "Student.db";
public static final String LESSON_TABLE = "LESSONS";
public static final String DAY_COLUMN= "Day";
public static final String ID_COLUMN= "_id";
public static final String VENUE_COLUMN = "Venue";
public static final String INTENT_COLUMN = "INTENT";
public static final String NAME_COLUMN = "Name";

public static final String BEGIN_TIME_COLUMN = "BeginTime";
public static final String END_TIME_COLUMN = "EndTime";
public static final int VERSION = 1;


public Database(Context context) 
    super(context, DATABASE_NAME, null, VERSION);


@Override
public void onCreate(SQLiteDatabase db) 
  String query = "create table " + LESSON_TABLE + "(" + ID_COLUMN + " integer primary key autoincrement," + DAY_COLUMN + " varchar," + NAME_COLUMN + " varchar," + BEGIN_TIME_COLUMN + " varchar," + END_TIME_COLUMN + " varchar," + VENUE_COLUMN + " varchar," + INTENT_COLUMN + " integer" + ")";
    db.execSQL(query);


@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) 
    String drop = "drop table if exists " + LESSON_TABLE;
    db.execSQL(drop);
   onCreate(db);



public void addLesson(Lesson lesson)throws SQLiteException

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(NAME_COLUMN,lesson.getName());
    values.put(DAY_COLUMN,lesson.getDay());
    values.put(BEGIN_TIME_COLUMN,lesson.getBeginTime());
    values.put(END_TIME_COLUMN,lesson.getEndTime());
    values.put(VENUE_COLUMN, lesson.getVenue());
    values.put(INTENT_COLUMN, lesson.getIntent());
    db.insert(LESSON_TABLE, null, values);
    //db.close();


 public Cursor getDayLessons(String day) throws SQLiteException 
     SQLiteDatabase db = this.getReadableDatabase();

     Cursor mcursor = db.rawQuery("select * from " + LESSON_TABLE + " where " + DAY_COLUMN + "='" + day + "'",);
     if (mcursor != null) 
         mcursor.moveToFirst();

     

         return mcursor;
     


public void updateLesson(String name, String bTime, String eTime, String venue, int _id)

     SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(NAME_COLUMN,name);
    values.put(BEGIN_TIME_COLUMN,bTime);
    values.put(END_TIME_COLUMN,eTime);
    values.put(VENUE_COLUMN,venue);

    db.update(LESSON_TABLE, values, ID_COLUMN + "='" + _id + "'", null);
   // db.close();


public void deleteLesson(int id)

        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(LESSON_TABLE,ID_COLUMN + "='" + id + "'",null);
   // db.close();



public Cursor getLesson(int _id)

       SQLiteDatabase db = this.getReadableDatabase();

    Cursor mcursor = db.rawQuery("select * from " + LESSON_TABLE + " where " + ID_COLUMN + "='" + _id + "'", null);
    if (mcursor!=null)
    
        mcursor.moveToFirst();
    
    return mcursor;
    

【问题讨论】:

【参考方案1】:

您必须使用 db 查询以所需的顺序检索数据。

SELECT * FROM table_name ORDER BY begintime ASC;

开始时间应该以毫秒为单位或iso format。

【讨论】:

以上是关于使用数据库中的时间作为字符串变量按升序对 ListView 上的数据进行排序?的主要内容,如果未能解决你的问题,请参考以下文章

集合List根据多个字段进行排序

alpha 对具有相同整数值且应按升序排列的列表进行排序[重复]

按 J 中的长度对字符串中的单词进行排序

如何按升序对 x 轴进行排序?

如何在 Reactjs 中按升序或降序对数据进行排序?

如何根据信号强度按升序对 getScanResults() 列表进行排序?