gpt4 book ai didi

android - 共享首选项未更新

转载 作者:行者123 更新时间:2023-11-30 02:57:26 24 4
gpt4 key购买 nike

我有一项主要 Activity 是创建共享偏好并每小时为其分配一个值。那里的共享首选项正在创建,并传递给一个 ListView ,该 ListView 使用适配器类填充并从相同的共享首选项中提取值在第二个 Activity 中,用于检索的代码写在简历中,以便它可以按下后退时显示更新的数字。看起来像这样,onlisttem click listener 将控件转发给这个 Activity ,该 Activity 应该添加或减去值并更新共享首选项,我在 onpause 和 onBackPressed 中更新它,因为我认为它太多了,无法放入 onClickListener,这是上次计数器 Activity 的代码。问题是共享首选项没有得到更新。

ImageButton add,sub,push;
TextView countkeeper, countpusher;
String []hours={"8:30 - 9:30","9:30 - 10:30","10:30 - 11:30","11:30 - 12:30","12:30 - 1:30","1:30 - 2:30","2:30 - 3:30","3:30 - 4:30","4:30 - 5:30"};
int lip,c,target;
Intent i;
SharedPreferences sp,sp1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}

add=(ImageButton)findViewById(R.id.imageButton1);
sub=(ImageButton)findViewById(R.id.imageButton2);
push=(ImageButton)findViewById(R.id.imageButton3);
countkeeper=(TextView)findViewById(R.id.countkeeper);
countpusher=(TextView)findViewById(R.id.countpusher);

sp= getSharedPreferences("Count", MODE_APPEND);
Log.d("CurrentCountFor"+hours[lip],""+sp.getInt("CurrentCountFor"+hours[lip], 10));
c=sp.getInt("CurrentCountFor"+hours[lip], 10);
target=sp.getInt("TargetFor"+hours[lip], 10);



i=getIntent();
lip= i.getIntExtra("lip", 0);


add.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

c = c+1;
countkeeper.setText("Count for "+hours[lip]+" is "+c+"/"+target);



}
});

sub.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
c=c-1;
countkeeper.setText("Count for "+hours[lip]+" is "+c+"/"+target);


}
});
}

@Override
public void onBackPressed() {
super.onBackPressed();
sp1= getSharedPreferences("Count", MODE_APPEND);
SharedPreferences.Editor edit=sp1.edit();
edit.putInt("CurrentCountFor"+hours[lip], c);
sp1.edit().commit();

};

@Override
protected void onPause() {
super.onPause();
sp1= getSharedPreferences("Count", MODE_APPEND);
SharedPreferences.Editor edit=sp1.edit();
edit.putInt("CurrentCountFor"+hours[lip], c);
sp1.edit().commit();


};

请帮助任何人。

最佳答案

sp1.edit().commit(); 替换为 edit.commit();

你有两次相同的问题。

关于android - 共享首选项未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23045623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com