Dastlab tanlagan ip chiziqning qiymatini qanday saqlay olaman?

Menda bor:

  1. kategoriya rasmini, imageViewni o'z ichiga olgan 1-parcha.

  2. 2-parcha orqaga qaytish tugmasi mavjud.

Dastlab spinner kategoriya qiymatini tanladim, shundan so'ng galereyadan rasmni tushirganimda, spinnerning qiymati o'zgaradi va boshlang'ich pozitsiyasiga 0 qaytadi va men tugmani orqaga tugmani bosganimda xuddi shu muammo.

spinner.setSelection (poisition) dan foydalanayotgan edim, lekin buni qaerdan olganimni bilmayman.

Xullas, galereyadan rasmni olsam yoki orqamga tugmani bosgan bo'lsam ham qanday qilib spinnerning tanlangan qiymatini saqlay olaman?

CrateView manbasi:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_add_book_photos,
            container, false);

    auth = FirebaseAuth.getInstance();
    mDatabase = FirebaseDatabase.getInstance();
    myRef = mDatabase.getReference("categories");
    spinnerCat = (Spinner) rootView.findViewById(R.id.spinner_cat_book);
    selectImage = (ImageView) rootView.findViewById(R.id.book_picture);

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            final List catListBook = new ArrayList();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                Categoriesbook book = snapshot.getValue(Categoriesbook.class);
                String catBook = book.getNom_cat();
                catListBook.add(catBook);
            }

            ArrayAdapter catAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, catListBook);
            spinnerCat.setAdapter(catAdapter);

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            System.out.println("The read failed: " + databaseError.getMessage());

        }
    });

    spinnerCat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            catlivre = (String) parent.getItemAtPosition(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    selectImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showFileChooser();

        }
    });

    return rootView;

}
0

6 javoblar

harakat qilib ko'ring:

use SharedPreferences to save the selected position of Spinner

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    catlivre = (String) parent.getItemAtPosition(position);
    int selected_item = spinner.getSelectedItemPosition();
    SharedPreferences sharedPref = getActivity().getSharedPreferences("Position",0);
    SharedPreferences.Editor prefEditor = sharedPref.edit();
    prefEditor.putInt("spinner_item", selected_item);
    prefEditor.commit();
}

Endi Framnerning onCreateView() ilovasida oldingi tanlangan taraytirgich joylashuvini joylashtirishingiz mumkin:

SharedPreferences sharedPref = getActivity().getSharedPreferences("Position", Context.MODE_PRIVATE);
    int spinnerValue = sharedPref.getInt("spinner_item",-1);
    if(spinnerValue != -1) {
       //set the value of the spinner 
        spinner.setSelection(spinnerValue,true);
    }
1
qo'shib qo'ydi
spinnerCat = (Spinner) rootView.findViewById (R.id.spinner_cat_book); birinchi marta kodni ishga tushirishingiz kerak.
qo'shib qo'ydi muallif rafsanahmad007, manba
Ushbu Javobni spinner.setSelection (spinnerValue, true ); - adapterni o'rnatgandan keyin chaqirilishi kerak. buni amalga oshirish uchun
qo'shib qo'ydi muallif rafsanahmad007, manba
Err bor: java.lang.NullPointerException spinnerCat.setSelection da (spinnerValue);
qo'shib qo'ydi muallif Amal, manba
Endi spinnerValue holatini olaman, lekin spinnerCat.setSelection (spinnerValue); ishlamaydi
qo'shib qo'ydi muallif Amal, manba
Rahmat aytishni taqiqlash mumkin, lekin rahmat. ishlamoqda
qo'shib qo'ydi muallif Amal, manba

harakat qilib ko'ring:

use SharedPreferences to save the selected position of Spinner

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    catlivre = (String) parent.getItemAtPosition(position);
    int selected_item = spinner.getSelectedItemPosition();
    SharedPreferences sharedPref = getActivity().getSharedPreferences("Position",0);
    SharedPreferences.Editor prefEditor = sharedPref.edit();
    prefEditor.putInt("spinner_item", selected_item);
    prefEditor.commit();
}

