【发布时间】:2016-04-29 19:41:47
【问题描述】:
我正在开发一个卡路里应用程序,用户在其中输入他们消耗的量 然后显示他们还剩多少卡路里。我为用户放置目标金额的共享首选项创建了一个 xml。
问题:获取共享首选项以减去我的列表视图总数。
现在该应用正在显示总金额。文本视图称为caloriesTotal。而不是显示总希望它显示还押。
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>
【问题讨论】:
-
愚蠢的问题,但是您能否只检索总卡路里,减去消耗的卡路里,然后将结果存储回共享偏好中?看起来caloriesTotal.setText 正在显示总卡路里,因为您告诉它这样做。您将文本设置为字符串 + var formattedCalories。格式化的卡路里定义为 Utils.formatNumber(totalCalorie)
-
我没试过。我不知道如何从共享偏好中获得价值。这就是我想要做的获取价值并将其恢复为总卡路里。我正在努力解决。
-
这是我第一次使用 java 以及开发应用程序。我知道这可能是个愚蠢的问题
标签: android listview android-fragments sharedpreferences android-preferences