简体   繁体   中英

how to save previous button state on next click item in Gallery

I'm new in android .I'm working with gallary.I would like to know how to save previous button state when I click on next gallery item.I want to make default button state off on all images and when button state will select with image it shoud be safe when I click on next one.When I come back on previous image it shoud be show saving button state .I tried to compare image id and button selected image id.But that's not really good work .How to do this?Thanks a lot for feature help.

 imageView = (ImageView) findViewById(R.id.ImageSportView);
    imageView.setImageResource(imgID[0]);
    sportButton = (Button) findViewById(R.id.SportButton1);

    gallery = (Gallery) findViewById(R.id.SportGallery);
    // creating AddImgadapter
    gallery.setAdapter(new AddImgAdapter(this));

        @Override
        public void onItemClick(AdapterView<?> perent, View view,

int position, long id) {

            // getting id position
            setSelected(position);
            // deselect button on item click
            onImageClick(view, position);
            Log.d(MY_LOG, "img click");
                        Log.d(MY_LOG, "img Id" + position);

        }

    });

}

// deselect button on image click
int itemPosition = 1;

public void onImageClick(View button, int position) {
    itemPosition = position;
    if (selectedPosition != position) {

        sportButton.setSelected(false);

        Log.d(MY_LOG, "selected postion " + selectedPosition + " = "
                + "item position" + position);

    } else {
        if (selectedPosition == position) {
            sportButton.setSelected(true);
        }
        Log.d(MY_LOG, "selected postion " + selectedPosition + " = "
                + "item position " + position);
    }

}

// getting Id current item
int selectedPosition = -1;

private void setSelected(int position) {

    selectedPosition = position;
    imageView.setImageResource(imgID[selectedPosition]);

}

public void onClickButton(View button) {

    if (button.isSelected()) {

        button.setSelected(false);

        Log.d(MY_LOG, "click off");
    } else {
        // on button click on we select picture id
        button.setSelected(true);
        if (selectedPosition != -1) {
            Log.d(MY_LOG, "selected position " + selectedPosition);
            //selectedImage(selectedPosition);
            //Intent intent = new Intent(Sport.this, Favorites.class);
        }
        Log.d(MY_LOG, "click on");

    }

}

One way to save the state of the previous button would be to make use of shared preferences in Android . Shared preferences allow key value pairs of data to be stored which can be later retrieved easily . There are one of the dataaccess mechanisms in Android . Others being SqlLite Database & Files .

Android Documentation on Share preference

Video on shared preference

Now coming back to your problem again . I once had to save the state of a checkedbutton . Then later access it again ( which seems to be similar to what you want to do as well )

    Part 1 Accessing Share preference Values : 

        public static final String PREFS_FILE = "MyPreferences";
        public static final String PREFS_NAME = "USER_NAME";
        public static final String PREFS_CHOICE = "USER_CHOICE";

        SharedPreferences sp;

        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            chkChoice = (CheckBox)findViewById(R.id.chkChoice);
            btnMain = (Button)findViewById(R.id.btnMain);
            btnMain.setOnClickListener(this);

            // Here i access the shared preference file .
            sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
            // If i have a preference for my checkbox saved then load it else FALSE(unchecked) 
            chkChoice.setChecked(sp.getBoolean(PREFS_CHOICE, false));
        }


   Part 2 Setting Share preference from your activity :

    sp = this.getSharedPreferences(PREFS_FILE, MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();

    editor.putString(PREFS_NAME, txtName.getText().toString());
    editor.putBoolean(PREFS_CHOICE, chkChoice.isChecked());

    editor.commit();

    // Close the activity to show that the data is still saved
    finish();

The above is for a checkbox . You will have to adapt it for the kind of button information you want to save . Hope this gets you started .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM