Вопрос

I'm experiencing the weirdest problem I've ever seen. I have a basic two-pane view. There's a pane that lists medications, and clicking a medication brings up the second pane, which contains the details about that medication. The details fragment is supposed to be arranged like this:

[Med Name]
[Med Dosage]
[Date Filled]
[Duration]

And the XML code bears this out, as far as I can tell:

<?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>

I set the strings to be displayed by each field using this code:

@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
        }
    }
}

But when I run the program, the weirdest thing happens. Namely, the TextViews get reordered somehow. Instead of the desired order, I somehow get

[Date Filled]
[Dosage]
[Name]
[Duration]

Can anyone tell me why on earth this might be happening?

EDIT: Commenting out the various calls to setText() causes the TextViews to display in the correct order, albeit with the hardcoded text, instead of what I want.

EDIT AGAIN: Turns out all I needed to do was clean the project. Now I know.

Это было полезно?

Решение

First try and remove + sign from all android:layout_below tags and make it like: android:layout_below="@id/dateFilledDetailsView". The + sign in the id declaration should be used only on the first appearance of the id. So have something as follows and comment the result:

<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>

Update:

For the sake keeping the answer updated, @Squonk's comment worked perfectly. The real issue was that R.java got corrupt. Cleaning the project solved the problem.

Другие советы

I don't think you should find view in Fragment like this:

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

Why don't you inflate layout file in onCreateView method in Fragment, then find view by inflated layout view.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top