我正在开发一个自己的应用程序,它将获取值并将其存储在共享首选项中,正在创建共享首选项,但该键的值存储为0.这里是主要活动
public class MainActivity extends ActionBarActivity {
int totalcount;
EditText etCount;
Button submit,cont;
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"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
etCount= (EditText)findViewById(R.id.editText1);
submit =(Button)findViewById(R.id.button1);
cont =(Button)findViewById(R.id.button2);
try{
totalcount= Integer.parseInt(etCount.getText().toString());
}catch(NumberFormatException nfe){
nfe.printStackTrace();
}
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences sp= getSharedPreferences("Count", MODE_APPEND);
SharedPreferences.Editor edit= sp.edit();
edit.putInt("totalcount", totalcount);
edit.commit();
Intent i= new Intent(getApplicationContext(),com.example.youtubehourlycounter.HourlyCountKeeper.class);
startActivity(i);
}
});
cont.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i= new Intent(getApplicationContext(),com.example.youtubehourlycounter.HourlyCountKeeper.class);
startActivity(i);
}
});
}
这是我尝试检索它的下一个活动。
public class HourlyCountKeeper extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hourly_count_keeper);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
ListView lv=(ListView)findViewById(R.id.listView1);
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"};
SharedPreferences sp=getSharedPreferences("Count", MODE_APPEND);
int str= sp.getInt("totalcount", 1000);
ArrayList<HashMap<String,HourlyData>> al = new ArrayList<HashMap<String,HourlyData>>();
for(int i=0;i<=8;i++){
HashMap<String, HourlyData> hm= new HashMap<String, HourlyData>();
HourlyData hd= new HourlyData();
hd.hours=hours[i].toString();
SharedPreferences sp1=getSharedPreferences("CurrentCountFor"+hours[i], MODE_APPEND);
int ccf= sp1.getInt("CurrentCountFor"+hours[i], 0);
int thishourscount=str/9;
hd.TargetByRemaining= (ccf+"/"+thishourscount).toString();
if(ccf==thishourscount){
hd.done=1;
}else{
hd.done=0;
}
hm.put("HourlyData", hd);
al.add(hm);
MyAdapter adapter= new MyAdapter(getApplicationContext(),al);
lv.setAdapter(adapter);
}}
当我签入文件管理器时,在应用程序内部 - &gt;共享首选项文件夹,我找到文件,拉动它们计数内的数据是这样的:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="count" value="0" />
</map>
谁能告诉我这里有什么问题?
答案 0 :(得分:0)
将以下代码放入{{1>} 提交按钮 -
onClickListener