我正在开发一个显示听力测试结果的Android应用程序。它是在不久前制作的,所以很多代码都被折旧了。旧代码使用TabActivity
,效果很好:
public class TabbedResultActivity extends TabActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabbed_results);
setTabs();
}
private void setTabs() {
mTabHost = getTabHost();
addTab(R.string.Left_ear, false);
addTab(R.string.Right_ear, true);
}
private void addTab(int labelId, boolean aRight) {
Intent intent = createIntent(aRight);
TabHost.TabSpec spec = mTabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
TextView title = (TextView) tabIndicator.findViewById(R.id.title);
title.setTextSize(14.0f);
title.setText(labelId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
mTabHost.addTab(spec);
}
private Intent createIntent( boolean aRight) {
Intent newIntent = new Intent(getIntent());
newIntent.putExtra(ResultActivity.IS_RIGHT_EAR, aRight);
newIntent.setClass(this, ResultActivity.class);
return newIntent;
}
但问题是,我必须在新设备上为菜单添加操作栏。为了使应用程序在所有设备上兼容,我使用了ActionBarSherlock
库。这很棒,但我无法弄清楚如何将ResultsActivity
添加到片段中,因为我需要ResultsActivity
从SherlockFragmentActivity
扩展,但要将其集成到片段中,我需要扩展SherlockFragment
类。如果我这样做,我会收到很多错误,因为它不会从活动中延伸出来。这是我原来的ResultsActivity
课程:
public class ResultActivity extends SherlockFragmentActivity {
public static final String IS_RIGHT_EAR = "is_right_ear";
private ArrayList<EarSrt> leftAnswerList;
private ArrayList<EarSrt> rightAnswerList;
private boolean isRightEarTab = true;
private boolean bothEarsBad;
private boolean leftEarBad;
private boolean rightEarBad;
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results_layout);
Intent intent = getIntent();
int rowId = intent.getIntExtra(ResultsDatabase._ID, -1);
if ( rowId != -1 ) {
ResultsDatabase db = new ResultsDatabase(this);
String select = "(" + ResultsDatabase._ID + " == " + rowId + ")";
Cursor c = db.getReadableDatabase().query(ResultsDatabase.TABLE_NAME, null, select, null, null, null,null);
if ( c.moveToFirst() ) {
int leftEarColumn = c.getColumnIndex(ResultsDatabase.LEFT_EAR);
byte[] leftEarByteArray = c.getBlob(leftEarColumn);
int rightEarColumn = c.getColumnIndex(ResultsDatabase.RIGHT_EAR);
byte[] rightEarByteArray = c.getBlob(rightEarColumn);
leftAnswerList = (ArrayList<EarSrt>) ResultsDatabase.deserializeObject(leftEarByteArray);
rightAnswerList = (ArrayList<EarSrt>) ResultsDatabase.deserializeObject(rightEarByteArray);
}
} else {
byte[] leftEarByteArray = getIntent().getByteArrayExtra(ResultsDatabase.LEFT_EAR);;
byte[] rightEarByteArray = getIntent().getByteArrayExtra(ResultsDatabase.RIGHT_EAR);
leftAnswerList = (ArrayList<EarSrt>) ResultsDatabase.deserializeObject(leftEarByteArray);
rightAnswerList = (ArrayList<EarSrt>) ResultsDatabase.deserializeObject(rightEarByteArray);
}
isRightEarTab = getIntent().getBooleanExtra(IS_RIGHT_EAR, true);
GraphView graphView = (GraphView)findViewById(R.id.graphview);
if ( isRightEarTab ) {
graphView.setPoints(rightAnswerList);
float average = calculateAverage(rightAnswerList);
graphView.setAverage(average);
} else {
graphView.setPoints(leftAnswerList);
float average = calculateAverage(leftAnswerList);
graphView.setAverage(average);
}
setResults(leftAnswerList, rightAnswerList);
}
private float calculateAverage(List<EarSrt> steps) {
float srt = 0.0f;
int length = steps.size();
for (int i = (int)Math.ceil( (float)length/(float)2); i < length; i++) {
EarSrt es = steps.get(i);
srt += es.getSrt();
//printf("%f ," , [es srt]);
}
srt = srt / (length-(float)Math.ceil( (float)length/(float)2));
// printf("\n%f\n" , srt);
return srt;
}
private void setResults(List<EarSrt> leftEar, List<EarSrt> rightEar) {
float esLeft = calculateAverage(leftEar);
float esRight = calculateAverage(rightEar);
leftEarBad = (esLeft > 24.0);
rightEarBad = (esRight > 24.0);
bothEarsBad = (leftEarBad && rightEarBad);
setResultCaption(bothEarsBad, leftEarBad, rightEarBad);
}
/**
* TODO: this needs finishing
*/
private void setResultCaption(boolean bothEarsBad, boolean leftEarBad, boolean rightEarBad) {
TextView resultsTextView = (TextView)findViewById(R.id.results_text);
StringBuilder resultsText = new StringBuilder();
if (bothEarsBad) {
resultsText.append(getString(R.string.The_test_indicates_a_possible_hearing_loss));
resultsText.append(getString(R.string.We_recommend_that_you_visit_a_Hearing_Care_Professional_for_a_comprehensive_hearing_check));
}else{
if (leftEarBad) {
resultsText.append(getString(R.string.The_test_indicates_a_possible_hearing_loss_for_your_left_ear));
resultsText.append(getString(R.string.We_recommend_that_you_visit_a_Hearing_Care_Professional_for_a_comprehensive_hearing_check));
} else if (rightEarBad) {
resultsText.append(getString(R.string.The_test_indicates_a_possible_hearing_loss_for_your_Right_ear));
resultsText.append(getString(R.string.We_recommend_that_you_visit_a_Hearing_Care_Professional_for_a_comprehensive_hearing_check));
}else {
resultsText.append(getString(R.string.There_is_no_indication_of_hearing_loss));
}
}
resultsText.append(getString(R.string.The_results_of_the_hearing_test_are_not_to_be_utilized_as_an_official_outcome_for_assessing_levels_of_hearing_loss_True_hearing_loss_assessments_can_only_be_determined_by_a_licensed_hearing_healthcare_provider));
resultsTextView.setText(resultsText.toString());
}
public void goToLocate(View aView){
Intent locate = new Intent( this, LocateActivity.class);
startActivity(locate);
finish();
}
}
我基本上需要将此代码集成到两个片段中,一个用于左耳,一个用于右耳。我已经去看了Sherlock碎片样本,但仍然无法弄清楚如何去做。如果有人能指出我正确的方向,那将是非常有帮助的。
提前致谢。