我遇到了我见过的最奇怪的问题。我有一个基本的两窗格视图。有一个列出药物的窗格,单击药物会弹出第二个窗格,其中包含有关该药物的详细信息。细节片段应该是这样安排的:
[Med Name]
[Med Dosage]
[Date Filled]
[Duration]
据我所知,XML 代码证明了这一点:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/nameDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
android:freezesText="true"
android:layout_centerHorizontal="true"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/dosageDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:freezesText="true"
android:text="dosage"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/dateFilledDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dosageDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/durationDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dateFilledDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:text="duration"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
我使用以下代码设置要由每个字段显示的字符串:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor mCursor) {
int count = mCursor.getCount();
String str = "There are " + count + " rows.";
// TODO: REMOVE THIS TOAST
Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
int nameIndex = mCursor.getColumnIndex(MedTable.MED_NAME);
int dosageIndex = mCursor.getColumnIndex(MedTable.MED_DOSAGE);
int dateIndex = mCursor.getColumnIndex(MedTable.MED_DATE_FILLED);
int durationIndex = mCursor.getColumnIndex(MedTable.MED_DURATION);
if(mCursor != null) {
// Moves to the next row in the cursor.
while(mCursor.moveToNext()) {
// Create the name string
String medName = "Name: " + mCursor.getString(nameIndex);
// Create the dosage string
String medDosage = "Dosage: " + mCursor.getString(dosageIndex);
// Create a date format to parse the date string
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String epochString = mCursor.getString(dateIndex);
long epoch = Long.parseLong(epochString);
String date = sdf.format(new Date(epoch * 1000));
// Create the date string
String medDate = "Date Filled: " + date;
// Create the duration string
String medDuration = "Duration: " + mCursor.getString(durationIndex) + " days";
// Setting the name
TextView nameView = (TextView) getActivity().findViewById(R.id.nameDetailsView);
nameView.setText(medName);
// Setting dosage
TextView dosageView = (TextView) getActivity().findViewById(R.id.dosageDetailsView);
dosageView.setText(medDosage);
// Setting the date
TextView dateView = (TextView) getActivity().findViewById(R.id.dateFilledDetailsView);
dateView.setText(medDate);
// Setting the duration
TextView durationView = (TextView) getActivity().findViewById(R.id.durationDetailsView);
durationView.setText(medDuration);
// end of while loop
}
}
}
但是当我运行程序时,最奇怪的事情发生了。即,TextViews 以某种方式重新排序。而不是所需的顺序,我以某种方式得到
[Date Filled]
[Dosage]
[Name]
[Duration]
谁能告诉我为什么会发生这种情况?
编辑:注释掉对 setText() 的各种调用会导致 TextViews 以正确的顺序显示,尽管是硬编码的文本,而不是我想要的。
再次编辑:原来我需要做的就是清理项目。现在我知道了。