如何使用SQLite保存图像,然后在RecycleView中使用图像

时间:2019-05-30 02:32:46

标签: android image sqlite android-recyclerview adapter

我在使用SQLite保存和显示图像时遇到问题,然后使用RecyclerView显示数据。我的意图是通过RadioButton获取用户信息,包括其类型,以最终显示数据和区分男人和女人的图像。尝试在没有图像(仅文本)的情况下一切正常,但是当我在图像中添加必要内容时,应用程序失败。

我用"/ * ??HERE * /"指出了我认为有问题的地方。

我真的希望你能帮助我。谢谢。

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);
            }
        });
    }
}

学生班

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
}

数据库助手类

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;
    }
}

还有适配器:-

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

1 个答案:

答案 0 :(得分:2)

考虑评论:-

  

是的,你是对的。该消息是:“ E / SQLiteLog:(1)表学生   有3列,但提供了4个值。”不假定   表中有4列:private final static String TABLE_ALUMNOS = "CREATE TABLE STUDENTS (NAMES TEXT, LAST NAME TEXT, ID TEXT, IMAGE TEXT)";

最可能的原因是,尽管您已更改架构(猜测时添加了IMAGE列),但 onCreate 仅在创建数据库时运行。数据库将保留(仅创建一次,数据将存储为应用程序的一部分)。

在开发应用程序时,如果数据消耗惨重,那么最快的解决方法是删除应用程序的数据或卸载应用程序(均删除数据库),然后重新运行应用程序。

随着 onUpgrade 方法删除该表并调用 onCreate ,您还有其他选择可以增加数据库版本号,例如将super(context, "db_students", null, 1);更改为super(context, "db_students", null, 2); (第四个参数是数据库版本)

关于单选按钮的选择,您可以使用类似:-

public class MainActivity extends AppCompatActivity {

    RadioButton rdbtnMale, rdbtnFemale;
    RadioGroup rgrpGender; //<<<<<<<<<<< ADDED
    Button btnRegister;
    int imgStudent; //<<<<<<<<<< Not needed

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rdbtnMale = this.findViewById(R.id.rdbtnMale);
        rdbtnFemale = this.findViewById(R.id.rdbtnFemale);
        btnRegister = this.findViewById(R.id.btnRegister);
        rgrpGender = this.findViewById(R.id.rgrpGender); //<<<<<<<<<< ADDED

        // ??HERE - NOTHING COMMENTED OUT
        /**
        if(rdbtnMale.isChecked()){
            imgStudent = R.drawable.boy;
        }else if(rdbtnFemale.isChecked()){
            imgStudent = R.drawable.girl;
        }
        **/
        //............ other code

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int studentGender;
                switch(rgrpGender.getCheckedRadioButtonId()) {
                    case R.id.rdbtnMale:
                        studentGender = 0;
                        break;
                    case R.id.rdbtnFemale:
                        studentGender = 1;
                        break;
                    default:
                        studentGender = -1;
                        break;
                }
                Log.d("SAVING","Student is gender " + String.valueOf(studentGender) + "\t(1 = Male 0= Female)"); //TODO Remove before publishing
                //<<<<<<<<<< COMMENTED OUT FOR DEMO >>>>>>>>>>
                //sqLiteDb.AddStudents(edtNames.getText().toString(),edtLastNames.getText().toString(), edtId.getText().toString(),studentGender);
            }
        });
    }
}
  • 以上允许其他性别的其他RadioButton只需添加适当的案例(s)

  • 以上内容可以单独运行并具有适当的布局。

从本质上讲,您并不是在保存图片,而是在显示性别。