从另一个java类更新列表并将位图转换为图像

sqxo8psd  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(184)

这是我第一次用androidstudios制作应用程序,所以如果有什么明显的地方我会错过,这就是为什么!
我实现了一个java类(称为coursegridfragment),其中我使用cardview在网格中显示学生列表。在我的公共课上,我定义了我的学生名单如下: public List<StudentRecyclerViewItem> studentList; 在oncreate中,我有以下代码行: initializeStudentList(); 然后我像这样填充我的列表:

public void initializeStudentList()
    {
        if(studentList == null)
        {
            studentList = new ArrayList<>();
            studentList.add(new StudentRecyclerViewItem("Robert Downey Jr","Tony Stark",R.drawable.c1_robert_downey));
            studentList.add(new StudentRecyclerViewItem("Chris Evans", "Steve Rogers",R.drawable.c1_chris_evans));
            studentList.add(new StudentRecyclerViewItem("Chris Hemsworth","Thor", R.drawable.c1_chris_hemsworth));
            studentList.add(new StudentRecyclerViewItem("Mark Ruffalo","Bruce Banner", R.drawable.c1_mark_ruffalo));
            studentList.add(new StudentRecyclerViewItem("Jeremy Renner","Clint Barton", R.drawable.c1_jeremy_renner));
            studentList.add(new StudentRecyclerViewItem("Scarlett Johansson","Natasha Romanoff", R.drawable.c1_scarlett_johansson));
            studentList.add(new StudentRecyclerViewItem("Chadwick Boseman","T'Challa", R.drawable.c1_chadwick_boseman));
            studentList.add(new StudentRecyclerViewItem("Tom Holland","Peter Parker", R.drawable.c1_tom_holland));
        }
    }

从另一个名为detectoractivity.java的java类中,我想将项目添加到我原来的studentlist中。我的变量声明如下:

ImageView ivFace = dialogLayout.findViewById(org.tensorflow.lite.examples.detection.R.id.dlg_image);
    TextView tvTitle = dialogLayout.findViewById(org.tensorflow.lite.examples.detection.R.id.dlg_title);
    EditText etName = dialogLayout.findViewById(R.id.dlg_input);
    EditText etNumber = dialogLayout.findViewById(R.id.dlg_input2);

    tvTitle.setText("Add New Entry");
    ivFace.setImageBitmap(rec.getCrop());
    etName.setHint("Input name");
    etNumber.setHint("Input student number");

代码如下:

@Override
      public void onClick(DialogInterface dlg, int i) {

          String name = etName.getText().toString();
          String number = etNumber.getText().toString();
          int img = ivFace.getScreenshotBmp();
          if (name.isEmpty()) {
              return;
          }
          detector.register(name, rec);

          CoursesGridFragment.studentList.add(new StudentRecyclerViewItem(name,number,img));
          dlg.dismiss();
      }

我有两个问题。如何正确地将新项目添加到我的studentlist中,以及如何将位图转换为图像,以便将其作为int传递到studentlist中。
使用其他堆栈溢出线程,我尝试在detectoractivity.java文件中使用以下类将其转换为位图:

public Bitmap getScreenshotBmp() {

    Bitmap screenshot;
    FileOutputStream fileOutputStream = null;

    File path = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    String uniqueID = UUID.randomUUID().toString();

    File file = new File(path, uniqueID + ".jpg");
    try {
      fileOutputStream = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    screenshot.compress(Bitmap.CompressFormat.JPEG, 30, fileOutputStream);

    try {
      fileOutputStream.flush();
      fileOutputStream.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return screenshot;
  }

但这似乎不起作用。我的问题有以下两个错误:
无法解析“imageview”中的方法“getscreenshotbmp”(用于转换我的图像)
不能从静态上下文引用非静态字段“studentlist”
任何帮助/指导都会非常有用!谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题