Endi Framnerning onCreateView() ilovasida oldingi tanlangan taraytirgich joylashuvini joylashtirishingiz mumkin:

SharedPreferences sharedPref = getActivity().getSharedPreferences("Position", Context.MODE_PRIVATE);
    int spinnerValue = sharedPref.getInt("spinner_item",-1);
    if(spinnerValue != -1) {
       //set the value of the spinner 
        spinner.setSelection(spinnerValue,true);
    }
1
qo'shib qo'ydi
spinnerCat = (Spinner) rootView.findViewById (R.id.spinner_cat_book); birinchi marta kodni ishga tushirishingiz kerak.
qo'shib qo'ydi muallif rafsanahmad007, manba
Ushbu Javobni spinner.setSelection (spinnerValue, true ); - adapterni o'rnatgandan keyin chaqirilishi kerak. buni amalga oshirish uchun
qo'shib qo'ydi muallif rafsanahmad007, manba
Err bor: java.lang.NullPointerException spinnerCat.setSelection da (spinnerValue);
qo'shib qo'ydi muallif Amal, manba
Endi spinnerValue holatini olaman, lekin spinnerCat.setSelection (spinnerValue); ishlamaydi
qo'shib qo'ydi muallif Amal, manba
Rahmat aytishni taqiqlash mumkin, lekin rahmat. ishlamoqda
qo'shib qo'ydi muallif Amal, manba

harakat qilib ko'ring:

use SharedPreferences to save the selected position of Spinner

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    catlivre = (String) parent.getItemAtPosition(position);
    int selected_item = spinner.getSelectedItemPosition();
    SharedPreferences sharedPref = getActivity().getSharedPreferences("Position",0);
    SharedPreferences.Editor prefEditor = sharedPref.edit();
    prefEditor.putInt("spinner_item", selected_item);
    prefEditor.commit();
}

Endi Framnerning onCreateView() ilovasida oldingi tanlangan taraytirgich joylashuvini joylashtirishingiz mumkin:

SharedPreferences sharedPref = getActivity().getSharedPreferences("Position", Context.MODE_PRIVATE);
    int spinnerValue = sharedPref.getInt("spinner_item",-1);
    if(spinnerValue != -1) {
       //set the value of the spinner 
        spinner.setSelection(spinnerValue,true);
    }
1
qo'shib qo'ydi
spinnerCat = (Spinner) rootView.findViewById (R.id.spinner_cat_book); birinchi marta kodni ishga tushirishingiz kerak.
qo'shib qo'ydi muallif rafsanahmad007, manba
Ushbu Javobni spinner.setSelection (spinnerValue, true ); - adapterni o'rnatgandan keyin chaqirilishi kerak. buni amalga oshirish uchun
qo'shib qo'ydi muallif rafsanahmad007, manba
Err bor: java.lang.NullPointerException spinnerCat.setSelection da (spinnerValue);
qo'shib qo'ydi muallif Amal, manba
Endi spinnerValue holatini olaman, lekin spinnerCat.setSelection (spinnerValue); ishlamaydi
qo'shib qo'ydi muallif Amal, manba
Rahmat aytishni taqiqlash mumkin, lekin rahmat. ishlamoqda
qo'shib qo'ydi muallif Amal, manba

A fragment tells you when it comes out of view by calling the callbacks defined in the Fragment lifecycle. https://developer.android.com/guide/components/fragments.html

Siz qilishingiz kerak bo'lgan barcha narsalar davlat muhofazasi va qaytarib olishda kodlashdir.

0
qo'shib qo'ydi

A fragment tells you when it comes out of view by calling the callbacks defined in the Fragment lifecycle. https://developer.android.com/guide/components/fragments.html

Siz qilishingiz kerak bo'lgan barcha narsalar davlat muhofazasi va qaytarib olishda kodlashdir.

0
qo'shib qo'ydi

A fragment tells you when it comes out of view by calling the callbacks defined in the Fragment lifecycle. https://developer.android.com/guide/components/fragments.html

Siz qilishingiz kerak bo'lgan barcha narsalar davlat muhofazasi va qaytarib olishda kodlashdir.

0
qo'shib qo'ydi