1

今まで見た中で最も奇妙な問題に直面しています。基本的な 2 ペイン ビューがあります。薬を一覧表示するペインがあり、薬をクリックすると、その薬に関する詳細を含む 2 番目のペインが表示されます。詳細フラグメントは、次のように配置する必要があります。

[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()へのさまざまな呼び出しをコメントアウトすると、テキストビューが正しい順序で表示されますが、必要なものではなく、ハードコードされたテキストが表示されます。

もう一度編集: 私がする必要があったのは、プロジェクトをきれいにすることだけでした。今私は知っている。

4

2 に答える 2

0

次のような Fragment でビューを見つけるべきではないと思います。

TextView nameView = (TextView) getActivity().findViewById(R.id.nameDetailsView);

Fragment の onCreateView メソッドでレイアウト ファイルを膨張させ、膨張させたレイアウト ビューでビューを検索してみてはいかがでしょうか。

于 2013-07-31T03:38:04.167 に答える