1

アプリの一部の状態を保存する際に問題があります。

レイアウトには 5 EditTextaButtonと a がありTextViewます。ユーザーが値を入力して計算すると (ボタンをクリック)、データが取り込まれTextViewます。

向きが変わっても何も起こらない場合、ビューは渡されて完全に機能しますが、( my で) スワイプするViewPagerと、入力された状態TextViewが保存されません。3 つのフラグメントがあり、右から中央にスワイプするだけで保存されますが、左端にスワイプするTextViewと、フラグメントに戻ったときに入力されていないため、入力された値EditTextが保存されます。

私の予想では、onClick()(計算を行って にデータを入力するTextView) からのプロセスは保存されません。Google サイトのガイドをフォローアップしようとしましたが、うまくいきませんでした。

私のコードは次のとおりです。

public class FuelConsumptionFragment extends Fragment implements OnClickListener {


View view;
int savedState = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (savedInstanceState != null) {

        savedState = savedInstanceState.getInt("curChoice", 0);

    }

}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", savedState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fuel_consumption, container, false);

    Button calculate_fuel = (Button)view.findViewById(R.id.calculate_fuel);
    calculate_fuel.setOnClickListener(this);

    return view;

    }

マニフェスト:

<activity
            android:name="simcas.fartberegneren.MainActivity"
            android:icon="@drawable/fartberegneren"
            android:label="" 
            android:configChanges="orientation|screenSize" >
</activity>

編集:インフレが問題である可能性があるという漠然とした考えがありonCreateViewますが、これは正しいですか?

編集 2:コードを調べて、「カスタム」アダプターが問題の一部である可能性があることに気付くと、コードは次のようになります。

public class MyAdapter extends FragmentStatePagerAdapter {

public MyAdapter(FragmentManager fm) {
    super(fm);

}

@Override
public int getCount() {
    return 3;
}


@Override
public Fragment getItem(int position) {
    switch (position) {
    case 0: return new SpeedZonesFragment();
    case 1: return new DistanceFragment();
    case 2: return new FuelConsumptionFragment();
    default: return null;
    }
}
}

これにより問題が発生する可能性はありますか? Fragment位置を切り替えるときに新しいものを作成しているように見えますが、正しいですか?

編集 3:のコードonClick():

@Override
public void onClick(View view) {

DecimalFormat format = new DecimalFormat("#.#");

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

EditText distance_entry_fuel = (EditText) getActivity().findViewById(R.id.distance_fuel);
EditText speed_a_entry_fuel = (EditText) getActivity().findViewById(R.id.speed_a_fuel);
EditText gas_a_use = (EditText) getActivity().findViewById(R.id.gas_use_a);
EditText speed_b_entry_fuel = (EditText) getActivity().findViewById(R.id.speed_b_fuel);
EditText gas_b_use = (EditText) getActivity().findViewById(R.id.gas_use_b);
EditText gas_price = (EditText) getActivity().findViewById(R.id.gas_price);

distance_entry_fuel.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
speed_a_entry_fuel.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
gas_a_use.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
speed_b_entry_fuel.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
gas_b_use.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
gas_price.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);


distance_entry_fuel.clearFocus();
speed_a_entry_fuel.clearFocus();
gas_a_use.clearFocus();
speed_b_entry_fuel.clearFocus();
gas_b_use.clearFocus();

double moneySpend;
double timeArest;
double timeArest1;
double timeBrest;
double timeBrest1;
double totalTime;
double totalTimerest;

