【发布时间】: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