Android 当singleitemview关闭并重新打开时,按钮图像会发生更改

Android 当singleitemview关闭并重新打开时,按钮图像会发生更改,android,listview,button,Android,Listview,Button,我有一个listview,单击listitem会打开一个新的SingleItemView活动 SingleItem视图包含一个按钮,用于将相应的listitem添加到“收藏夹”活动(如果尚未添加),并将按钮颜色更改为黄色,表示已将其添加到favoorites中,如果未添加到“收藏夹”中,则将按钮颜色更改为灰色 在这里,一切正常,但如果我关闭singleitemview并重新打开它,按钮的颜色会变回以前的颜色,但仍然是facvorites 我该怎么做才能保留按钮的颜色 单击我的列表活动 publi

我有一个listview,单击listitem会打开一个新的SingleItemView活动 SingleItem视图包含一个按钮,用于将相应的listitem添加到“收藏夹”活动(如果尚未添加),并将按钮颜色更改为黄色,表示已将其添加到favoorites中,如果未添加到“收藏夹”中,则将按钮颜色更改为灰色 在这里,一切正常,但如果我关闭singleitemview并重新打开它,按钮的颜色会变回以前的颜色,但仍然是facvorites

我该怎么做才能保留按钮的颜色

单击我的列表活动

public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

                            ObjectMapper mapper = new ObjectMapper();
                            Product pro = productListAdapter.getItem(position);

                            String favimg = ((ImageView) view.findViewById(R.id.imgbtn_favorite)).toString();

    try
    {
        String hi = "this is testint";


        String jsonInString = mapper.writeValueAsString(pro);

        Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class);
        intent.putExtra("selected item", jsonInString);


        ;

        startActivityForResult(intent, 1);
    //  startActivity(intent);
    }
    catch (JsonProcessingException e)
    {}  



}
final Button btn = (Button) findViewById(R.id.singleitemButton1);
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            products=new ArrayList<Product>();
            Bundle extras = getIntent().getExtras();

            String jsonObj = extras.getString("selected item");


            ObjectMapper mapper = new ObjectMapper();

            try
            {
                Product pro = mapper.readValue(jsonObj, Product.class);

                if (checkFavoriteItem(pro)) {

                    sharedPreference.removeFavorite(SingleItemView.this, pro);

                btn.setBackgroundColor(Color.GRAY);
                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.remove_favr),
                                   Toast.LENGTH_SHORT).show();


                } else {
                    sharedPreference.addFavorite(SingleItemView.this, pro);
                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.add_favr),
                                   Toast.LENGTH_SHORT).show();
                        btn.setBackgroundColor(Color.YELLOW);

                }
            }
            catch (IOException e)
            {};





        });
public void onItemClick(AdapterView父视图、视图、int位置、,
长id){
ObjectMapper mapper=新的ObjectMapper();
Product pro=productListAdapter.getItem(位置);
字符串favimg=((ImageView)view.findviewbyd(R.id.imgbtn_favorite)).toString();
尝试
{
String hi=“这是测试”;
字符串jsonInString=mapper.writeValueAsString(pro);
Intent Intent=new Intent(activity.getApplicationContext(),SingleItemView.class);
intent.putExtra(“选定项”,jsonInString);
;
startActivityForResult(意向,1);
//星触觉(意向);
}
捕获(JsonProcessingException e)
{}  
}
singleitemview活动的favbtn

public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

                            ObjectMapper mapper = new ObjectMapper();
                            Product pro = productListAdapter.getItem(position);

                            String favimg = ((ImageView) view.findViewById(R.id.imgbtn_favorite)).toString();

    try
    {
        String hi = "this is testint";


        String jsonInString = mapper.writeValueAsString(pro);

        Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class);
        intent.putExtra("selected item", jsonInString);


        ;

        startActivityForResult(intent, 1);
    //  startActivity(intent);
    }
    catch (JsonProcessingException e)
    {}  



}
final Button btn = (Button) findViewById(R.id.singleitemButton1);
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            products=new ArrayList<Product>();
            Bundle extras = getIntent().getExtras();

            String jsonObj = extras.getString("selected item");


            ObjectMapper mapper = new ObjectMapper();

            try
            {
                Product pro = mapper.readValue(jsonObj, Product.class);

                if (checkFavoriteItem(pro)) {

                    sharedPreference.removeFavorite(SingleItemView.this, pro);

                btn.setBackgroundColor(Color.GRAY);
                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.remove_favr),
                                   Toast.LENGTH_SHORT).show();


                } else {
                    sharedPreference.addFavorite(SingleItemView.this, pro);
                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.add_favr),
                                   Toast.LENGTH_SHORT).show();
                        btn.setBackgroundColor(Color.YELLOW);

                }
            }
            catch (IOException e)
            {};





        });
final按钮btn=(按钮)findViewById(R.id.singleitemButton1);
btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
products=新的ArrayList();
Bundle extras=getIntent().getExtras();
String jsonObj=extras.getString(“选定项”);
ObjectMapper mapper=新的ObjectMapper();
尝试
{
Product pro=mapper.readValue(jsonObj,Product.class);
如果(选中FavoriteItem(pro)){
SharedReference.removeFavorite(SingleItemView.this,pro);
btn.setBackgroundColor(颜色为灰色);
Toast.makeText(SingleItemView.this,
SingleItemView.this.getResources().getString(R.string.remove_favr),
吐司。长度(短)。show();
}否则{
SharedReference.addFavorite(SingleItemView.this,pro);
Toast.makeText(SingleItemView.this,
SingleItemView.this.getResources().getString(R.string.add_favr),
吐司。长度(短)。show();
btn.setBackgroundColor(颜色:黄色);
}
}
捕获(IOE异常)
{};
});