【问题标题】:Want to combine redundant Methods of pulling user/patient images into one method想要将提取用户/患者图像的冗余方法合并为一种方法
【发布时间】:2015-07-14 17:32:13
【问题描述】:

我在 xml 布局中设置了九个 ImageView。我的应用程序直接从设备上的文件夹中提取图像并将图像加载到这些 ImageView 上。由于冗余,我的应用程序正在减慢我添加的更多患者/用户。我确信有办法将这些方法(做完全相同的事情)组合成一种方法,我只是不确定如何,我正在考虑创建一个文件数组。有任何想法吗?

这是我关于被声明和实例化的九个 ImageView 的代码

ImageView patientOne;
ImageView patientTwo;
ImageView patientThree; 
ImageView patientFour; 
ImageView patientFive; 
ImageView patientSix; 
ImageView patientSeven; 
ImageView patientEight; 
ImageView patientNine;

public void initializeViews() {
    patientOne=  (ImageView)findViewById(R.id.patient_one);
    patientTwo=  (ImageView)findViewById(R.id.patient_two);
    patientThree=  (ImageView)findViewById(R.id.patient_three);
    patientFour=  (ImageView)findViewById(R.id.patient_four);
    patientFive=  (ImageView)findViewById(R.id.patient_five);
    patientSix=  (ImageView)findViewById(R.id.patient_six);
    patientSeven=  (ImageView)findViewById(R.id.patient_seven);
    patientEight=  (ImageView)findViewById(R.id.patient_eight);
    patientNine=  (ImageView)findViewById(R.id.patient_nine);
    newPatient = (Button)findViewById(R.id.new_patient);
} 

这是我从特定对应目录中拉取患者/用户图像的九个冗余命令

public void getPatientOne() {
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient1/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patientOne.setImageURI(Uri.fromFile(imgFile));
    }
}

public void getPatientTwo() {
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient2/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patientTwo.setImageURI(Uri.fromFile(imgFile));
    }   
}

public void getPatientThree() {
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient3/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patientThree.setImageURI(Uri.fromFile(imgFile));
    }
}

public void getPatientFour() {
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient4/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patientFour.setImageURI(Uri.fromFile(imgFile));
    }
}

public void getPatientFive() {
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient5/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patientFive.setImageURI(Uri.fromFile(imgFile));
    }
}

public void getPatientSix() {
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient6/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patientSix.setImageURI(Uri.fromFile(imgFile));
    }
}
//And so on and so forth until I have nine Patients
}//end of class

这些是我的 onCreate 方法中调用的方法

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro);

    initializeViews(); 

    getPatientOne();
    getPatientTwo();
    getPatientThree();
    getPatientFour();
    getPatientFive();
    getPatientSix();
}

如果你好奇,这就是我创建患者文件夹的方式

//  Name of Folder = Patient's Number + randomInt
public void createPatientFolder() {

    count=count+1;
    StringBuilder sb = new StringBuilder();
    sb.append("/PerinatalMonitor/Patient");
    sb.append(count);
    testname= sb.toString();

    SharedPreferences pinfo = getSharedPreferences("pf",Context.MODE_PRIVATE);
    SharedPreferences.Editor editor= pinfo.edit();
    editor.clear();
    editor.putString("name", testname);
    editor.putBoolean("safe",false);
    editor.commit();

    patientFolder = new File(Environment.getExternalStorageDirectory() + testname);
    boolean success= true;

    if(patientFolder.exists()){
        Toast toast = Toast.makeText(this, "Already Exists", Toast.LENGTH_LONG);
        toast.show();

        createPatientFolder();
    }

    if(!patientFolder.exists()) {
        success = patientFolder.mkdirs();
        Toast toast = Toast.makeText(this, "pass", Toast.LENGTH_SHORT);
        toast.show();
    }

    if (success) {
        Toast toast = Toast.makeText(this, "pass", Toast.LENGTH_SHORT);
        toast.show();
    } 
    else {
        Toast toast = Toast.makeText(this, "fail", Toast.LENGTH_SHORT);
        toast.show();
    }
}

【问题讨论】:

  • 我强烈建议您使用RecyclerView + GridLayoutManager(3),这样您就可以比创建静态视图更容易地维护您的代码。 Aleady 你可以看到你有重复的代码。您应该创建一个 getPatientFour(final String path),以便您可以重复使用它。
  • Jared,非常感谢您的回复,我将尝试您的解决方案...是的,我绝对不想使用静态视图!如何在不预先定义它们的情况下将图像视图添加到布局中?以及如何定位这些图像视图并将患者的图像插入到相应的图像视图中?
  • A RecyclerView 类似于 Listview 。见这里:blog.sqisland.com/2014/12/recyclerview-grid-with-header.html.

标签: android android-layout android-imageview android-file


【解决方案1】:

我建议您使用 Imageview 数组。您的 getPatientXX() 方法的唯一区别是文件夹编号,即 Patient1、Patient2 等。

很容易有一个循环来遍历图像视图并设置图像路径。

   public void getPatients() {
for(int i=0;i<patient.length;i++)
{
    String path = Environment.getExternalStorageDirectory()+ "/PerinatalMonitor/Patient"+i+"/patient.jpg";
    File imgFile = new File(path);
    if(imgFile.exists())
    {
        patient[i].setImageURI(Uri.fromFile(imgFile));
    }
}
}

其中,患者是 imageview 数组。此方法将遍历整个 imageview 数组,从文件夹中获取图像。

【讨论】:

  • 感谢您对 Trinadh 的回复...您的方法很容易实现...但是我如何定位图像视图并插入相应的图像?也许我不应该有预定义的图像视图,有没有办法在我添加患者时插入图像视图?
  • 到目前为止,您可以在您的 initializeViews() 方法中定义 imageview 数组,例如“patient[1]= (ImageView)findViewById(R.id.patient_one);”对于所有图像视图。如果要在添加患者时添加图像视图,则需要动态分配图像视图数组,并且应在调用 createPatient() 之后调用 getPatient() 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多