无法从总数中减去 SharedPreference 编号
Having trouble getting the SharedPreference number to subtract from a total
我正在开发一个卡路里应用程序,用户可以在其中输入他们消耗的量
然后显示他们还剩多少卡路里。我为用户放置目标金额的共享首选项创建了一个 xml。
问题:获取共享首选项以减去我的列表视图总数。
现在应用程序正在显示总金额。其中文本视图称为 caloriesTotal。而不是显示总想要它显示remanding。
FragmentHome.java
public class FragmentHome extends DialogFragment implements View.OnClickListener {
private TextView caloriesTotal;
private ListView listView;
private LinearLayout mLayout;
ImageButton AddEntrybtn;
CalorieDatabase calorieDB;
Context context;
private int foodCalories;
Button mButton;
//Database
private DatabaseHandler dba;
private ArrayList<Food> dbFoods = new ArrayList<>();
private CustomListViewAdapter foodAdapter;
private Food myFood ;
//fragment
private android.support.v4.app.FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
public FragmentHome() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_home, false);
AddEntrybtn = (ImageButton) myView.findViewById(R.id.AddItems);
AddEntrybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
((appMain) getActivity()).loadSelection(1);
}
});
caloriesTotal = (TextView) myView.findViewById(R.id.tv_calorie_amount);
listView = (ListView) myView.findViewById(R.id.ListId);
refreshData();
return myView;
}
public void refreshData (){
dbFoods.clear();
dba = new DatabaseHandler(getContext());
ArrayList<Food> foodsFromDB = dba.getFoods();
int totalCalorie = dba.totalCalories();
String formattedCalories = Utils.formatNumber(totalCalorie);
//setting the editTexts:
String Prefs="" ;
caloriesTotal.setText("Total Calories: " + formattedCalories );
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
editor.putString("prefs_key_current_calorie_amount", String.valueOf(Prefs));
editor.apply();
//Loop
for (int i = 0; i < foodsFromDB.size(); i ++){
String name = foodsFromDB.get(i).getFoodName();
String date = foodsFromDB.get(i).getRecordDate();
int cal = foodsFromDB.get(i).getCalories();
int foodId = foodsFromDB.get(i).getFoodId();
Log.v("Food Id", String.valueOf(foodId));
myFood= new Food();
myFood.setFoodId(foodId);
myFood.setFoodName(name);
myFood.setCalories(cal);
myFood.setRecordDate(date);
dbFoods.add(myFood);
}
dba.close();
//setting food Adapter:
foodAdapter = new CustomListViewAdapter(getActivity(),
R.layout.row_item,dbFoods);
listView.setAdapter(foodAdapter);
foodAdapter.notifyDataSetChanged();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle username = getActivity().getIntent().getExtras();
String username1 = username.getString("Username");
TextView userMain= (TextView) getView().findViewById(R.id.User);
userMain.setText(username1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.AddItems:
AddEntry addEntry = new AddEntry();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.FragmentHolder,addEntry)
.commit();
break;
case R.id.action_settings:
Intent preferenceScreenIntent = new Intent(getContext(),
PreferenceScreenActivity.class);
startActivity(preferenceScreenIntent);
break;
}
}
}
activity_preference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="User Settings">
<EditTextPreference
android:title="Daily Calorie Amount"
android:inputType="number"
android:key="@string/prefs_key_daily_calorie_amount"
android:summary="@string/prefs_description_daily_calorie_amount" />
</PreferenceCategory>
</PreferenceScreen>
您可以使用类似这样的方法从 SharedPreferences 中保存和检索整数:
savePrefs("calories", totalCalories);
totalCalories = loadPrefs("calories", totalCalories);
//save prefs
public void savePrefs(String key, int value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
//get prefs
public int loadPrefs(String key, int value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
return sharedPreferences.getInt(key, value);
}
Whosebug 上也有许多类似问题的示例,展示了如何使用 SharedPreferences。
这里是 developer.android.com 中有关 SharedPreferences 的一些文档:
http://developer.android.com/reference/android/content/SharedPreferences.html
http://developer.android.com/training/basics/data-storage/shared-preferences.html
我正在开发一个卡路里应用程序,用户可以在其中输入他们消耗的量 然后显示他们还剩多少卡路里。我为用户放置目标金额的共享首选项创建了一个 xml。
问题:获取共享首选项以减去我的列表视图总数。
现在应用程序正在显示总金额。其中文本视图称为 caloriesTotal。而不是显示总想要它显示remanding。
FragmentHome.java
public class FragmentHome extends DialogFragment implements View.OnClickListener {
private TextView caloriesTotal;
private ListView listView;
private LinearLayout mLayout;
ImageButton AddEntrybtn;
CalorieDatabase calorieDB;
Context context;
private int foodCalories;
Button mButton;
//Database
private DatabaseHandler dba;
private ArrayList<Food> dbFoods = new ArrayList<>();
private CustomListViewAdapter foodAdapter;
private Food myFood ;
//fragment
private android.support.v4.app.FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
public FragmentHome() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_home, false);
AddEntrybtn = (ImageButton) myView.findViewById(R.id.AddItems);
AddEntrybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
((appMain) getActivity()).loadSelection(1);
}
});
caloriesTotal = (TextView) myView.findViewById(R.id.tv_calorie_amount);
listView = (ListView) myView.findViewById(R.id.ListId);
refreshData();
return myView;
}
public void refreshData (){
dbFoods.clear();
dba = new DatabaseHandler(getContext());
ArrayList<Food> foodsFromDB = dba.getFoods();
int totalCalorie = dba.totalCalories();
String formattedCalories = Utils.formatNumber(totalCalorie);
//setting the editTexts:
String Prefs="" ;
caloriesTotal.setText("Total Calories: " + formattedCalories );
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
editor.putString("prefs_key_current_calorie_amount", String.valueOf(Prefs));
editor.apply();
//Loop
for (int i = 0; i < foodsFromDB.size(); i ++){
String name = foodsFromDB.get(i).getFoodName();
String date = foodsFromDB.get(i).getRecordDate();
int cal = foodsFromDB.get(i).getCalories();
int foodId = foodsFromDB.get(i).getFoodId();
Log.v("Food Id", String.valueOf(foodId));
myFood= new Food();
myFood.setFoodId(foodId);
myFood.setFoodName(name);
myFood.setCalories(cal);
myFood.setRecordDate(date);
dbFoods.add(myFood);
}
dba.close();
//setting food Adapter:
foodAdapter = new CustomListViewAdapter(getActivity(),
R.layout.row_item,dbFoods);
listView.setAdapter(foodAdapter);
foodAdapter.notifyDataSetChanged();
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle username = getActivity().getIntent().getExtras();
String username1 = username.getString("Username");
TextView userMain= (TextView) getView().findViewById(R.id.User);
userMain.setText(username1);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.AddItems:
AddEntry addEntry = new AddEntry();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.FragmentHolder,addEntry)
.commit();
break;
case R.id.action_settings:
Intent preferenceScreenIntent = new Intent(getContext(),
PreferenceScreenActivity.class);
startActivity(preferenceScreenIntent);
break;
}
}
}
activity_preference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="User Settings">
<EditTextPreference
android:title="Daily Calorie Amount"
android:inputType="number"
android:key="@string/prefs_key_daily_calorie_amount"
android:summary="@string/prefs_description_daily_calorie_amount" />
</PreferenceCategory>
</PreferenceScreen>
您可以使用类似这样的方法从 SharedPreferences 中保存和检索整数:
savePrefs("calories", totalCalories);
totalCalories = loadPrefs("calories", totalCalories);
//save prefs
public void savePrefs(String key, int value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.apply();
//get prefs
public int loadPrefs(String key, int value) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
return sharedPreferences.getInt(key, value);
}
Whosebug 上也有许多类似问题的示例,展示了如何使用 SharedPreferences。
这里是 developer.android.com 中有关 SharedPreferences 的一些文档: http://developer.android.com/reference/android/content/SharedPreferences.html http://developer.android.com/training/basics/data-storage/shared-preferences.html