Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.3 kB
2
Indexable
Never
package pl.pollub.android.app_1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class GradesActivity extends AppCompatActivity {

    private Button sendGrades;
    private RecyclerView gradesListRv;
    private List<Subject> subjectList;
    public static String GRADES_STATE_KEY = "GRADES_STATE_KEY";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grades);
        this.gradesListRv = findViewById(R.id.grades_list_rv);
        this.sendGrades = findViewById(R.id.send_grades_bt);
        this.subjectList = new ArrayList<>();
        Intent intent = getIntent();
        if(intent != null){
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                int numberOfSubject = Integer.parseInt(bundle.getString(MainActivity.NUMBER_OF_GRADES_KEY));
                String[] subjects = getResources().getStringArray(R.array.nazwy_przedmiotow_tab);
                for(int i = 0; i < numberOfSubject; i++){
                    this.subjectList.add(new Subject(subjects[i],2));
                }
            }
        }
        if(savedInstanceState != null){
            restoreGradesState(savedInstanceState);
        }
        SubjectsListAdapter adapter = new SubjectsListAdapter(this.subjectList,this);
        this.gradesListRv = findViewById(R.id.grades_list_rv);
        this.gradesListRv.setAdapter(adapter);
        this.gradesListRv.setLayoutManager(new LinearLayoutManager(this));
        this.sendGrades.setOnClickListener(view -> {
            Intent intentToMain = new Intent(this, MainActivity.class);
            Bundle bundleToMain = new Bundle();
            intentToMain.putExtras(bundleToMain);
            setResult(RESULT_OK,intentToMain);
            this.finish();
        });
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        StringBuilder state = new StringBuilder();
        this.subjectList.stream().forEach(subject -> {
            state.append(subject.getGrade());
            state.append(";");
        });
        outState.putString(GRADES_STATE_KEY, state.toString());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        restoreGradesState(savedInstanceState);
    }

    private void restoreGradesState(@NonNull Bundle savedInstanceState) {
        String currentGradesState = savedInstanceState.getString(GRADES_STATE_KEY);
        if(currentGradesState != null){
            String[] gradesStr = currentGradesState.split(";");
            for(int i = 0; i<gradesStr.length; i++){
                this.subjectList.get(i).setGrade(Integer.parseInt(gradesStr[i]));
            }
        }
    }
}
Leave a Comment