是的,所以我的问题是我有18个微调器包含43个选项供用户选择。我希望每个选项都能更改不同页面上的链接,以便用户查看有关该选项的pdf。我有共享首选项来分享选择,我现在正在实现我的if / else if语句,但如果我使用if / else语句,我将为每个微调器最终得到43,然后每个微调器为43最后我需要实现700多条if语句,并且必须有一种更简单的方法来实现我不知道的想法。这是一个代码示例:
if (spinnerValue == 1)
{
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>";
textView.setText(Html.fromHtml(text));
}
else if (spinnerValue == 2){
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>";
textView.setText(Html.fromHtml(text));
}
else if (spinnerValue == 3){
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-3-Information-Systems.pdf'>Unit 3: Information Systems</a>";
textView.setText(Html.fromHtml(text));
}
所以在这结束时,将有43个if(spinnervalue == 1到43)和18个微调器值。我是java和android工作室的新手,所以任何帮助或指导或许对此进行解释都非常感激。
编辑:我的if语句可以工作,但也可以切换,但我仍然需要实现43次if / switch语句18次。有什么办法我可以做一些事情 - 如果(spinnervalue 1到18 == 1) - 那么这将允许我写一次43个案例并让它适用于所有微调器值(1 - 18)
答案 0 :(得分:2)
以下是我对此的第一个想法:
String[] fileNames = new String[] {
//Put your filenames here in order
}
然后在你当前的代码中:
if(spinnerValue<=fileNames.length) { //Make sure the value is valid
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/"+fileNames[spinnerValue-1]+".pdf'>"+fileNames[spinnerValue-1].replaceAll("-"," ")+"</a>";
textView.setText(Html.fromHtml(text));
}
不确定这是否是最有效的方式,但请尝试考虑如何动态设置html。
答案 1 :(得分:2)
由于唯一的区别是字符串,并且您使用的是连续值1-43,因此最简单的是文本数组。
请记住,数组是从零开始的。
private static final String[] SPINNER_TEXTS = {
/*1*/"<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>",
/*2*/"<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>",
/*3*/"<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-3-Information-Systems.pdf'>Unit 3: Information Systems</a>"
};
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(SPINNER_TEXTS[spinnerValue - 1]));
答案 2 :(得分:1)
很多都可以重构 - 例如
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "";
switch(spinnerValue) {
case 1:
text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>";
break;
case 2:
text = "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>";
break;
}
textView.setText(Html.fromHtml(text));
但我并不相信这是最好的方法。我可能会创建一个地图(或一个数组,因为它只是键的数值)的微调值 - &gt; URL并使用它来填充链接。需要更少的代码行,并且将来可以扩展更多!
答案 3 :(得分:0)
您可以创建这样的映射表:
Map<Integer, String> spinnerValueToLinkMapping = new HashMap<Integer, String> {{
put(1, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-1-Communication-and-Employability-Skills-for-IT.pdf'>Unit 1: Communication and Employability Skills for IT</a>");
put(2, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-2-Computer-Systems.pdf'>Unit 2: Computer Systems</a>");
put(3, "<a href='http://qualifications.pearson.com/content/dam/pdf/BTEC-Nationals/Information-Technology/2010/Specification/Unit-3-Information-Systems.pdf'>Unit 3: Information Systems</a>");
}}
然后使用以下代码检索所需的链接:
TextView textView =(TextView)findViewById(R.id.textViewWeb1);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = spinnerValueToLinkMapping.get(spinnerValue);
textView.setText(Html.fromHtml(text));
有了这个,你最终会得到易于支持的映射表和DRY代码