I have a problem saving and displaying an image using SQLite
,then display the data with a RecyclerView
. My intention is to get the information of the user, within it its genre through RadioButton
, to finally show the data and an image distinguishing between man and woman. All was OK trying to do it without image, just text, but when I added the necessary to the image, the app failed.
I pointed with "/ * ??HERE * /"
where I suppose there is a problem.
I really hope you can help me. Thanks.
public class MainActivity extends AppCompatActivity {
EditText edtNames, edtLastNames, edtId;
Button btnRegister, btnShow;
RadioButton rdbtnMale, rdbtnFemale;
int imgStudent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtNames= (EditText) findViewById(R.id.edtNames);
edtLastNames= (EditText) findViewById(R.id.edtLastNames);
//...etc, etc ->linkinkgs
final SQLiteDb sqLiteDb =new SQLiteDb(getApplicationContext());
// ??HERE
if(rdbtnMale.isChecked()){
imgStudent = R.drawable.boy;
}else if(rdbtnFemale.isChecked()){
imgStudent = R.drawable.girl;
}
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqLiteDb.AddStudents(edtNames.getText().toString(),
edtLastNames.getText().toString(), edtId.getText().toString(),imgStudent /* ??HERE*/);
Toast.makeText(getApplicationContext(),"Was added correctly",
Toast.LENGTH_SHORT).show();
}
});
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent showStudents = new Intent(getApplicationContext(),StudentsActivity.class);
startActivity(showStudents);
}
});
}
}
The Student class
public class Student {
private String Names, LastNames, Id;
private int Image;
public Student(String names, String lastnames, String id, int imageStudent) {
//...parameters
}
// + Getters and Setters
}
The Database Helper class
public class SQLiteDb extends SQLiteOpenHelper {
private static final String TABLE_STUDENTS = "CREATE TABLE STUDENTS(NAMES TEXT,
LASTNAMES TEXT,ID TEXT, IMAGEN INT)";
public SQLiteDb(Context context) {
super(context, "db_students", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(TABLE_STUDENTS);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS TABLE_STUDENTS");
onCreate(sqLiteDatabase);
}
public void AddStudents(String names, String lastnames, String id,/*??HERE*/ int img) {
SQLiteDatabase bd = getWritableDatabase();
if (bd != null) {
bd.execSQL("INSERT INTO STUDENTS VALUES('" + names + "','"
+ lastnames + "','" + id + "','" + img + "')");
bd.close();
}
}
public List<Student> ShowStudents() {
SQLiteDatabase bd = getReadableDatabase();
Cursor cursor = bd.rawQuery("SELECT * FROM STUDENTS", null);
List<Student> students = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
students.add(new Student(cursor.getString(0), cursor.getString(1)
, cursor.getString(2),/*??HERE*/ cursor.getInt(3)));
} while (cursor.moveToNext());
}
return students;
}
}
And the adapter :-
public class AdapterStudentList extends RecyclerView.Adapter<AdapterStudentList.ViewHolder> {
public static class ViewHolder extends RecyclerView.ViewHolder{
private TextView CompleteName;
ImageView ImageStudent;
public ViewHolder(View itemView) {
super(itemView);
CompleteName =(TextView)itemView.findViewById(R.id.tvCompleteName);
ImageStudent = (ImageView)itemView.findViewById(R.id.imgStudent);
}
}
public List<Student> studentsList;
public AdapterStudentList(List<Student>studentsList){
this.studentsList = studentsList;
}
//@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student,parent,false);
ViewHolder viewHolder= new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.CompleteName.setText(studentsList.get(position).getNames()+" "+ studentsList.get(position).getLastNames());
holder.ImageStudent.setImageResource(studentsList.get(position).getImagen());
}
@Override