0

Работаю в Android Studio, надо написать приложение. Но, оно не работает. Задача приложения: с низу активити вводим код коробки (их может быть несколько) и содержимое. Сверху вводим код уже сохраненной коробки и получаем её содержимое.

Xml код:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mrfair.homesklad.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/textView"
        android:layout_width="364dp"
        android:layout_height="61dp"
        android:layout_marginBottom="192dp"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toTopOf="@+id/editText3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button7" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="327dp"
        android:layout_height="39dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="Введите код существующей коробки "
        android:inputType="number"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="72dp"
        android:layout_marginBottom="22dp"
        android:layout_marginStart="29dp"
        android:layout_marginTop="82dp"
        android:onClick="readFile"
        android:text="Показать содержимое коробки"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintStart_toStartOf="@+id/editText"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="29dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="66dp"
        android:layout_marginBottom="57dp"
        android:layout_marginRight="16dp"
        android:onClick="writefile"
        android:text="Добавить новую или изменить старую коробку"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText3" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="276dp"
        android:layout_height="42dp"
        android:ems="10"
        android:hint="Введите код новой коробки"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.496"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.559" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="329dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:ems="10"
        android:hint="Введите содержимое новой коробки"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>

Java код:

package com.example.mrfair.homesklad;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    SharedPreferences sPref;
    TextView content;
    EditText code;
    String codetext;
    EditText code2;
    EditText content2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        code = (EditText)findViewById(R.id.editText);
        content = (TextView)findViewById(R.id.textView);
        codetext = code.getText().toString();
        code2 = findViewById(R.id.editText2);
        content2 = findViewById(R.id.editText3);
    }

    public void readFile (View v) {
        sPref = getPreferences(MODE_PRIVATE);
        String saved_text = sPref.getString(codetext, "");
        content.setText(saved_text);
    }
    public void writefile (View v){
        sPref = getPreferences(MODE_PRIVATE);
        String codetext = code2.getText().toString();
        String conttext = content2.getText().toString();
        sPref = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor et = sPref.edit();
        et.putString(codetext, conttext);
        et.apply();
    }
}

Манифест:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mrfair.homesklad">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Не работают преференсы. Что делать?

5
  • Чем данный вопрос отличается от этого?
    – post_zeew
    10 янв 2018 в 6:46
  • Тем что, здесь один активити
    – Ermine
    10 янв 2018 в 6:54
  • В чем выражается Не работают преференсы?
    – post_zeew
    10 янв 2018 в 7:12
  • 2
    codetext = code.getText().toString(); - в onCreate текст ещё не введён. Нужно перенести этот вызов туда, где текст уже должен быть (readFile в Вашем случае). И желательно добавить проверку, что текст есть и он правильный
    – woesss
    10 янв 2018 в 7:25
  • @woesss Спасибо большое!
    – Ermine
    10 янв 2018 в 8:00

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Посмотрите другие вопросы с метками или задайте свой вопрос.