如何将链接图像放到sqlite并在recyclerview中显示

slmsl1lt  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(278)

这是我的小项目,我可以把名字、地址和电话放到sqlite中并显示出来。然后我想将链接图像填充到框中,并在recyclerview中显示一个图像。请帮帮我!
以下是我的应用程序和代码:
recyclerview显示,无img
我想填充这个盒子的链接图片。。。img就会出现!
主要活动:

public class MainActivity extends AppCompatActivity {
    private StudentAdapter studentAdapter;
    private ArrayList<Student> studentList;
    private StudentDatabase studentDatabase;
    private FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        floatingActionButton = findViewById(R.id.floatingActionButton);
        final RecyclerView recyclerView = findViewById(R.id.recycle);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        studentAdapter = new StudentAdapter();
        studentDatabase = new StudentDatabase(this);
        studentList = new ArrayList<>();
        studentList = studentDatabase.getAll();
        recyclerView.setAdapter(studentAdapter);
        studentAdapter.setData(studentList);
        Student student = new Student();
        recyclerView.smoothScrollToPosition(studentList.size());

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CreateStudent.class);
                Student student = new Student();
                intent.putExtra("data", student);
                startActivityForResult(intent, 800);
            }
        });

        studentAdapter.setOnClickEvent(new StudentAdapter.OnClickEvent() {
            @Override
            public void OnItemClick(int position) {
                Student student = studentList.get(position);
                Intent intent = new Intent(MainActivity.this, ModifyStudent.class);
                intent.putExtra("data", student);
                startActivityForResult(intent, 70);
            }

            @Override
            public void OnDelete(int position) {
                Student student = studentList.get(position);
                studentDatabase.delete(student.getId());
                studentList.remove(position);
                studentList = studentDatabase.getAll();
                studentAdapter.setData(studentList);

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 70 && resultCode == RESULT_OK) {
            Student student = (Student) data.getSerializableExtra("returnData");
            studentDatabase.update(student);
            studentList = studentDatabase.getAll();
            studentAdapter.setData(studentList);
        }
        if (requestCode == 800 && resultCode == RESULT_OK) {
            studentList = studentDatabase.getAll();
            studentAdapter.setData(studentList);
        }
    }
}

学生.java:

public class Student implements Serializable {
    private String img;
    private int id;
    private String name;
    private String address;
    private String phone;

    public Student(String img, int id, String name, String address, String phone) {
        this.img = img;
        this.id = id;
        this.name = name;
        this.address = address;
        this.phone = phone;
    }
    public Student(){

    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

createstudent.java:

public class CreateStudent extends AppCompatActivity {
    private EditText createName, createAddress, creatPhone, createImage;
    private StudentDatabase studentDatabase;
    private Button btnCreate;
    private Button btnCancle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_student2);
        createName = findViewById(R.id.create_name);
        createAddress = findViewById(R.id.create_address);
        creatPhone = findViewById(R.id.create_phone);
        btnCreate = findViewById(R.id.btn_create);
        btnCancle = findViewById(R.id.btn_cancle);
        createImage = findViewById(R.id.create_img);

        final Student student = new Student();
        if (student != null) {
            createName.setText(student.getName());
            createAddress.setText(student.getAddress());
            creatPhone.setText(student.getPhone());
        }
        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String cname = createName.getText().toString();
                String caddress = createAddress.getText().toString();
                String cphone = creatPhone.getText().toString();
                String cimage = createImage.getText().toString();
                if (cname.trim().equals("") || caddress.trim().equals("") || cphone.trim().equals("")) {
                    btnCreate.setEnabled(false);
                } else {
                    student.setName(cname);
                    student.setAddress(caddress);
                    student.setPhone(cphone);
                    student.setImg(cimage);
                    studentDatabase = new StudentDatabase(CreateStudent.this);
                    studentDatabase.addStudent(student);
                    Intent intent = new Intent();
                    intent.putExtra("createData", student);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

studentdatabase.java文件:

public class StudentDatabase extends SQLiteOpenHelper {

    public StudentDatabase(@Nullable Context context) {
        super(context, "STUDENT_DATABASE", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE STUDENT(ID INTEGER PRIMARY KEY, NAME TEXT, ADDRESS TEXT, PHONE INTEGER)";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS STUDENT");
        onCreate(db);
    }

    public void addStudent(Student student) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("NAME", student.getName());
        cv.put("ADDRESS", student.getAddress());
        cv.put("PHONE", student.getPhone());

        db.insert("STUDENT", null, cv);
        db.close();
    }

    public void delete(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete("STUDENT", " ID = " + id, null);
        db.close();
    }

    public ArrayList<Student> getAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM STUDENT";
        Cursor cursor = db.rawQuery(query, null);
        ArrayList<Student> studentList = new ArrayList<>();
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                String address = cursor.getString(2);
                String phone = cursor.getString(3);
                Student student = new Student();
                student.setName(name);
                student.setId(id);
                student.setAddress(address);
                student.setPhone(phone);
                studentList.add(student);
            } while (cursor.moveToNext());
        }
        db.close();
        return studentList;
    }

    public void update(Student student) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("NAME", student.getName());
        cv.put("ADDRESS", student.getAddress());
        cv.put("PHONE", student.getPhone());
        db.update("STUDENT", cv, "ID = " + student.getId(), null);

    }

}

