I have a ListView, cursor adapter and a sqlite database. I receive data from the database and it displays on the ListView. I would like to sort the data from the value 'beginTime' in my code in ascending order. 'beginTime' is retrieved as a String from the database.How can I go about it?
Monday Class
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;
}
}
Custom Adapter Class
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 Database Class
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; }
}
Aucun commentaire:
Enregistrer un commentaire