I'm trying to make a simple listview that contains a list of different foods you've eaten throughout the day. I created a simple foodItem class which just holds a string and a calorie counter, and I created a FoodItemAdapter class, which overrides the ArrayAdapter class. For some reason I crash when I try to launch the app, and it appears that the textViews haven't been loaded properly. This is the error:
05-17 14:20:20.528 21314-21314/com.example.jake.caloriecounter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jake.caloriecounter, PID: 21314
android.content.res.Resources$NotFoundException: String resource ID #0x64
at android.content.res.Resources.getText(Resources.java:286)
at android.widget.TextView.setText(TextView.java:4148)
at com.example.jake.caloriecounter.FoodItemAdapter.getView(FoodItemAdapter.java:38)
This is the code for the food item adapter
public class FoodItemAdapter extends ArrayAdapter<FoodItem> {
Context context;
String TAG = "FoodItemAdapter";
public FoodItemAdapter(Context context, int resourceId, ArrayList<FoodItem> foodList) {
super(context,resourceId,foodList);
this.context = context;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
//Inflate if needed
if(convertView == null) {
//Need to create a new view
Log.d(TAG,"Creating new view.....");
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_food_item, null);
} else {
Log.d(TAG,"Using old convertView.....");
}
FoodItem item = getItem(position); //Gets data assosciated with this position from the data
if(item != null) {
//Get views so we can set them to the correct data
TextView foodTextView = (TextView) convertView.findViewById(R.id.foodName);
TextView calorieTextView = (TextView) convertView.findViewById(R.id.calorieCount);
//Set the views based on the data
foodTextView.setText(item.getName());
calorieTextView.setText(item.getCalorieCount());
}
return convertView;
}
}
And this is the code for the fragment which contains the list view:
public class DayListFragment extends Fragment {
private static final String TAG = "DayListFragment";
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment DayListFragment.
*/
// TODO: Rename and change types and number of parameters
public static DayListFragment newInstance(String param1, String param2) {
DayListFragment fragment = new DayListFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public DayListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d(TAG,"Inflating....");
View view = inflater.inflate(R.layout.fragment_day_list, container, false);
return view;
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG,"Starting....");
//Setup data source (temporary)
ArrayList<FoodItem> foodArray = new ArrayList<FoodItem>();
foodArray.add(new FoodItem("HotDog",100));
foodArray.add(new FoodItem("Cake",500));
//Set up the adapter & hook it up to the listView
FoodItemAdapter foodAdapter = new FoodItemAdapter(getActivity(),R.layout.layout_food_item,foodArray);
ListView listView = (ListView) getActivity().findViewById(R.id.listViewMain);
listView.setAdapter(foodAdapter);
//These few lines work fine on their own, so I know that findViewById works here
TextView temp = (TextView) getActivity().findViewById(R.id.tempText);
temp.setText("Test Text");
}
}
I've tried a few different solutions, but I haven't gotten things to work. I feel like there's a problem with the getView methods, like it's not inflating properly. Am I missing something?