关闭后即可保持活动的变化

时间:2016-02-18 12:53:49

标签: android listview button android-activity

我有一个listview,当点击listitem时会打开一个新的单一视图活动

单一视图包含一个按钮,用于将相应的列表项添加到收藏夹活动(如果尚未添加),并将按钮颜色更改为黄色,表示它已添加到favoorites,否则为灰色,如果不是收藏夹

直到这里一切正常但是如果我关闭了singleitemview并重新打开它,按钮颜色会变回以前的颜色,但仍然在其他颜色中

如何更改按钮颜色

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





        });

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



}

1 个答案:

答案 0 :(得分:0)

在您的singleitemview活动的UploadedTemplatesService方法中,在您为具有收藏夹按钮的视图充气后,您只需检查该项目是否在收藏夹中并相应地设置按钮的背景颜色。