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

时间:2016-02-18 11:19:11

标签: android listview button

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

如何保留按钮颜色

onitemclick我的列表活动

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)
    {}  



}

赞成singleitemview活动

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)
            {};





        });

0 个答案:

没有答案