SHARE
TWEET

Untitled

a guest Jun 16th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private static final String TABLE_STUDENTS="CREATE TABLE STUDENTS(NAMES TEXT,
  2.                                            LASTNAME TEXT, ID TEXT, IMAGE IMAGEVIEW)";
  3.  
  4. //...Class constructor and methods OnCreate and onUpgrade
  5.  
  6. public void addStudents(String names, String lastname , String id, int img){
  7.     SQLiteDatabase bd = getWritableDatabase();
  8.     if(bd != null){
  9.         bd.execSQL("INSERT INTO STUDENTS VALUES('"+names+"','"+lastname+"','"+id+"','"+
  10.         img+"')");
  11.         bd.close();
  12.     }
  13. }
  14.  
  15. public List<Student> showStudents(){  
  16.     /*Student is the class where I defined the general attributes*/
  17.     SQLiteDatabase bd = getReadableDatabase();
  18.     final Cursor cursor = bd.rawQuery("SELECT * FROM STUDENTS",null);
  19.     final List<Student> students = new ArrayList<>();
  20.     if(cursor.moveToFirst()){
  21.         do{
  22.             students.add(new Student(cursor.getString(0),cursor.getString(1),
  23.             cursor.getString(2), cursor.getInt(3)));
  24.         }while (cursor.moveToNext());
  25.     }
  26.     return students;
  27. }
  28.      
  29. private RecyclerView recyclerViewStudents;
  30. private AdapterStudentsLists adapterStudentsList;
  31.  
  32. @Override
  33. protected void onCreate(Bundle savedInstanceState) {
  34.     super.onCreate(savedInstanceState);
  35.     setContentView(R.layout.activity_students_list);
  36.  
  37.     recyclerViewStudents = (RecyclerView) findViewById(R.id.recyclerStudents);
  38.     recyclerViewStuents.setLayoutManager(new LinearLayoutManager(this));
  39.  
  40.     final SQLiteDb sqLiteDb = new SQLiteDb(getApplicationContext());
  41.     recyclerViewStudents.setAdapter(adapterStudentsList);
  42.  
  43.     adapterStudentsList = new AdapterStudentsList(sqLiteDb.showStudents());
  44.     adapterStudentsList.setOnClickListener(new View.OnClickListener() {
  45.         @Override
  46.         public void onClick(View view) {
  47.             Toast.makeText(getApplicationContext(),"Id: "/*+ SELECTED STUDENT ID */,
  48.             Toast.LENGTH_SHORT).show();
  49.         }
  50.     });
  51.     recyclerViewStudents.setAdapter(adapterStudentsList);
  52. }
  53.      
  54. public static  class  ViewHolder extends RecyclerView.ViewHolder{
  55.        private TextView CompleteName;
  56.        ImageView ImageStudent;
  57.     public ViewHolder(View itemView) {
  58.         super(itemView);
  59.         NombreCompleto =(TextView)itemView.findViewById(R.id.tvNombreCompleto);
  60.         ImagenAlumno = (ImageView)itemView.findViewById(R.id.imgAlumno);
  61.     }
  62. }
  63.  
  64. public List<Alumno> alumnosLista;
  65.  
  66. public AdatadorListaAlumnos(List<Alumno>alumnosLista){
  67.     this.alumnosLista = alumnosLista;
  68. }
  69.  
  70. @Override
  71. public void onBindViewHolder (ViewHolder holder, final int position) {
  72.     holder.NombreCompleto.setText(alumnosLista.get(position).getNombres() + " " +
  73.       alumnosLista.get(position).getApellidos());
  74. holder.ImagenAlumno.setImageResource(alumnosLista.get(position).getImagen());
  75. }
  76.  
  77. private View.OnClickListener listener;
  78.  
  79. public void setOnClickListener(View.OnClickListener listener){
  80.     this.listener=listener;
  81. }
  82.  
  83. @Override
  84. public void onClick(View view) {
  85.     if(listener!=null) {
  86.         listener.onClick(view);
  87.     }
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top