【发布时间】:2016-02-18 12:53:48
【问题描述】:
我有一个列表视图,当单击列表项时会打开一个新的单视图活动
singleitenm 视图包含一个按钮,用于将相应的列表项添加到收藏夹活动(如果尚未添加),并将按钮颜色更改为黄色,表示它已添加到收藏夹中,否则为灰色,如果不在收藏夹中
到这里一切正常,但如果我关闭 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)
{}
}
【问题讨论】:
标签: android listview button android-activity