try {

    double distance = Double.parseDouble(distance_entry_fuel.getText().toString());
    double speedA = Double.parseDouble(speed_a_entry_fuel.getText().toString());
    double gasA = Double.parseDouble(gas_a_use.getText().toString());
    double speedB = Double.parseDouble(speed_b_entry_fuel.getText().toString());
    double gasB = Double.parseDouble(gas_b_use.getText().toString());
    double gasPrice = Double.parseDouble(gas_price.getText().toString());

    double gasUseA = (distance / gasA);
    double gasUseB = (distance / gasB);

    if (speedB > speedA) {

        if (gasUseB > gasUseA) {

            moneySpend = ((gasUseB - gasUseA) * gasPrice);

        }

        else {

            moneySpend = ((gasUseA - gasUseB) * gasPrice);

        }

        timeArest = (distance % speedA); // Calculate the remainder of A
        timeArest1 = ((timeArest / speedA) * 60); // Convert the remainder of A to minutes       
        timeBrest = (distance % speedB); // Calculate the remainder of B
        timeBrest1 = ((timeBrest / speedB) * 60); // Convert the remainder of B to minutes

        totalTime = (int)((distance / speedA) - (distance / speedB)); // Calculate the amount of hours saved            

        if (timeArest >= timeBrest) {   // Condition for calculating time

            totalTimerest = (timeArest1 - timeBrest1);

        }

        else {                          // opposite condition

            totalTimerest = ((timeArest1 - timeBrest1) + 60); // Make up for the negative number

        }           

    }

    else {

        if (gasUseB > gasUseA) {

            moneySpend = ((gasUseB - gasUseA) * gasPrice);

        }

        else {

            moneySpend = ((gasUseA - gasUseB) * gasPrice);

        }

        timeArest = (distance % speedA); // Calculate the remainder of A
        timeArest1 = ((timeArest / speedA) * 60); // Convert the remainder of A to minutes       
        timeBrest = (distance % speedB); // Calculate the remainder of B
        timeBrest1 = ((timeBrest / speedB) * 60); // Convert the remainder of B to minutes

        totalTime = (int)((distance / speedB) - (distance / speedA)); // Calculate the amount of hours saved

        if (timeBrest >= timeArest) {   // Condition for calculating time

            totalTimerest = (timeBrest1 - timeArest1);

        }

        else {                          // opposite condition

            totalTimerest = ((timeBrest1 - timeArest1) + 60); // Make up for the negative number

        }   
    }

    String minuteTotal = "";
    String hourTotal = "";

    if (totalTimerest == 1) {

        minuteTotal = " minut";

        }

    else { 

        minuteTotal = " minutter";

    }

    if (totalTime == 1) {

       hourTotal = " time";

    }

    else {

        hourTotal = " timer";

    }

    String moneySpendOnGas;

    String distanceText = "Over en strækning på " + format.format(Double.parseDouble(distance_entry_fuel.getText().toString())) + " km";
    String gasUsedA = "Bruger du " + format.format(gasUseA) + " liter benzin, ved en gennemsnitsfart på " + speedA + " km/t.";
    String gasUsedB = "Bruger du " + format.format(gasUseB) + " liter benzin, ved en gennemsnitsfart på " + speedB + " km/t.";

    if (totalTime > 0) {

        if (totalTimerest > 0) {

            moneySpendOnGas = "Det koster dig " + format.format(moneySpend) + " kr at komme " + (int)totalTime + hourTotal + " og " + (int)totalTimerest + minuteTotal + " hurtigere frem.";

        }

        else {

            moneySpendOnGas = "Det koster dig " + format.format(moneySpend) + " kr at komme " + (int)totalTime + hourTotal + " hurtigere frem.";

        }

    }

    else {

        moneySpendOnGas = "Det koster dig " + format.format(moneySpend) + " kr at komme " + (int)totalTimerest + minuteTotal + " hurtigere frem.";

    }

    TextView distance_text_fuel = (TextView) getActivity().findViewById(R.id.distance_text_fuel);
    distance_text_fuel.setText(distanceText);
    TextView gas_used_a = (TextView) getActivity().findViewById(R.id.gas_used_a);
    gas_used_a.setText(gasUsedA);
    TextView gas_used_b = (TextView) getActivity().findViewById(R.id.gas_used_b);
    gas_used_b.setText(gasUsedB);
    TextView money_spend = (TextView) getActivity().findViewById(R.id.money_spend);
    money_spend.setText(moneySpendOnGas);
}

catch (NumberFormatException e) {

    DialogFragment alert = new EntryAlertDialog();
    alert.show(getFragmentManager(), "Alert");

}

}   
4

1 に答える 1

3

その状態を強制的TextViewに保存することもできEditTextます。レイアウト ファイルでタグを呼び出すTextView.setFreezesText(true)か追加するandroid:freezesText="true"だけです。TextView

于 2013-02-25T14:12:29.530 に答える