studentadapter.java文件:

public class StudentAdapter<extend> extends RecyclerView.Adapter<StudentAdapter.MyViewHolder> {
    private ArrayList<Student> studentList;
    private OnClickEvent onClickEvent;

    public void setOnClickEvent(OnClickEvent onClickEvent) {
        this.onClickEvent = onClickEvent;
    }

    public StudentAdapter() {
    }

    public void setData(ArrayList<Student> listData) {
        if (studentList == null) {
            studentList = new ArrayList<>();
        }
        studentList.clear();
        studentList.addAll(listData);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public StudentAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StudentAdapter.MyViewHolder holder, final int position) {
        Student student = studentList.get(position);
        Glide.with(holder.imageView.getContext()).load(studentList.get(position).getImg()).into(holder.imageView);
        holder.name.setText(student.getName());
        holder.address.setText(student.getAddress());
        holder.phone.setText(student.getPhone());

        holder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickEvent != null) {
                    onClickEvent.OnDelete(position);
                }
            }
        });
        holder.btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickEvent != null) {
                    onClickEvent.OnItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView name, address, phone;
        private Button btn_delete, btn_edit;
        private ImageView imageView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.img);
            name = itemView.findViewById(R.id.name);
            address = itemView.findViewById(R.id.address);
            phone = itemView.findViewById(R.id.phone);
            btn_delete = itemView.findViewById(R.id.btn_delete);
            btn_edit = itemView.findViewById(R.id.btn_edit);
        }
    }

    public interface OnClickEvent {
        void OnItemClick(int position);

        void OnDelete(int position);
    }
}
zqry0prt

zqry0prt1#

虽然您已经用glide库正确地定义了recyclerview来显示图片,但是您没有在数据库中存储(保存)图片信息。

public void addStudent(Student student) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("NAME", student.getName());
        cv.put("ADDRESS", student.getAddress());
        cv.put("PHONE", student.getPhone());

        db.insert("STUDENT", null, cv);
        db.close();
    }

你应该吃点

cv.put("PICTURE", student.getImg());

你也应该为你的孩子做同样的事 public void update(Student student) 存储(保存)用户图片的方法。还有你的 public void addStudent(Student student) 方法定义。你也应该在你的电脑里检索你的图片信息`

public ArrayList<Student> getAll() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM STUDENT";
        Cursor cursor = db.rawQuery(query, null);
        ArrayList<Student> studentList = new ArrayList<>();
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                String address = cursor.getString(2);
                String phone = cursor.getString(3);
                Student student = new Student();
                student.setName(name);
                student.setId(id);
                student.setAddress(address);
                student.setPhone(phone);
                studentList.add(student);
            } while (cursor.moveToNext());
        }
        db.close();
        return studentList;
    }

像这样:

String picture = cursor.getString(4);

student.setImg(picture);

图片信息应该是图片位置的路径,可能是url或手机磁盘中的文件位置。
............... 更新:

String query = "CREATE TABLE STUDENT(ID INTEGER PRIMARY KEY, NAME TEXT, ADDRESS TEXT, PHONE INTEGER, PICTURE TEXT)";

相关问题