如何在Gallery中的下一个单击项目上保存上一个按钮状态

时间:2013-05-15 08:11:00

标签: android android-button android-gallery

我是android的新手。我正在使用gallary。我想知道当我点击下一个图库项目时如何保存以前的按钮状态。我想在所有图像和按钮上关闭默认按钮状态当我点击下一张图片时,状态会选择带图像。当我回到之前的图像时,它应该显示保存按钮状态。我试图比较图像ID和按钮选择的图像id。但这不是很好的工作。怎么做?非常感谢功能帮助。

 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");

    }

}

1 个答案:

答案 0 :(得分:0)

保存上一个按钮状态的一种方法是在Android中使用共享首选项。共享首选项允许存储关键值数据对,以后可以轻松检索。 Android中有一种数据访问机制。其他人是SqlLite数据库&amp;档案。

Android Documentation on Share preference

Video on shared preference

现在再次回到你的问题。我曾经不得不保存一个checkbutton的状态。然后再次访问它(这似乎与你想要做的类似)

    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();

上面是一个复选框。您必须根据要保存的按钮信息进行调整。希望这能让你开始。